vps建设网站,中国石油建设工程协会网站,如何做网站优化并快速提高权重,永久免费微信小程序商城目录
1.获取Stream流
2.Stream流常见的中间方法
3.Stream流常见的终结方法 1、 Stream 是什么#xff1f;有什么作用#xff1f;结合了什么技术#xff1f; ●也叫 Stream 流#xff0c;是Jdk8开始新增的一套 API ( java . util . stream .*)#xff0c;可以用于操作集…目录
1.获取Stream流
2.Stream流常见的中间方法
3.Stream流常见的终结方法 1、 Stream 是什么有什么作用结合了什么技术 ●也叫 Stream 流是Jdk8开始新增的一套 API ( java . util . stream .*)可以用于操作集合或者数组的数据。.
优势 Stream 流大量的结合了 Lambda 的语法风格来编程提供了一种更加强大更加简单的方式操作集合或者数组中的数据代码更简洁可读性更好。 2、说说 Stream 流处理数据的步骤是什么 package com.xinbao.d8_stream;import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;public class StreamTest1 {public static void main(String[] args) {ListString names new ArrayList();Collections.addAll(names,张三丰, 张无忌, 周芷若, 赵敏, 张强);System.out.println(names);//找出姓张且是三个字的名字存到新集合中去ListString list new ArrayList();for (String name : names) {if (name.startsWith(张) name.length() 3){list.add(name);}}System.out.println(list);//使用Stream流解决问题ListString list2 names.stream().filter(s - s.startsWith(张)).filter(a - a.length() 3).collect(Collectors.toList());System.out.println(list2);}
}
E:\JVsoft\Java\jdk-17\bin\java.exe -javaagent:E:\JVsoft\IntelliJIDEA2021.1.1\lib\idea_rt.jar4519:E:\JVsoft\IntelliJIDEA2021.1.1\bin -Dfile.encodingUTF-8 -classpath E:\JVsoft\code\out\production\collection-map-stream-app com.xinbao.d8_stream.StreamTest1
[张三丰, 张无忌, 周芷若, 赵敏, 张强]
[张三丰, 张无忌]
[张三丰, 张无忌]1.获取Stream流 package com.xinbao.d8_stream;import java.util.*;
import java.util.stream.Stream;public class StreamTest2 {public static void main(String[] args) {//1.如何获取List集合的Stream流ListString names new ArrayList();Collections.addAll(names,张三丰, 张无忌, 周芷若, 赵敏, 张强);StreamString stream names.stream();//2.如何获取Set集合的Stream流SetString set new HashSet();Collections.addAll(set,刘德华, 张曼玉, 张国荣, 刘嘉玲, 王祖贤);StreamString stream1 set.stream();//alt enterstream1.filter(s - s.contains(张)).forEach(s - System.out.println(s));//3.如何获取Map集合的Stream流MapString,Double map new HashMap();map.put(古力娜扎, 167.5);map.put(迪丽热巴, 164.5);map.put(马尔扎哈, 187.7);map.put(哈尼克孜, 163.8);SetString keys map.keySet();StreamString ks keys.stream();CollectionDouble values map.values();StreamDouble vs values.stream();SetMap.EntryString, Double entries map.entrySet();StreamMap.EntryString, Double kvs entries.stream();kvs.filter(s - s.getKey().contains(哈)).forEach(s - System.out.println(s.getKey() -- s.getValue()));//4.如何获取数组的Stream流String[] names2 {刘德华, 张曼玉, 张国荣, 刘嘉玲, 王祖贤};StreamString stream2 Arrays.stream(names2);StreamString names21 Stream.of(names2);}
}E:\JVsoft\Java\jdk-17\bin\java.exe -javaagent:E:\JVsoft\IntelliJIDEA2021.1.1\lib\idea_rt.jar39123:E:\JVsoft\IntelliJIDEA2021.1.1\bin -Dfile.encodingUTF-8 -classpath E:\JVsoft\code\out\production\collection-map-stream-app com.xinbao.d8_stream.StreamTest2
张国荣
张曼玉
马尔扎哈--187.7
哈尼克孜--163.82.Stream流常见的中间方法 package com.xinbao.d6_map_impl;import java.util.Objects;public class Student implements ComparableStudent{private String name;private int age;private double height;//this oOverride
// public int compareTo(Student o) {
// return this.age - o.age;//年龄升序
// }public int compareTo(Student o) {return o.age - this.age;}public Student() {}Overridepublic boolean equals(Object o) {if (this o) return true;if (o null || getClass() ! o.getClass()) return false;Student student (Student) o;return age student.age Double.compare(student.height, height) 0 Objects.equals(name, student.name);}Overridepublic int hashCode() {return Objects.hash(name, age, height);}Overridepublic String toString() {return Student{ name name \ , age age , height height };}public Student(String name, int age, double height) {this.name name;this.age age;this.height height;}public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}public void setAge(int age) {this.age age;}public double getHeight() {return height;}public void setHeight(double height) {this.height height;}
}package com.xinbao.d8_stream;import com.xinbao.d6_map_impl.Student;import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;public class StreamTest3 {public static void main(String[] args) {ListDouble scores new ArrayList();Collections.addAll(scores,88.5,22.5,88.7,56.9,24.8,46.7,96.3);//需求1找出成绩大于60的数据排序输出scores.stream().filter(s - s60).sorted().forEach(s - System.out.println(s));ListStudent students new ArrayList();students.add(new Student(蜘蛛精, 25, 168.5));students.add(new Student(蜘蛛精, 25, 168.5));students.add(new Student(至尊宝, 23, 163.5));students.add(new Student(牛魔王, 28, 183.5));System.out.println(students);//需求2找出年龄大于23小于30的学生升序输出students.stream().filter(s - s.getAge() 23 s.getAge() 30).sorted((o1,o2) - o1.getAge() - o2.getAge()).forEach(System.out::println);//需求3取出身高最高的前三名学生并输出System.out.println(-------------------------------------------------------------);students.stream().sorted((o1, o2) - Double.compare(o2.getHeight(),o1.getHeight())).limit(3).forEach(System.out::println);//需求4取出身高倒数的两名学生输出System.out.println(-------------------------------------------------------------);students.stream().sorted((o1,o2) - Double.compare(o2.getHeight(),o1.getHeight())).skip(students.size() - 2).forEach(System.out::println);//需求5找出身高超过168的学生名字去重再输出System.out.println(--------------------------------------------------------------);students.stream().filter(s - s.getHeight()168).map(Student::getName)//.map(s-s.getName) 映射.distinct().forEach(System.out::println);//distinct去重复自定义类型的对象希望内容一样就认为重复重写hashCode,equalsstudents.stream().filter(s - s.getHeight()168).distinct().forEach(System.out::println);StreamString ls Stream.of(李四, 王五, 刘六六);StreamString zs Stream.of(张三);StreamString concat Stream.concat(ls, zs);concat.forEach(System.out::println);}
}E:\JVsoft\Java\jdk-17\bin\java.exe -javaagent:E:\JVsoft\IntelliJIDEA2021.1.1\lib\idea_rt.jar26802:E:\JVsoft\IntelliJIDEA2021.1.1\bin -Dfile.encodingUTF-8 -classpath E:\JVsoft\code\out\production\collection-map-stream-app com.xinbao.d8_stream.StreamTest3
88.5
88.7
96.3
[Student{name蜘蛛精, age25, height168.5}, Student{name蜘蛛精, age25, height168.5}, Student{name至尊宝, age23, height163.5}, Student{name牛魔王, age28, height183.5}]
Student{name蜘蛛精, age25, height168.5}
Student{name蜘蛛精, age25, height168.5}
Student{name牛魔王, age28, height183.5}
-------------------------------------------------------------
Student{name牛魔王, age28, height183.5}
Student{name蜘蛛精, age25, height168.5}
Student{name蜘蛛精, age25, height168.5}
-------------------------------------------------------------
Student{name蜘蛛精, age25, height168.5}
Student{name至尊宝, age23, height163.5}
--------------------------------------------------------------
蜘蛛精
牛魔王
Student{name蜘蛛精, age25, height168.5}
Student{name牛魔王, age28, height183.5}
李四
王五
刘六六
张三3.Stream流常见的终结方法 package com.xinbao.d8_stream;import com.xinbao.d6_map_impl.Student;import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;public class StreamTest4 {public static void main(String[] args) {ListStudent students new ArrayList();Student s1 new Student(蜘蛛精, 25, 168.5);Student s2 new Student(蜘蛛精, 25, 168.5);Student s3 new Student(至尊宝, 23, 163.5);Student s4 new Student(牛魔王, 28, 183.5);Student s5 new Student(狐狸精, 23, 161.5);Student s6 new Student(白骨精, 25, 161.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找出最高学生对象并输出Student max students.stream().max((o1, o2) - Double.compare(o1.getHeight(), o2.getHeight())).get();System.out.println(max);OptionalStudent max1 students.stream().max((o1, o2) - Double.compare(o1.getHeight(), o2.getHeight()));System.out.println(max1);//需求3找出最矮学生对象并输出Student min students.stream().min((o1, o2) - Double.compare(o1.getHeight(), o2.getHeight())).get();System.out.println(min);//需求4找出高于165的学生对象放到新集合中返回ListStudent collect students.stream().filter(s - s.getHeight() 165).collect(Collectors.toList());System.out.println(collect);SetStudent collect2 students.stream().filter(s - s.getHeight() 165).collect(Collectors.toSet());System.out.println(collect2);//去重//流只能收集一次类比迭代器
// StreamStudent str students.stream().filter(s - s.getHeight() 165);
// ListStudent list str.collect(Collectors.toList());
// System.out.println(list);
// SetStudent set str.collect(Collectors.toSet());//报错流已经终结
// System.out.println(set);//需求五找出高于165的学生对象并把对象的名字和身高存入到一个Map集合返回MapString,Double map students.stream().filter(s - s.getHeight()165)//不会自动去重需要.distinct()去重.distinct().collect(Collectors.toMap(a - a.getName(), a - a.getHeight()));System.out.println(map);Object[] objects students.stream().filter(s - s.getHeight() 165).toArray();System.out.println(Arrays.toString(objects));Student[] students1 students.stream().filter(s - s.getHeight() 165).toArray(len - new Student[len]);System.out.println(Arrays.toString(students1));}
}
E:\JVsoft\Java\jdk-17\bin\java.exe -javaagent:E:\JVsoft\IntelliJIDEA2021.1.1\lib\idea_rt.jar27234:E:\JVsoft\IntelliJIDEA2021.1.1\bin -Dfile.encodingUTF-8 -classpath E:\JVsoft\code\out\production\collection-map-stream-app com.xinbao.d8_stream.StreamTest4
3
Student{name牛魔王, age28, height183.5}
Optional[Student{name牛魔王, age28, height183.5}]
Student{name狐狸精, age23, height161.5}
[Student{name蜘蛛精, age25, height168.5}, Student{name蜘蛛精, age25, height168.5}, Student{name牛魔王, age28, height183.5}]
[Student{name牛魔王, age28, height183.5}, Student{name蜘蛛精, age25, height168.5}]
{蜘蛛精168.5, 牛魔王183.5}
[Student{name蜘蛛精, age25, height168.5}, Student{name蜘蛛精, age25, height168.5}, Student{name牛魔王, age28, height183.5}]
[Student{name蜘蛛精, age25, height168.5}, Student{name蜘蛛精, age25, height168.5}, Student{name牛魔王, age28, height183.5}]进程已结束退出代码为 0