目录
一、概述
1. 也叫Stream流,是Jdk8开始新增的一套API(java.util.stream.*),可以用于操作集合或者数组的数据。
2. Stream流大量的结合了Lambda的语法风格来编程,提供了一种更加强大,更加简单的方式操优势:作集合或者数组中的数据,代码更简洁,可读性更好
public static void main(String[] args) {
List<String> names = new ArrayList<>();
Collections.addAll(names,"张三丰","张无忌","周芷若","赵敏","张强");
//找出姓张名字是三个字的,放在一个新集合里面
List<String> list = new ArrayList<>();
for (String name : names) {
if (name.startsWith("张") && name.length() == 3) {
list.add(name);
}
}
System.out.println(list);
//开始使用Stream流来解决
List<String> list2 = names.stream().filter(s -> s.startsWith("张")).filter(a -> a.length() == 3).collect(Collectors.toList());
System.out.println(list2);
}
二、 获取Stream流
1. 获取集合的Stream流
2. 获取数组的Stream流(两种方式)
public static void main(String[] args) {
//获取List集合的Stream流
List<String> names = new ArrayList<>();
Collections.addAll(names,"张三丰","张无忌","周芷若","赵敏","张强");
// 写names.stream 之后ctrl + alt + v 补充完整
Stream<String> stream = names.stream();
stream.filter(s -> s.startsWith("张") && s.length() == 3).forEach(a -> System.out.println(a));
//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.5);
map.put("迪丽热巴",168.3);
map.put("马尔扎哈",166.8);
map.put("卡尔扎哈",168.3);
//不可以直接用map.stream()方法,因为stream()接受的是Collection集合
//获取键
Set<String> key = map.keySet();
Stream<String> stream2 = key.stream();
//获取值
Collection<Double> values = map.values();
Stream<Double> stream3 = values.stream();
//整体处理
// 把键值封装成Entry的一个元素,再整体处理
Set<Map.Entry<String, Double>> entries = map.entrySet();
Stream<Map.Entry<String, Double>> stream4 = entries.stream();
stream4.filter(e -> e.getKey().contains("巴")).forEach(a -> System.out.println(a.getKey() + "===>" + a.getValue()));
//如何获取数组的Stream流
String[] names2 = {"张翠山","东方不败","独孤求败","大塔山"};
//两种方法 1.stream 2.of
Stream<String> stream5 = Arrays.stream(names2);
Stream<String> names21 = Stream.of(names2);
}
三、Stream流常见的中间方法
1. 中间方法指的是调用完成后会返回新的Stream流,可以继续使用(支持链式编程)
四、Stream 流常见 的终结方法
1. 终结方法指的是调用完成后,不会返回新Stream了,没法继续使用流了
2. 收集Stream流:就是把Stream流操作后的结果转回到集合或者数组中去返回
3. Stream流:方便操作集合/数组的手段; 集合/数组:才是开发中的目的
public static void main(String[] args) {
List<Double> scores = new ArrayList<>();
Collections.addAll(scores,88.5,49.6,99.9,100.6,15.2,60.8);
//需求1: 找出成绩大于60分的数据,并升序再输出
scores.stream().filter(s -> s >= 60).sorted().forEach(t -> System.out.println(t));
List<Student> students = new ArrayList<>();
students.add(new Student("蜘蛛精",26,197.6));
students.add(new Student("蜘蛛精",26,197.6));
students.add(new Student("紫霞",23,167.6));
students.add(new Student("白晶晶",25,169.0));
students.add(new Student("牛魔王",35,183.3));
students.add(new Student("牛夫人",34,168.5));
System.out.println(students);
//需求1:请计算出身高超过168的学生有几人 count
long count = students.stream().filter(s -> s.getHeight() > 168).count();
System.out.println(count);
//需求2:请找出身高最高的学生对象,并输出 max
Student s1 = students.stream().max((o1,o2) -> Double.compare(o1.getHeight(),o2.getHeight())).get();
System.out.println(s1);
//需求3:请找出身高最矮的学生对象,并输出 min
Student s2 = students.stream().min((o1,o2) -> Double.compare(o1.getHeight(),o2.getHeight())).get();
System.out.println(s2);
//需求4:请找出身高超过17的学生对象,并放到一个新集合中去返回 collect toList toSet
//流只能收集一次
List<Student> list1 = students.stream().filter(s -> s.getHeight() >= 170).collect(Collectors.toList());
System.out.println(list1);
Set<Student> list2 = students.stream().filter(s -> s.getHeight() >= 170).collect(Collectors.toSet());// 去重复
System.out.println(list2);
///需求5:请找出身高超过170的学生对象,并把学生对象的名字和身高,存入到一个Map集合返回 toMap
Map<String,Double> student2 = students.stream().filter(s -> s.getHeight() > 170)
.distinct().collect(Collectors.toMap(a -> a.getName(),a -> a.getHeight()));
System.out.println(student2);
//返回一个数组 toArray
Object[] arr = students.stream().filter(a -> a.getHeight() > 170).toArray();
Student[] arr1 = students.stream().filter(a -> a.getHeight() > 170).toArray(s -> new Student[s]);
System.out.println(Arrays.toString(arr));
}