java8 集合神操作

时间:2022-03-09 21:17:09

  

 public class StreamUtils {

     private static final List<Integer> listInteger = Lists.newArrayList(1, 2, 3, 4, 5, 6, 3, 5, 1, 4, 2, 8, 9);

     private static final List<Integer> arrayList = Lists.newArrayList(1, 25, 6, 9, 22, 44);

     public static void main(String[] args) {
///取%2的数
List<Integer> collect = listInteger.stream().filter(i -> i % 2 == 0).collect(Collectors.toList());
System.out.println(collect);
///去重
List<Integer> collect1 = listInteger.stream().distinct().collect(Collectors.toList());
System.out.println(collect1); ///跳过前面3个元素
List<Integer> collect2 = listInteger.stream().skip(3).collect(Collectors.toList());
System.out.println(collect2); ///取前面3个元素
List<Integer> collect3 = listInteger.stream().limit(3).collect(Collectors.toList());
System.out.println(collect3); ///打印dish getName集合
List<String> collect4 = list.stream().map(Dish::getName).collect(Collectors.toList());
System.out.println(collect4); String[] helloWord = {"hellow", "word"};
///{h,e,l,l,o,w},{w,o,r,d}
Stream<String[]> stream = Arrays.stream(helloWord).map(s -> s.split(""));
///h,e,l,l,o,w,w,o,r,d || flatMap 扁平化操作接受stream
Stream<String> stringStream = stream.flatMap(Arrays::stream);
///去重
stringStream.distinct().forEach(System.out::println);
//allMatch 所有的元素的满足条件
System.out.println(arrayList.stream().allMatch(i -> i > 50)); ///anyMatch 当元素数组中有一个元素满足就返回true
System.out.println(arrayList.stream().anyMatch(i -> i > 40)); ///noneMatch 没有一个元素满足的情况下返回true
System.out.println(arrayList.stream().noneMatch(i -> i < 0)); ///findAny随机获取一个元素
Optional<Integer> any = arrayList.stream().filter(i -> i > 2).findAny();
System.out.println(any.get()); ///Options 中的orElse 如果返回结果是null使用orElse可以设置默认值,返回-1
Integer integer = arrayList.stream().filter(i -> i > 66).findAny().orElse(-1);
System.out.println(integer); ///isPresent元素是否存在,ifPresent 元素存在需要做什么事情
Optional<Integer> first = arrayList.stream().filter(i -> i > 10).findFirst();
System.out.println("optional元素是否存在:"+first.isPresent());
first.ifPresent(System.out::println); //reduce 聚合函数 将数组中的元素累加 0设置默认值初始值
Integer sum = arrayList.stream().reduce(0, (x, y) -> x + y);
System.out.println(sum); ///打印数组中累加的值
arrayList.stream().reduce((x,y)->x+y).ifPresent(System.out::println); ///获取数组中的最大值
System.out.println(arrayList.stream().reduce(Integer::max).get());
///获取数组最小值
System.out.println(arrayList.stream().reduce(Integer::min).get()); ///累加
arrayList.stream().reduce(Integer::sum).ifPresent(System.out::println); 73  
///根据name分组
Map<String, List<UserInfo>> collect = listUser.stream().collect(Collectors.groupingBy(UserInfo::getName));
System.out.println(JSON.toJSONString(collect)); ///Collectors.averagingDouble 取出平均值
Optional.ofNullable(list.stream().collect(Collectors.averagingDouble(Dish::getOalories)))
.ifPresent(System.out::println); ///collectingAndThen 对结果进行处理
Optional.ofNullable(list.stream().collect(Collectors.collectingAndThen(Collectors.averagingDouble(Dish::getOalories),(a->"平均值:"+a))))
.ifPresent(System.out::println); List<Dish> dishList = list.stream().filter(d -> d.getType().equals(Dish.Type.OTHER)).collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList)); // dishList.add(new Dish("salmon", false, 550, Dish.Type.FISH)); System.out.println(JSON.toJSONString(dishList)); ///打印集合个数
Optional.ofNullable(list.stream().collect(Collectors.counting())).ifPresent(System.out::println);
///{OTHER=4, MEAT=3, FISH=2} 分组之后统计分组的个数
Optional.ofNullable(list.stream().collect(Collectors.groupingBy(Dish::getType,Collectors.counting()))).ifPresent(System.out::println);
///分组之后 求出平均值 并且返回的TreeMap
Optional.ofNullable(list.stream().collect(Collectors.groupingBy(Dish::getType, TreeMap::new,Collectors.averagingDouble(Dish::getOalories)))).ifPresent(System.out::println);
///DoubleSummaryStatistics 统计集合的值 DoubleSummaryStatistics{count=9, sum=4200.000000, min=120.000000, average=466.666667, max=800.000000}
DoubleSummaryStatistics summaryStatistics = list.stream().collect(Collectors.summarizingDouble(Dish::getOalories));
System.out.println(summaryStatistics.toString()); ///concurrentMap 和 Map使用一样
ConcurrentMap<Dish.Type, List<Dish>> collect1 = list.stream().collect(Collectors.groupingByConcurrent(Dish::getType));
System.out.println(collect1);
///转换为skipListMap
ConcurrentSkipListMap<Dish.Type, Double> collect2 = list.stream().collect(Collectors.groupingByConcurrent(Dish::getType, ConcurrentSkipListMap::new, Collectors.averagingDouble(Dish::getOalories))); String collect3 = list.stream().collect(Collectors.mapping(Dish::getName, Collectors.joining(",", "[", "]"))); System.out.println(collect3);

     }
}