java stream的常用例子
Posted 轻语~
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java stream的常用例子相关的知识,希望对你有一定的参考价值。
一、标题
java stream的常用例子
二、描述
stream在当前互联网技术社区传播的已经很广泛了,且有阿里11.11用stream很好的完成数据处理案例,为此迎着互联网技术风口细细地学习一下stream,说不定能让代码飘起来
三、常用的例子
以下内容均使用该代码作为前提:
1 public class AppTest { 2 3 public static void main(String[] args){ 4 List<StreamDemo> list = new ArrayList<>(); 5 list.add(StreamDemo.builder().dateValue(LocalDate.now()).intValue(10).strValue("str1").descValue("str1").timeValue(LocalTime.now()).dateTimeValue(LocalDateTime.now()).bigDecimal(BigDecimal.TEN).build()); 6 list.add(StreamDemo.builder().dateValue(LocalDate.now()).intValue(15).strValue("str3").descValue("str2").timeValue(LocalTime.now()).dateTimeValue(LocalDateTime.now()).bigDecimal(BigDecimal.ONE).build()); 7 list.add(StreamDemo.builder().dateValue(LocalDate.now()).intValue(11).strValue("str6").descValue("str3").timeValue(LocalTime.now()).dateTimeValue(LocalDateTime.now()).bigDecimal(BigDecimal.ZERO).build()); 8 list.add(StreamDemo.builder().dateValue(LocalDate.now()).intValue(16).strValue("str8").descValue("str4").timeValue(LocalTime.now()).dateTimeValue(LocalDateTime.now()).bigDecimal(BigDecimal.TEN).build()); 9 } 10 } 11 @Builder 12 @AllArgsConstructor 13 @Data 14 class StreamDemo{ 15 private String strValue; 16 private String descValue; 17 private Integer intValue=2; 18 private LocalDate dateValue; 19 private LocalTime timeValue; 20 private LocalDateTime dateTimeValue; 21 private BigDecimal bigDecimal; 22 23 public String toString() { 24 return "StreamDemo{" + 25 "strValue=‘" + strValue + ‘‘‘ + 26 ", descValue=‘" + descValue + ‘‘‘ + 27 ", intValue=" + intValue + 28 ", dateValue=" + dateValue + 29 ", timeValue=" + timeValue + 30 ", dateTimeValue=" + dateTimeValue + 31 ", bigDecimal=" + bigDecimal + 32 ‘}‘; 33 } 34 }
1、操作数值
1.1、获取累积指
System.out.println(Stream.of(list.get(0),list.get(1),list.get(2)).map(StreamDemo::getIntValue).reduce(10,(n,m)->n*m));
System.out.println(Stream.of(list.get(0),list.get(1),list.get(2)).map(StreamDemo::getIntValue).collect(Collectors.reducing((n,m)->n+m)).get());
1.2、获取最大值
System.out.println(Stream.of(list.get(0),list.get(1),list.get(2)).map(StreamDemo::getIntValue).max((n,m)->n.compareTo(m)).get());
System.out.println(Stream.of(list.get(0),list.get(1),list.get(2)).collect(Collectors.maxBy((n,m)->n.getIntValue().compareTo(m.getIntValue()))).get());
1.3、获取最小值
System.out.println(Stream.of(list.get(0),list.get(1),list.get(2)).map(StreamDemo::getIntValue).min((n,m)->n.compareTo(m)).get());
System.out.println(Stream.of(list.get(0),list.get(1),list.get(2)).collect(Collectors.minBy((n,m)->n.getIntValue().compareTo(m.getIntValue()))).get());
1.4、获取数值集合
Stream.of(list.get(0),list.get(1),list.get(2)).mapToInt(StreamDemo::getIntValue).forEach(System.out::println);
Stream.of(list.get(0),list.get(1),list.get(2)).collect(Collectors.mapping(StreamDemo::getIntValue,Collectors.toList())).forEach(System.out::println);
1.5、将多个数值集合合并为一个数值集合
Stream.of(list.get(0),list.get(1),list.get(2)).flatMapToInt(f-> IntStream.concat(IntStream.of(f.getIntValue()),IntStream.of(f.getBigDecimal().intValue()))).sorted().forEachOrdered(System.out::println);
1.6、获取平均值
System.out.println(Stream.of(list.get(0),list.get(1),list.get(2)).collect(Collectors.averagingInt(StreamDemo::getIntValue)).intValue());
1.7、获取数据统计
System.out.println(Stream.of(list.get(0),list.get(1),list.get(2)).collect(Collectors.summarizingInt(StreamDemo::getIntValue)).toString());
2、操作集合
2.1:、获取集合长度
System.out.println(Stream.of(list.get(0),list.get(1),list.get(2)).count());
System.out.println(Stream.of(list.get(0),list.get(1),list.get(2)).collect(Collectors.counting()));
2.2、限制集合长度
Stream.of(list.get(0),list.get(1),list.get(2)).limit(1).forEach(System.out::println);
2.3、跳过集合前几项
Stream.of(list.get(0),list.get(1),list.get(2)).skip(1).forEach(System.out::println);
2.4、集合排序
Stream.of(list.get(0),list.get(1),list.get(2)).sorted((n,m)->n.getIntValue().compareTo(m.getIntValue())).forEach(System.out::println);
2.5、去重
Stream.of(list.get(0),list.get(1),list.get(2)).distinct().forEach(System.out::println);
2.6、集合过滤
Stream.of(list.get(0),list.get(1),list.get(2)).filter(streamDemo -> StringUtils.isEmpty(streamDemo.getStrValue())).forEach(System.out::println); System.out.println(Stream.of(list.get(0),list.get(1),list.get(2)).noneMatch(streamDemo -> StringUtils.isEmpty(streamDemo.getStrValue()))); System.out.println(Stream.of(list.get(0),list.get(1),list.get(2)).anyMatch(streamDemo -> StringUtils.isEmpty(streamDemo.getStrValue()))); System.out.println(Stream.of(list.get(0),list.get(1),list.get(2)).allMatch(streamDemo -> StringUtils.isEmpty(streamDemo.getStrValue()))); Stream.of(list.get(0),list.get(1),list.get(2)).collect(Collectors.partitioningBy(streamDemo -> StringUtils.isEmpty(streamDemo.getStrValue()))).forEach((k,v)->System.out.printf("k:%s,v:%s ",k,v));
2.7、给集合每项添加内容
Stream.of(list.get(0),list.get(1),list.get(2)).peek(streamDemo -> streamDemo.setDescValue(streamDemo.getStrValue())).forEach(System.out::println);
2.8、集合分组
Stream.of(list.get(0),list.get(1),list.get(2)).collect(Collectors.groupingBy(StreamDemo::getStrValue,Collectors.toList())).forEach((k,v)->System.out.printf("k:%s,v:%s ",k,v)); Stream.of(list.get(0),list.get(1),list.get(2)).collect(Collectors.groupingBy(StreamDemo::getStrValue,Collectors.groupingBy(StreamDemo::getStrValue))).forEach((k,v)->System.out.printf("k:%s,v:%s ",k,v)); System.out.println(Stream.of(list.get(0),list.get(1),list.get(2)).map(StreamDemo::getStrValue).collect(Collectors.joining(";"))); Stream.of(list.get(0),list.get(1),list.get(2)).collect(Collectors.toMap(StreamDemo::getStrValue,StreamDemo::getDescValue)).forEach((k,v)->System.out.printf("k:%s,v:%s ",k,v)); System.out.println(Stream.of(list.get(0),list.get(1),list.get(2)).collect(Collectors.mapping(StreamDemo::getStrValue,Collectors.joining(";"))));
2.9、合并集合
Stream.of(list.get(0),list.get(1),list.get(2)).flatMap(f->Stream.concat(Stream.of(f.getIntValue()),Stream.of(f.getBigDecimal()))).forEach(System.out::println);
2.10、将集合转为数组
System.out.println(Arrays.toString(Stream.of(list.get(0),list.get(1),list.get(2)).toArray(t->new StreamDemo[t])));
以上是关于java stream的常用例子的主要内容,如果未能解决你的问题,请参考以下文章