stream使用

目录

1.筛选与切片

2.映射map

3.peek

4.Collector工具库:Collectors


1.筛选与切片

filter:过滤流中的某些元素;

limit(n):获取n个元素;

skip(n):跳过n个元素,配合limit可实现分页;

distinct:通过流中元素的hashCode()和equals()去除重复元素;

Stream<Integer> stream=Stream.of(4,3,1,8,65,2,8,4,67,9);
Stream<Integer> stream1=stream.filter(s->s>3) //4,8,65,8,4,67,9
                              .distinct() //4,8,65,67,9
                              .skip(1) //8,65,67,9
                              .limit(2); //8,65
stream1.forEach(System.out::println);

2.映射map

map:接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素;

flatMap:接收一个行数作为参数,将流中的每个值都换成另一个流,然后把所有流连城一个流;

List<String> list= Arrays.asList("z,h,a,o","y,a,n");
List<String> list1=list.stream()
                       .map(s->s.replaceAll(",",""))
                       .collect(Collectors.toList());
System.out.println(list1);
// [zhao, yan]
List<String> list= Arrays.asList("z,h,a,o","y,a,n");
List<String> list2=list.stream().flatMap(s->{
    String[] arr=s.split(",");
    Stream<String> s1=Arrays.stream(arr);
    return s1;
}).collect(Collectors.toList());
System.out.println(list2);
// [z, h, a, o, y, a, n]

3.peek

peek,和map相似,能得到流中的每一个元素。但map接收的是一个Function表达式,有返回值;而peek接收的是Consumer表达式,无返回值。

Student s1=new Student("haha",1);
Student s2=new Student("hihi",2);
List<Student> list= Arrays.asList(s1,s2);
List<Student> list1=list.stream()
                        .peek(s->s.setAge(4))
                        .collect(Collectors.toList());
for (int i = 0; i < list1.size(); i++) {
    System.out.println(list1.get(i).getAge());
}

4.Collector工具库:Collectors

Student s1=new Student("haha",1);
Student s2=new Student("hihi",2);
List<Student> list= Arrays.asList(s1,s2);
// 将age转成List
List<Integer> list2=list.stream()
        .map(Student::getAge)
        .collect(Collectors.toList());
System.out.println(list2);

//  转成set
Set<Integer> set2=list.stream()
        .map(Student::getAge)
        .collect(Collectors.toSet());
System.out.println(set2);

// 转成map key不能相同,否则报错
Map<String,Integer> map2=list.stream()
        .collect(Collectors.toMap(Student::getName,Student::getAge));
System.out.println(map2);

字符串分隔符连接:

Student s1=new Student("haha",1);
Student s2=new Student("hihi",2);
List<Student> list= Arrays.asList(s1,s2);
String str=list.stream()
        .map(Student::getName)
        .collect(Collectors.joining(",","(",")"));
System.out.println(str);
// (haha,hihi)

聚合操作:

Student s1=new Student("haha",1);
Student s2=new Student("hihi",2);
List<Student> list= Arrays.asList(s1,s2);
// 求学生总数
Long count=list.stream().collect(Collectors.counting());
// 求学生最大年纪
Integer max=list.stream().map(Student::getAge).collect(Collectors.maxBy(Integer::compare)).get();
// 求最小年纪
Integer min=list.stream().map(Student::getAge).collect(Collectors.minBy(Integer::compare)).get();        // 求所有年纪之和
Double dou=list.stream().collect(Collectors.summingDouble(Student::getAge));
// 求平均年纪
Double dou1=list.stream().collect(Collectors.averagingDouble(Student::getAge));

 分组groupingBy

Student s1=new Student("haha",1);
Student s2=new Student("hihi",2);
Student s3=new Student("hei",1);
List<Student> list= Arrays.asList(s1,s2,s3);
// 根据age分组
Map<Integer,List<Student>> listMap=list.stream().collect(Collectors.groupingBy(Student::getAge));

// 多重分组,先根据age分,在根据name分
Map<Integer,Map<String,List<Student>>> listMap1=list.stream().collect(Collectors.groupingBy(Student::getAge,Collectors.groupingBy(Student::getName)));

//分区
Map<Boolean, List<Student>> listMap2=list.stream().collect(Collectors.partitioningBy(s->s.getAge()>1));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值