当前位置:   article > 正文

Stream 33

Stream 33

 

        

  1. package Array.collection;
  2. import java.util.*;
  3. import java.util.stream.Stream;
  4. public class stream1 {
  5. public static void main(String[] args) {
  6. //、如何茯取List集合的Stream流?
  7. List<String> names = new ArrayList<>();
  8. Collections. addAll(names,"方法","过时","丰富");
  9. Stream<String> stream = names . stream();
  10. //2、如何荻取Set集合的Stream流?
  11. Set<String> set = new HashSet<>();
  12. Collections. addAll(set, "刈徳半", "張曼玉", "蜘蛛精","弓徳", "徳母西並");
  13. Stream<String> stream1 = set. stream();
  14. stream1. filter(s -> s. contains("徳")). forEach(s -> System. out. println(s));
  15. // 3、如何茯取Map集合的Stream流?
  16. Map<String, Double> map = new HashMap<>();
  17. map. put("古力娜扎",172.3);
  18. map . put("迪雨熱巴",168.3);
  19. map.put("弓尓扎哈",166.3);
  20. map. put("卞尓扎巴",168.3);
  21. Set<String> keys = map.keySet() ;
  22. Stream<String> ks = keys. stream();
  23. Collection<Double> values = map. values();
  24. Stream<Double> vS = values. stream();
  25. Set<Map. Entry<String, Double>> entries = map. entrySet();
  26. Stream<Map. Entry<String, Double>> kvs = entries. stream() ;
  27. kvs. filter(e -> e.getKey() . contains("把" )).forEach(e -> System. out
  28. .println(e.getKey()+ "-->" +e.getValue()));
  29. // 4. 如何获取数组Stream
  30. String[] names2 = {"张翠山","东方不败生","唐大山","独孤求败"};
  31. Stream<String> stream2 = Arrays. stream(names2);
  32. Stream<String> names21 = Stream. of(names2) ;
  33. }
  34. }

 

  1. package Array.collection;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.List;
  5. import java.util.stream.Stream;
  6. public class stream2 {
  7. public static void main(String[] args) {
  8. List<Integer> scores=new ArrayList<>();
  9. Collections.addAll(scores,32,45,425,55,47,747,56);
  10. //找出成绩大于56的数据,并升序后,在输出
  11. scores.stream().filter(s -> s>=56).sorted().
  12. forEach(s -> System.out.println(s));
  13. List<student> students=new ArrayList<>();
  14. student s1=new student("完全",31,123.2);
  15. student s2=new student("访问",13,123.2);
  16. student s3=new student("太她",34,123.2);
  17. student s4=new student("完全",31,123.2);
  18. Collections.addAll(students ,s1,s2,s3,s4);
  19. //找出年龄大于等于23,且年龄小于等于34的学生,并按年龄降序输出
  20. students.stream().filter(s -> s.getAge()>=23&& s.getAge()<=34 )
  21. .sorted((o1,o2) ->o2.getAge()-o1.getAge()).forEach(s ->
  22. System.out.println(s));
  23. //取出身高最高的前三名
  24. students.stream().sorted((o1,o2) ->Double.compare(o2.getHeight(),o1.getHeight()))
  25. .limit(3).forEach(s -> System.out.println(s));
  26. //取出身高倒数两名学生
  27. students.stream().sorted((o1,o2) ->Double.compare(o2.getHeight(),o1.getHeight()))
  28. .skip(students.size()-2).forEach(System.out::println);
  29. //找出身高超过123.4的学生叫什么,要求去除重复名字,在输出
  30. students.stream().filter(s -> s.getHeight()>123.4).map(s -> s.getName())
  31. .distinct().forEach(System.out::println);
  32. //自定义对象(希望内容相同就重复)重写hashcode,equals方法
  33. students.stream().filter(s -> s.getHeight()>123.4)
  34. .distinct().forEach(System.out::println);
  35. Stream<String> a=Stream.of("ewr","wrw");
  36. Stream<String> d=Stream.of("eef","wegt");
  37. Stream f=Stream.concat(a,d);
  38. f.forEach(System.out::println);
  39. }
  40. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/937483
推荐阅读
相关标签
  

闽ICP备14008679号