赞
踩
- package Array.collection;
-
- import java.util.*;
- import java.util.stream.Stream;
-
- public class stream1 {
- public static void main(String[] args) {
-
- //、如何茯取List集合的Stream流?
- List<String> names = new ArrayList<>();
- Collections. addAll(names,"方法","过时","丰富");
- Stream<String> stream = names . stream();
-
- //2、如何荻取Set集合的Stream流?
- Set<String> set = new HashSet<>();
- Collections. addAll(set, "刈徳半", "張曼玉", "蜘蛛精","弓徳", "徳母西並");
- 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.3);
- map . put("迪雨熱巴",168.3);
- map.put("弓尓扎哈",166.3);
- map. put("卞尓扎巴",168.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()));
-
- // 4. 如何获取数组Stream
- String[] names2 = {"张翠山","东方不败生","唐大山","独孤求败"};
- Stream<String> stream2 = Arrays. stream(names2);
- Stream<String> names21 = Stream. of(names2) ;
-
- }
- }

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

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。