一,入门
1.可以用于操作集合和数组的数据
2.优势:代码更简洁,可读性更好
List<String> names=new ArrayList<>();
Collections.addAll(names,"张三丰","张无忌","周芷若");
System.out.println(names);
List<String> list=new ArrayList<>();
for (String name : names) {
if (name.startsWith("张")){
list.add(name);
}
}
System.out.println(list);
List<String> list2= names.stream().filter(s -> s.startsWith("张")).filter(a -> a.length() == 3).collect(Collectors.toList());
System.out.println(list2);
二,常见方法
//1.list
List<String> names = new ArrayList<>();
Collections.addAll(names, "张三丰", "张无忌", "周芷若");
Stream<String> stream = names.stream();
//2.set
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
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<String> vs = keys.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.数组
String[] names2 = {"张三丰", "张无忌", "周芷若"};
Stream<String> s1 = Arrays.stream(names2);
Stream<String> s2 = Stream.of(names2);
三,常见中间方法
List<Double> scores=new ArrayList<>();
Collections.addAll(scores,88.5,100.0,60.0,99.0,99.6,25.0);
//1.找出大于60分的数据,升序
scores.stream().filter(s->s>=60).forEach(System.out::println);
List<Student> students=new ArrayList<>();
Student s1=new Student("蜘蛛精",26,172.5);
Student s2=new Student("蜘蛛精",26,172.5);
Student s3=new Student("紫霞",25,167.5);
Student s4=new Student("牛魔王",35,168.5);
Collections.addAll(students,s1,s2,s3,s4);
System.out.println(students);
//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.找出身高最高的前三位
students.stream().sorted((o1, o2) -> Double.compare(o2.getHeight(), o1.getHeight())).limit(3).forEach(s->System.out.println(s));
System.out.println("----------------------------------");
//4.找出身高倒数的后两位
students.stream().sorted((o1, o2) -> Double.compare(o2.getHeight(),o1.getHeight())).limit(students.size()-2).forEach(System.out::println);
//5.找出身高超过168的学生名字,除去重复(distinct)
students.stream().filter(s->s.getHeight()>168).map(s->s.getName()).distinct().forEach(System.out::println);
四,常见终极方法
List<Student> students=new ArrayList<>();
Student s1=new Student("蜘蛛精",26,172.5);
Student s2=new Student("蜘蛛精",26,172.5);
Student s3=new Student("紫霞",25,167.5);
Student s4=new Student("牛魔王",35,168.5);
Collections.addAll(students,s1,s2,s3,s4);
//1.需求:计算出身高超出168的几人
Long size=students.stream().filter(s->s.getHeight()>168).count();
System.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.getHeight())).get();
System.out.println(ss);
//4.中出身高超过170的学生对象,收集到一个集合并返回
//流只能收集一次
List<Student> students1=students.stream().filter(a->a.getHeight()>170).collect(Collectors.toList());
System.out.println(students1);
//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);
//数组
//Object[] arr=students.stream().filter(a->a.getHeight()>170).toArray();
Student[] arr=students.stream().filter(a->a.getHeight()>170).toArray(len->new Student[len]);
System.out.println(Arrays.toString(arr));