JDK8新特性:stream流
Stream流体验
案例需求:有一个List集合,元素有"张三丰","张无忌","周芷若","赵敏","张强"
,找出姓张,且是3个字的名字,存入到一个新集合中去。
List<String> names = new ArrayList<>();
Collections.addAll(names, "张三丰","张无忌","周芷若","赵敏","张强");
System.out.println(names);
- 用传统方式来做,代码是这样的
// 找出姓张,且是3个字的名字,存入到一个新集合中去。
List<String> list = new ArrayList<>();
for (String name : names) {
if(name.startsWith("张") && name.length() == 3){
list.add(name);
}
}
System.out.println(list);
- 用Stream流来做,代码是这样的(ps: 是不是想流水线一样,一句话就写完了)
List<String> list2 = names.stream().filter(s -> s.startsWith("张")).filter(a -> a.length()==3).collect(Collectors.toList());
System.out.println(list2);
学习路线
获取stream流
主要掌握下面四点:
1、如何获取List集合的Stream流?
2、如何获取Set集合的Stream流?
3、如何获取Map集合的Stream流?
4、如何获取数组的Stream流?
对于map集合有三种方式:
1、用keySet方法获取key的set集合,根据set集合获取stream流
2、用values方法获取value的Collection集合,根据Collection集合获取stream流
3、获取键值对entry对象,根据得到的set集合获取stream流
示例代码:
// 1、如何获取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.6);
map.put("马尔扎哈", 166.3);
map.put("卡尔扎巴", 168.3);
//第一种方式:用keySet方法获取key的set集合,根据set集合获取stream流
Set<String> s = map.keySet();
Stream<String> stream2 = s.stream();
// Stream<String> stream2 = map.keySet().stream();//该种方式等同于上述2行代码
//第二种方式:用values方法获取value的Collection集合,根据Collection集合获取stream流
Collection<Double> values = map.values();
Stream<Double> stream3 = values.stream();
// Stream<Double> stream3 = map.values().stream();//该种方式等同于上述2行代码
//第三种方式:获取键值对entry对象,根据得到的set集合获取stream流
Set<Map.Entry<String, Double>> entries = map.entrySet();
Stream<Map.Entry<String, Double>> stream4 = entries.stream();
// Stream<Map.Entry<String, Double>> stream4 = map.entrySet().stream();//该种方式等同于上述2行代码
stream4.filter(e -> e.getKey().contains("巴") && e.getValue() > 168.4).forEach( stringDoubleEntry->
System.out.println(stringDoubleEntry.getKey()+"--->"+stringDoubleEntry.getValue()));
// 4、如何获取数组的Stream流?
String[] names2 = {"张翠山", "东方不败", "唐大山", "独孤求败"};
Stream<String> stream5 = Arrays.stream(names2);
Stream<String> stream6 = Stream.of(names2);
stream流的常见中间方法
List<Double> scores = new ArrayList<>();
Collections.addAll(scores, 88.5, 100.0, 60.0, 99.0, 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.5);
student s2 = new student("蜘蛛精", 26, 172.5);
student s3 = new student("紫霞", 23, 167.6);
student s4 = new student("白晶晶", 25, 169.0);
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(new Comparator<student>() {
// @Override
// public int compare(student o1, student o2) {
// return -(o1.getAge()- o2.getAge());
// }
// }).forEach(s-> System.out.println(s));
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(new Comparator<student>() {
// @Override
// public int compare(student o1, student o2) {
// return -Double.compare(o1.getHeight(), o2.getHeight());
// }
// }).limit(3).forEach(s-> System.out.println(s));
students.stream().sorted(( o1, o2) ->
-Double.compare(o1.getHeight(), o2.getHeight())
).limit(3).forEach(s-> System.out.println(s));
// 需求4:取出身高倒数的2名学生,并输出。 s1 s2 s3 s4 s5 s6
students.stream().sorted(( o1, o2)-> -(Double.compare(o1.getHeight(),o2.getHeight())))
.skip(students.size()-2).forEach(s-> System.out.println(s));
// 需求5:找出身高超过168的学生叫什么名字,要求去除重复的名字,再输出。
//.map方法是映射关系,相当于数据库的投影
// students.stream().filter(s->s.getHeight()>168).map(s->s.getName())
// .distinct().forEach(s-> System.out.println(s));
students.stream().filter(s->s.getHeight()>168).map(s->s.getName())
.distinct().forEach(System.out::println);//静态方法引用
// distinct去重复,自定义类型的对象(希望内容一样就认为重复,重写hashCode,equals)
students.stream().distinct().forEach(System.out::println);
//合并两个流,concat方法
Stream<String> st1 = Stream.of("张三", "李四");
Stream<String> st2 = Stream.of("张三2", "李四2", "王五");
Stream<String> allSt = Stream.concat(st1, st2);
allSt.forEach(System.out::println);
stream流的终结方法
注意:流只能收集一次
List<student> students = new ArrayList<>();
student s1 = new student("蜘蛛精", 26, 172.5);
student s2 = new student("蜘蛛精", 26, 172.5);
student s3 = new student("紫霞", 23, 167.6);
student s4 = new student("白晶晶", 25, 169.0);
student s5 = new student("牛魔王", 35, 183.3);
student s6 = new student("牛夫人", 34, 168.5);
Collections.addAll(students, s1, s2, s3, s4, s5, s6);
// 需求1:请计算出身高超过168的学生有几人。
long count = students.stream().filter(s -> s.getHeight() > 168).count();
System.out.println(count);
// 需求2:请找出身高最高的学生对象,并输出。
students.stream().max(( o1, o2)->
Double.compare(o1.getHeight(), o2.getHeight())
).get();
// 需求3:请找出身高最矮的学生对象,并输出。
students.stream().min(( o1, o2)->
Double.compare(o1.getHeight(), o2.getHeight())
).get();
// 需求4:请找出身高超过170的学生对象,并放到一个新集合中去返回。
List<student> collect = students.stream().filter(s -> s.getHeight() > 170).collect(Collectors.toList());
// 需求5:请找出身高超过170的学生对象,并把学生对象的名字和身高,存入到一个Map集合返回。
Map<String, Double> collect1 = students.stream().filter(s -> s.getHeight() > 170)
.distinct()
.collect(Collectors.toMap(a -> a.getName(), a -> a.getHeight()));
//转换为数组
Object[] arr = students.stream().filter(a -> a.getHeight() > 170).toArray();