JDK8新特性:Stream流 用法总结

本文介绍了Java8中的StreamAPI,包括如何从集合和数组获取Stream流,常见的中间操作如map、filter、limit、distinct等,以及终结方法如count、forEach、max、min等。同时展示了如何对Stream进行收集,将结果转换回集合或数组。示例代码详细展示了Stream在处理数据时的各种用法。
摘要由CSDN通过智能技术生成

目录

1.获取集合的stream流:

2.获取数组的stream流:

3.Stream流常见的中间方法

4. 终结方法

5. 收集Stream流


JDK8新增特性

作用:操作集合或者数组的数据

Stream流只能操作一次;Stream方法返回的是新的流;Stream不调用终止方法,中间的操作不会执行。

常用方法:map() 映射,(取值)

count 统计个数

filter() 过滤

limit(3) 取用前3个

skip(.size()-2) 跳过前几个

distinct() 去重

终结方法包括 count()forEach() ,max()和min().get() Collections

.collect(Collectors.toList()) 放入list集合

.collect(Collectors.toMap(a -> a.get(),a.get()) 放入map集合

.toArray(len ->new Student[len]) 转  数组

1.获取集合的stream流:

 //1.如何获取List集合的Stream流
        List<String> names = new ArrayList<>();
        Collections.addAll(names,"张三丰","刘德华","孙悟空","马德");
        Stream<String> stream = names.stream();
// 2//2.如何获取Set集合的Stream流
        Set<String> set = new HashSet<>();
        Collections.addAll(set,"刘德华1","张曼玉","郭麒麟","德华熊猫");
Stream<String> stream1 = set.stream();
stream1.filter(s -> s.contains("德")).forEach(s -> System.out.println(s));


      //3.如何获取Map集合的Stream流?
        Map<String,Double> map = new HashMap<>();
        map.put("古力娜扎",172.9);
        map.put("迪丽热巴",182.3);
        map.put("腾腾哈儿",162.3);
        map.put("卡巴哇伊",173.3);
Set<String> keys = map.keySet();
 Stream<String> ks =  keys.stream();

 Collection<Double> values = map.values();
 Stream<Double> vs = values.stream();


 Set<Map.Entry<String,Double>> entries = map.entrySet();
 Stream<Map.Entry<String,Double>> kvs = entries.stream();
 kvs.filter(e -> e.getKey().contains("巴")).
         forEach(e -> System.out.println(e.getKey()+ "-->" +e.getValue()));

2.获取数组的stream流:

String[] names2 = {"张翠山","东方不败","独孤求败","死也不变"};

Stream<String> s1 = Arrays.stream(names2);

Stream<String> s2 = Stream.of(names2);

3.Stream流常见的中间方法

List<Double> scores =new ArrayList<>();
        Collections.addAll(scores,88.5,100.0,60.0,99.8, 9.5, 99.6, 25.0) ;
// 需求1:找出成绩大于等于60分的数据,并升序后,再输出。
        scores.stream().filter(s -> s >= 60).sorted().forEach(s-> 
          System.out.println(s));

        List<Student> students=new ArrayList<>();
        Student s1= new Student("蜘蛛精",26,172.6);
        Student s2= new Student("蜘蛛精",26,172.6);
        Student s3= new Student("紫霞",23,167.6);
        Student s4= new Student("白晶晶",25,169.8);
        Student s5= new Student("牛魔王",35,183.3);
        Student s6= new Student("牛夫人",34,168.5);
        Collections.addAll(students,s1,s2,s3,s4,s5,s6);
// 需求2:找出年龄大于等于23,且年龄小于第于30岁的学生,并按照年龄降序输出。
        students.stream().filter(s->s.getAge()>=23 && s.getAge()< 30)
                .sorted((o1,o2)-> o2.getAge()-o1.getAge())
                .forEach(s ->System.out.println(s));
// 需求3:取出身高最高的前3名学生,并输出。
        students.stream().sorted((o1,o2)>Double.compare(o2.getHeight(),o1.getHeight())).limit(3).forEach(System.out::println);
// 需求4:取出身高倒数的2名学生,并输出。
      students.stream().sorted((o1,o2)>Double.compare(o2.getHeight(),o1.getHeight()))
                .skip(students.size()-2).forEach(System.out::println);
//需求5:找出身高超过168的学生叫什么名字,要求去除重复的名字,再输出。
        students.stream().filter(s->s.getHeight()>168)
          .map(Student::getName).distinct().forEach(System.out::println);
// distinct去重复,自定义类型的对象(希望内容一样就认为重复,重写hashCode,equals
        students.stream().filter(s>s.getHeight()>168).distinct().forEach(System.out::println);

        Stream<String> st1=Stream.of("张三","李四");
        Stream<String>st2=Stream.of("张三2","李四2","王五");
        Stream<String> allSt=Stream.concat(st1,st2);
        allSt.forEach(System.out::println);

4. 终结方法

指的是调用完成后,不会返回新Stream了,没法继续使用流了。

需求1:请计算出身高超过168的学生有几人。
Long size =students.stream().filter(s ->s.getheight() >168).count();
Systen.out.println(size);
// 需求2:请找出身高最高的学生对象,并龄出。
Student  s = students.stream().max((o1,o2)->Double.compare(o1.getHeight(),o2.getHeight())).get();
System.out.println(s);
//需求3,请出身高最的学生对象,并输出。
Student ss  = students.stream().min((o1,o2) -> Double.compare(o1.getHeight(),o2.geteight())).get();
System.out.println(ss);

5. 收集Stream流

把Stream流操作后的结果转回到集合或者数组中去返回

 // 即求4:请找出身高超过170的学生对象,并放到一个新集合中去返回。流只能收集一次

List<Student> students1 = students.stream().filter(a>a.getHeight()>170).collect(Collectors.toList());
System.out.println(students1);

Set<Student> students2 = students.stream().filter(a>a.getHeight()>170).collect(Collectors.toSet());
System.out.println(students2);

// 需求5:请找出身高超过170的学生对象,并把学生对象的名字和身高,存入到一个Map 集合返回。

Map<String, Double> map =
students.stream().filter(a->a.getHeight() >170)
        .distinct().collect(Collectors.toMap(a->a.getName(),a->a.getHeight()));
System.out.println(map);

//把流收集到数组中
Student[] arr = students.stream().filter(a ->a.getHeight()>170).toArray(len->new Student[len]);
System.out.println(Arrays.toString(arr));
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值