Java8-Stream collect

112人阅读 评论(0)

package lambda;

import java.util.Arrays;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;

public class StreamCollectExam {
public static void collectExam1() {
List<String> numbers = Arrays.asList("1", "2", "3", "4", "1", "2", "7", "8");

// [1, 2, 3, 4, 7, 8]
SortedSet<String> uniqueSortedNumbers = numbers.stream().collect(Collectors.toCollection(TreeSet::new));
System.out.println(uniqueSortedNumbers);

// [1, 2, 3, 4, 1, 2, 7, 8]
List<String> listNumbers = numbers.stream().collect(Collectors.toList());
System.out.println(listNumbers);
}

/**
* The combiner is used only in a parallel stream.
*/
public static void collectExam2() {
List<String> numbers = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8");
/**
* print result:
in supplier, create a StringBuilder
in peek,param=1
in accumulator,param=1,result[before]=,result[after]=1
in peek,param=2
in accumulator,param=2,result[before]=1,result[after]=12
in peek,param=3
in accumulator,param=3,result[before]=12,result[after]=123
in peek,param=4
in accumulator,param=4,result[before]=123,result[after]=1234
in peek,param=5
in accumulator,param=5,result[before]=1234,result[after]=12345
in peek,param=6
in accumulator,param=6,result[before]=12345,result[after]=123456
in peek,param=7
in accumulator,param=7,result[before]=123456,result[after]=1234567
in peek,param=8
in accumulator,param=8,result[before]=1234567,result[after]=12345678
streamNumbers=12345678
*/
StringBuilder streamNumbers = numbers.stream().peek(StreamCollectExam::peek)
.collect(StreamCollectExam::supplier, StreamCollectExam::accumulator, StreamCollectExam::combiner);
System.out.println("streamNumbers=" + streamNumbers);
System.out.println("-------------------------");

/**
* print result:
in supplier, create a StringBuilder
in peek,param=5
in accumulator,param=5,result[before]=,result[after]=5
in peek,param=6
in accumulator,param=6,result[before]=5,result[after]=56
in supplier, create a StringBuilder
in peek,param=7
in accumulator,param=7,result[before]=,result[after]=7
in peek,param=8
in accumulator,param=8,result[before]=7,result[after]=78
in supplier, create a StringBuilder
in peek,param=3
in accumulator,param=3,result[before]=,result[after]=3
in peek,param=4
in accumulator,param=4,result[before]=3,result[after]=34
in supplier, create a StringBuilder
in peek,param=1
in accumulator,param=1,result[before]=,result[after]=1
in peek,param=2
in accumulator,param=2,result[before]=1,result[after]=12
parallelStreamNumbers=12345678
*/
/**
*   <R> R collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner)
*      A supplier that supplies a mutable container to store the results.For a
*                 parallel execution, this function may be called
*                 multiple times and must return a fresh value each time
*      An accumulator that accumulates the results into the mutable container.
*      A combiner that combines the partial results when used in parallel.
*
*   First, create a supplier which returns an StringBuilder
*   Second, create an accumulator that receives two arguments which is the container
*          returned from the supplier(StringBuilder in this case). and the element of the stream.
*   Finally, a combiner combines the results into one StringBuilder
*/
StringBuilder parallelStreamNumbers = numbers.parallelStream().peek(StreamCollectExam::peek)
.collect(StreamCollectExam::supplier, StreamCollectExam::accumulator, StreamCollectExam::combiner);
System.out.println("parallelStreamNumbers=" + parallelStreamNumbers);

}

public static void peek(String param) {
System.out.println("in peek,param=" + param);
}

private static StringBuilder supplier() {
System.out.println("in supplier, create a StringBuilder");
return new StringBuilder();
}

public static void accumulator(StringBuilder result, String param) {
System.out.print("in accumulator,param=" + param + ",result[before]=" + result);
result.append(param);
System.out.println(",result[after]=" + result);
}

public static void combiner(StringBuilder result, StringBuilder additional) {
System.out.println(",result[after]=" + result);
}

public static void main(String[] args) {
collectExam1();
collectExam2();
}
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：1561次
• 积分：82
• 等级：
• 排名：千里之外
• 原创：7篇
• 转载：0篇
• 译文：0篇
• 评论：0条
文章分类
文章存档
阅读排行
评论排行