首页 > 编程语言 > 详细

java8 Collectors常用流API

时间:2021-06-19 09:17:45      阅读:19      评论:0      收藏:0      [点我收藏+]
Stu stu = new Stu();
stu.setId(10007L);
stu.setName("张收纳");

Stu stu1 = new Stu();
stu1.setId(18562L);
stu1.setName("李四");

Stu stu3 = new Stu();
stu3.setId(10087L);
stu3.setName("张收纳");
List<Stu> stus = Arrays.asList(stu, stu1,stu3);
/**
* 对象字段转map 这个没啥好说的
*/
Map<Long, Stu> collect = stus.stream().collect(Collectors.toMap(Stu::getId, stu2 -> stu2));
Stu stu2 = collect.get(10007L);
System.out.println(stu2);


/**
* 将数据收到到Collectors.toSet()中最后在转成 ArrayList
*/
List<String> list
= Stream.of("Demo1", "Demo2","Demo1").collect(Collectors.collectingAndThen(
Collectors.toSet(),
ArrayList::new));
System.out.println(list);


/**
* 求平均值
*/
Stream<String> stream = Stream.of("20", "50");
double res = stream.collect(Collectors.averagingDouble(a -> Double.parseDouble(a)));
System.out.println("Arithmetic Mean of the stream elements = "+res);
/**
* 求元素个数
*/
Stream<String> counting = Stream.of("20", "50");
Long collect1 = counting.collect(Collectors.counting());
System.out.println(collect1);

/**
* 收集对象中某个字段转成一个集合
*/
List<String> userNames = stus.stream().collect(Collectors.mapping(Stu::getName, Collectors.toList()));
userNames.forEach(System.out::println);
/**
* 自定义收集的集合
*/
HashSet<Stu> collect2 = stus.stream().collect(Collectors.toCollection(HashSet::new));
collect2.forEach(System.out::println);
/**
* 将集合变成字符串 20,50
*/
Stream<String> joining = Stream.of("20", "50");
String collect3 = joining.collect(Collectors.joining(","));
System.out.println(collect3);
/**
* 获取最大值
*/
Stream<Integer> maxBy = Stream.of(10, 20, 30);
Optional<Integer> collect4 = maxBy.collect(Collectors.maxBy(Integer::compareTo));
System.out.println(collect4.get());

/**
* 获取最小值
*/
Stream<Integer> minBy = Stream.of(10, 20, 30);
Optional<Integer> collect5 = minBy.collect(Collectors.minBy(Integer::compareTo));
System.out.println(collect5.get());
/**
* 分组 ,这里可以求分组数量,分组后的总和
*/
Stu s1 = new Stu();
s1.setId(10007L);
s1.setName("张收纳");
Stu s2 = new Stu();
s2.setId(18562L);
s2.setName("李四");
Stu s3 = new Stu();
s3.setId(10087L);
s3.setName("张收纳");
ArrayList<Stu> stus1 = Lists.newArrayList(s1, s2, s3);
Map<Long, List<Stu>> collect6 = stus1.stream().collect(Collectors.groupingBy(Stu::getId));

for (Map.Entry<Long, List<Stu>> entry : collect6.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}

/**
* 道理和上面那个是一样的,这个是线程安全的
*/
//ConcurrentMap<Long, List<Stu>> collect6 = stus1.stream().collect(Collectors.groupingByConcurrent(Stu::getId));
/**
* 求集合中所有和 Optional<BigDecimal> collect7 = reducing.collect(Collectors.reducing(BigDecimal::add));
*
* 第一个BigDecimal.ONE 加集合所有的 reducing.collect(Collectors.reducing(BigDecimal.ONE, BigDecimal::add));
*
* a=BigDecimal.ONE b=全部集合的和 BigDecimal collect7 = reducing.collect(Collectors.reducing(BigDecimal.ONE, (a, b) -> a.subtract(b)));
*/
Stream<BigDecimal> reducing = Stream.of(BigDecimal.TEN, BigDecimal.ONE, BigDecimal.ONE);
BigDecimal collect7 = reducing.collect(Collectors.reducing(BigDecimal.ONE, BigDecimal::add));
System.out.println(collect7);
/**
* 将一个集合拆分成两个集合,满足条件的一个,不满足条件的一个
*/
Stream<Integer> partitioningBy = Stream.of(1, 2, 3);
Map<Boolean, List<Integer>> collect8 = partitioningBy.collect(Collectors.partitioningBy(integer -> integer.equals(1)));

for (Map.Entry<Boolean, List<Integer>> map: collect8.entrySet()){
System.out.println("Key = " + map.getKey() + ", Value = " + map.getValue());
}
/**
* 求和
*/
Stream<Integer> summingInt = Stream.of(1, 2, 3);
Integer collect9 = summingInt.collect(Collectors.summingInt(Integer::intValue));
System.out.println(collect9);

java8 Collectors常用流API

原文:https://www.cnblogs.com/521315lvyy/p/14901762.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!