码迷,mamicode.com
首页 > 编程语言 > 详细

java stream的常用例子

时间:2019-12-07 23:27:58      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:lis   gif   count()   click   concat   intvalue   ISE   empty   spl   

一、标题
  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     }
View Code

  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());
View Code

    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());
View Code

    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());
View Code

    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);
View Code

    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);
View Code

    1.6、获取平均值

技术图片
System.out.println(Stream.of(list.get(0),list.get(1),list.get(2)).collect(Collectors.averagingInt(StreamDemo::getIntValue)).intValue());
View Code

    1.7、获取数据统计

技术图片
System.out.println(Stream.of(list.get(0),list.get(1),list.get(2)).collect(Collectors.summarizingInt(StreamDemo::getIntValue)).toString());
View Code

 

 

  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()));
View Code

    2.2、限制集合长度

技术图片
Stream.of(list.get(0),list.get(1),list.get(2)).limit(1).forEach(System.out::println);
View Code

    2.3、跳过集合前几项

技术图片
Stream.of(list.get(0),list.get(1),list.get(2)).skip(1).forEach(System.out::println);
View Code

    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);
View Code

    2.5、去重

技术图片
Stream.of(list.get(0),list.get(1),list.get(2)).distinct().forEach(System.out::println);
View Code

    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\n",k,v));
View Code

    2.7、给集合每项添加内容

技术图片
Stream.of(list.get(0),list.get(1),list.get(2)).peek(streamDemo -> streamDemo.setDescValue(streamDemo.getStrValue())).forEach(System.out::println);
View Code

    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\n",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\n",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\n",k,v));
    System.out.println(Stream.of(list.get(0),list.get(1),list.get(2)).collect(Collectors.mapping(StreamDemo::getStrValue,Collectors.joining(";"))));
View Code

    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);
View Code

    2.10、将集合转为数组

技术图片
System.out.println(Arrays.toString(Stream.of(list.get(0),list.get(1),list.get(2)).toArray(t->new StreamDemo[t])));
View Code

 

 

 

  

    

 

 

 

 

 

 

 

 

 

 

java stream的常用例子

标签:lis   gif   count()   click   concat   intvalue   ISE   empty   spl   

原文地址:https://www.cnblogs.com/lswater/p/12003473.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!