用法:
直接上代码
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;
/**
* Created by macro on 2022/3/19
*
* @author hhoa
**/
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();
//使用Collectors的toUnmodifiableList()方法
List<Integer> collect = stream.collect(Collectors.toUnmodifiableList());
Assertions.assertThrows(Exception.class,()-> collect.add(1), "没有抛出异常");
System.out.println("抛出了异常");
}
/**
* 测试Collectors中的AveragingDouble
* 同理:
* averagingDouble、averagingInt、averagingLong一样
*/
@DisplayName("测试返回平均值")
@Test
public void testAveragingDouble(){
Stream<Double> stream = doubleList.stream();
Stream<Integer> integerStream = integerList.stream();
//求double数组的平均值
Double collect = stream.collect(Collectors.averagingDouble((a) -> a));
System.out.println("double数组的平均值: " +collect);
//把Integer转化为double再求平均值
Double collect1 = integerStream.collect(Collectors.averagingDouble(Integer::doubleValue));
System.out.println("integer数组的平均值: " + collect1);
}
/**
* 经过一个collector转换后再执行一个Function
*/
@DisplayName("测试CollectingAndThen")
@Test
public void testCollectingAndThen(){
Stream<Integer> integerStream = integerList.stream();
//把Integer转化为double再求平均值
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);
}
/**
* 分类分类结果保存在Map中
*/
@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));
}
/**
* 类似于GroupBy
*/
@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);
}
/**
* 类似maxBy或minBy
* maxBy和minBy的底层也是用BinaryOperator.minBy或BinaryOperator.maxBy来写的
*/
@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));
}
/**
* 获取一个对数组转化为int型之后的统计资料
* return Statistics - 里面包含转成的int值,还包括min, max, average等
*
* 同理还有summarizingDouble, summarizingLong
*
*/
@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);
}
/**
* 返回流内数据转化为Int后的和
* 同理还有summingDouble, summingInt
*/
@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);
}
/**
* 根据Function获取key,和value并注入一个Map中,最后返回map
*
*/
@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);
}
/**
* 把数组转化为set集合,里面相同的元素只会保留一个
*/
@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");
}
}