用法: 直接上代码
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.*;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class TestCollector {
private List<Double> doubleList;
private List<Integer> integerList;
private List<String> stringList;
@DisplayName("测试返回不可变数组")
@Test
public void testToUnmodifiableList(){
Stream<Integer> stream = integerList.stream();
List<Integer> collect = stream.collect(Collectors.toUnmodifiableList());
Assertions.assertThrows(Exception.class,()-> collect.add(1), "没有抛出异常");
System.out.println("抛出了异常");
}
@DisplayName("测试返回平均值")
@Test
public void testAveragingDouble(){
Stream<Double> stream = doubleList.stream();
Stream<Integer> integerStream = integerList.stream();
Double collect = stream.collect(Collectors.averagingDouble((a) -> a));
System.out.println("double数组的平均值: " +collect);
Double collect1 = integerStream.collect(Collectors.averagingDouble(Integer::doubleValue));
System.out.println("integer数组的平均值: " + collect1);
}
@DisplayName("测试CollectingAndThen")
@Test
public void testCollectingAndThen(){
Stream<Integer> integerStream = integerList.stream();
Boolean collect = integerStream.collect(Collectors.collectingAndThen(Collectors.averagingInt((a) -> a), (a) -> a > 5));
testAveragingDouble();
System.out.println("integer数组求平均值之后再判断是否>5: " + collect);
}
@DisplayName("测试Counting")
@Test
public void testCounting(){
Stream<Integer> integerStream = integerList.stream();
Long collect = integerStream.collect(Collectors.counting());
System.out.println("数组个数: " + collect);
}
@DisplayName("测试GroupBy")
@Test
public void testGroupBy(){
Stream<Integer> integerStream = integerList.stream();
Map<Object, List<Integer>> collect = integerStream.collect(Collectors.groupingBy((t)->t > 5));
System.out.println("根据是否>5分类");
for (Object o : collect.entrySet()) {
System.out.println(o);
}
integerStream = integerList.stream();
Map<Boolean, Long> collect1 = integerStream.collect(Collectors.groupingBy((t) -> t > 5, Collectors.counting()));
System.out.println("获取大于5和不大于的5的数的个数");
for (Object o : collect1.entrySet()) {
System.out.println(o);
}
}
@DisplayName("测试Joining")
@Test
public void testJoining(){
Stream<String> stringStream = stringList.stream();
String collect = stringStream.collect(Collectors.joining());
System.out.println("将所有字符串串联起来");
System.out.println(collect);
stringStream = stringList.stream();
collect = stringStream.collect(Collectors.joining("-"));
System.out.println("用-将所有字符串串联起来");
System.out.println(collect);
stringStream = stringList.stream();
collect = stringStream.collect(Collectors.joining("-", "[", "]"));
System.out.println("用-将所有字符串串并用[]将修饰他们联起来");
System.out.println(collect);
}
@DisplayName("测试Mapping")
@Test
public void testMapping(){
Stream<Integer> stream = integerList.stream();
Double collect = stream.collect(Collectors.mapping((t) -> t * 2, Collectors.averagingInt((t) -> t)));
System.out.println(collect);
}
@DisplayName("测试MaxBy")
@Test
public void testMaxBy(){
Stream<Integer> stream = integerList.stream();
Optional<Integer> collect = stream.collect(Collectors.maxBy((a, b) -> a - b));
System.out.println(collect.orElse(null));
}
@DisplayName("测试minBy")
@Test
public void testMinBy(){
Stream<Integer> stream = integerList.stream();
Optional<Integer> collect = stream.collect(Collectors.minBy((a, b) -> a - b));
System.out.println(collect.orElse(null));
}
@DisplayName("测试PartitionBy")
@Test
public void testPartitionBy(){
Stream<Integer> stream = integerList.stream();
Map<Boolean, List<Integer>> collect = stream.collect(Collectors.partitioningBy((t) -> t > 5));
System.out.println(collect);
}
@DisplayName("测试Reducing")
@Test
public void testReducing(){
Stream<Integer> stream = integerList.stream();
Optional<Integer> collect = stream.collect(Collectors.reducing(BinaryOperator.minBy((a, b)->a - b)));
System.out.println(collect.orElse(null));
}
@DisplayName("测试summarizingInt")
@Test
public void testSummarizingInt(){
Stream<String> stream = stringList.stream();
IntSummaryStatistics collect = stream.collect(Collectors.summarizingInt((t) -> {
switch (t) {
case "zhangsan":
return 1;
case "lisi":
return 2;
case "wangwu":
return 3;
default:
return 4;
}
}));
double average = collect.getAverage();
System.out.println(collect);
System.out.println(average);
}
@DisplayName("测试SummingInt")
@Test
public void testSummingInt(){
Stream<Integer> stream = integerList.stream();
Integer collect = stream.collect(Collectors.summingInt((t) -> t));
System.out.println(collect);
}
@DisplayName("测试ToList")
@Test
public void testToList(){
Stream<Integer> stream = integerList.stream();
List<Integer> collect = stream.collect(Collectors.toList());
System.out.println(collect);
}
@DisplayName("测试toMap")
@Test
public void testToMap(){
Stream<Integer> stream = integerList.stream();
Map<Integer, Integer> collect = stream.collect(Collectors.toMap((t) -> t*10, (t) -> t));
System.out.println(collect);
}
@DisplayName("测试toSet")
@Test
public void testToSet(){
Stream<Integer> stream = integerList.stream();
Set<Integer> collect = stream.collect(Collectors.toSet());
System.out.println(collect);
}
@DisplayName("----------------------------------分界线-------------------------------------------------")
@BeforeEach
public void getStringList(){
List<String> list = Arrays.asList("zhangsan", "lisi", "wangwu");
System.out.print("字符串数组:[");
this.stringList = list;
for (String str: list){
System.out.print(str+ " ");
}
System.out.print("]\n");
}
@BeforeEach
public void getIntegerList(){
List<Integer> list = new ArrayList<>();
System.out.print("整型数组:[");
for (int i = 0; i < 4; i++){
int num = Math.abs(new Random().nextInt() % 10);
list.add(num);
System.out.print(num+ " ");
}
this.integerList = list;
System.out.print("]\n");
}
@BeforeEach
public void getDoubleList(){
List<Double> list = new ArrayList<>();
System.out.print("Double数组:[ ");
for (int i = 0; i < 4; i++){
double num = Math.abs(new Random().nextDouble() % 10);
list.add(num);
System.out.print(num+ " ");
}
this.doubleList = list;
System.out.print("]\n");
}
}
|