JAVA語言引入了一個流式Stream API,這個API對集合數據進行操作,類似于使用SQL執行的數據庫查詢,同樣可以使用Stream API并行執行操作。
Stream和Collection的區別
Collection:靜態的內存數據結構,強調的是數據。
Stream API:和集合相關的計算操作,強調的是計算。
Collection面向的是內存,存儲在內存中;Stream API面向的是CPU,通過CPU來計算。
Stream API的操作步驟
第一步:創建Stream
通過數據源(如集合、數組等)來獲取一個Stream對象。
第二步:中間操作
對數據源的數據進行處理,該操作會返回一個Stream對象,因此可以進行鏈式操作。
第三步:終止操作
執行終止操作時,才會真正執行中間操作,并返回一個計算完畢后的結果。
Stream API的重要特點
- Stream自己不會存儲元素,只能對元素進行計算.
- Stream不會改變數據對象,反而可能會返回一個持有結果的新Stream.
- Stream上的操作屬于延遲執行,只有等到用戶真正需要結果的時候才會執行.
- Stream一旦執行了終止操作,則就不能再調用其它中間操作或中止操作了.
順序流和并行流
????????在前面獲得Stream對象的方式,我們都稱之為“順序流",順序流對Stream元素的處理是單線程的,即一個一個元素進行處理,處理數據的效率較低,如果Stream流中的數據處理沒有順序要求,并且還希望可以并行處理Stream的元素,那么就可以使用"并行流"來實現,從而提高處理數據的效率。
一個普通Stream轉換為可以并行處理的stream非常簡單,只需要用調用Stream提供的parallel()方法進行轉換即可,這樣就可以并行的處理Stream的元素,不需要編寫任何多線程代碼就可以享受到并行處理帶來的執行效率的提升。
獲取Stream的方式?
通過Collection接口的stream()方法來獲取Stream對象
public class StreamAPITest01 {public static void main(String[] args) {List<Integer> list = new ArrayList<>();list.add(1);list.add(2);list.add(3);list.add(4);// 注意,這個流屬于順序流,本質是單線程的。數據量如果不是很多,采用這種方式。Stream<Integer> stream = list.stream();// java.util.stream.ReferencePipeline$Head@b4c966a// 通過Stream對象可以對集合中的元素進行計算。System.out.println(stream); // 輸出stream對象的內存地址// 這是一個并行流(底層自動啟動多線程,你不需要管,程序員不需要干涉)// 在計算的時候自動會啟動多線程去運算。// 什么時候用?如果數據量非常龐大。Stream<Integer> parallelStream = list.parallelStream();// java.util.stream.ReferencePipeline$Head@2f4d3709System.out.println(parallelStream);}
}
通過Arrays數組工具類的stream()方法
public class StreamAPITest02 {public static void main(String[] args) {String[] names = {"zhangsan", "lisi", "wangwu"};Stream<String> stream = Arrays.stream(names); // 上面集合對應的流對象System.out.println(stream);int[] nums = {1,2,3,4};IntStream stream1 = Arrays.stream(nums);System.out.println(stream1);long[] lnums = {1L,2L,3L};LongStream stream2 = Arrays.stream(lnums);System.out.println(stream2);}
}
使用Stream接口本身的of(可變長度參數)方法
public class StreamAPITest03 {public static void main(String[] args) {// java.util.stream.ReferencePipeline$Head@b4c966a// stream是一個順序流,單線程Stream<String> stream = Stream.of("abc", "def", "xyz");System.out.println(stream);System.out.println(stream.isParallel()); // falseStream<String> parallel = stream.parallel(); // 將stream對象變為并行流System.out.println(parallel);System.out.println(stream == parallel); // trueSystem.out.println(parallel.isParallel()); // true// java.util.stream.ReferencePipeline$Head@2f4d3709Stream<Integer> integerStream = Stream.of(1, 2, 3, 4);System.out.println(integerStream);}
}
Stream中間操作
Student類
public class Student implements Comparable<Student>{private String name;private int age;private String gender;@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +", gender='" + gender + '\'' +'}';}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 String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public Student(String name, int age, String gender) {this.name = name;this.age = age;this.gender = gender;}@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 && Objects.equals(name, student.name) && Objects.equals(gender, student.gender);}@Overridepublic int hashCode() {return Objects.hash(name, age, gender);}@Overridepublic int compareTo(Student o) {return this.getAge() - o.getAge();}
}
StudentService類
public class StudentService {public static List<Student> getStudents(){List<Student> studentList = new ArrayList<>();studentList.add(new Student("zhangsan", 20, "男"));studentList.add(new Student("lisi", 21, "女"));studentList.add(new Student("wangwu", 22, "男"));studentList.add(new Student("zhaoliu", 18, "女"));studentList.add(new Student("qianqi", 19, "女"));/*studentList.add(new Student("qianqi", 19, "男"));studentList.add(new Student("qianqi", 19, "男"));studentList.add(new Student("qianqi", 19, "男"));*/return studentList;}
}
filter(過濾)
public class StreamAPITest04 {public static void main(String[] args) {// 篩選出年齡大于20的學生對象// filter 屬于中間操作,過濾// forEach 屬于終止操作,遍歷// filter和forEach都是Stream接口中的方法。// 由于Stream支持鏈式調用,所以可以一致"."// 匿名內部類的方式StudentService.getStudents().stream().filter(new Predicate<Student>() {@Overridepublic boolean test(Student student) {return student.getAge() > 20;}}).forEach(new Consumer<Student>() { // 必須要有終止操作@Overridepublic void accept(Student student) {System.out.println(student);}});// Lambda表達式方式StudentService.getStudents().stream().filter(student -> student.getAge() > 20).forEach(System.out::println);// 篩選出字符串長度大于3的元素Stream<String> stream = Stream.of("zhangsan", "lisi", "wangwu", "abc");stream.filter(s -> s.length() > 3).forEach(System.out::println);// 篩選出學生名字長度大于4的學生StudentService.getStudents().stream().filter(student -> student.getName().length() > 4).forEach(System.out::println);}
}
map(映射)
public class StreamAPITest05 {public static void main(String[] args) {// 把字符串中的字母全部轉化為大寫// 匿名內部類的方式Stream.of("abc", "def", "xyz").map(new Function<String, String>() {@Overridepublic String apply(String s) {return s.toUpperCase();}}).forEach(System.out::println);// Lambda表達式方式Stream.of("abc", "def", "xyz").map(String::toUpperCase).forEach(System.out::println);// 獲得集合中所有學生的名字StudentService.getStudents().stream().map(Student::getName).forEach(System.out::println);System.out.println("==============");// 需求:獲得集合中性別為男的學生名字// 思路:先篩選,后映射// 先filter,然后mapStudentService.getStudents().stream().filter(student -> student.getGender().equals("男")).map(Student::getName).forEach(System.out::println);System.out.println("==================");// 將多個集合中的數據合并到一個流Stream當中// flatMap的方法作用是什么?將多個集合中的所有元素全部放到一個Stream中。List<Integer> list1 = new ArrayList<>();list1.add(1);list1.add(2);list1.add(3);List<Integer> list2 = new ArrayList<>();list1.add(4);list1.add(5);list1.add(6);Stream<List<Integer>> twoListStream = Stream.of(list1, list2);// 匿名內部類方式/*twoListStream.flatMap(new Function<List<Integer>, Stream<?>>() {@Overridepublic Stream<?> apply(List<Integer> integers) {return integers.stream();}}).forEach(System.out::println);*/// Lambda表達式方式twoListStream.flatMap(List<Integer>::stream).forEach(System.out::println);}
}
distinct(去重)
public class StreamAPITest06 {public static void main(String[] args) {// 除去重復的元素Stream.of(1,1,1,1,1,1,1,2).distinct().forEach(System.out::println);// 除去重復的學生(除重后輸出學生對象)// 去除重復記錄是基于 hashCode + equals方法的。記得重寫。StudentService.getStudents().stream().distinct().forEach(System.out::println);// 需求:除去年齡相同的學生(除重后輸出學生年齡)// 思路:先映射,后除重// 先把學生映射成對應的年齡,然后再去重StudentService.getStudents().stream().map(Student::getAge).distinct().forEach(System.out::println);}
}
sorted(排序)
public class StreamAPITest07 {public static void main(String[] args) {// 需求:對元素執行“升序”排序Stream.of(1,2,3,4,100,0,-1).sorted().forEach(System.out::println);Stream.of("ccc", "bbb", "abc", "aaa").sorted().forEach(System.out::println); // aaa, abc, bbb, ccc// 需求:按照學生的年齡執行“升序”排序(排序后輸出學生對象)// 注意:這里的排序是對學生對象進行排序,排序規則需要指定,Student實現java.lang.Comparable接口。StudentService.getStudents().stream().sorted().forEach(System.out::println);// 需求:按照學生的年齡執行“升序”排序(排序后輸出學生年齡)// 先映射,再排序StudentService.getStudents().stream().map(Student::getAge).sorted().forEach(System.out::println);System.out.println("==================");// 需求:對元素執行“升序”排序Stream.of(10, 20, 30, 18, 15).sorted(Integer::compareTo).forEach(System.out::println);// 需求:按照學生的年齡執行“降序”排序(排序后輸出學生對象)StudentService.getStudents().stream().sorted((o1,o2) -> o2.getAge() - o1.getAge()).forEach(System.out::println);// 需求:按照學生的年齡執行“升序”排序(排序后輸出學生年齡)// 先映射,再排序StudentService.getStudents().stream().map(Student::getAge).sorted().forEach(System.out::println);StudentService.getStudents().stream().map(Student::getAge).sorted(Integer::compareTo).forEach(System.out::println);}
}
concat(合并)
public class StreamAPITest08 {public static void main(String[] args) {Stream<Integer> stream1 = Stream.of(1, 2, 3);Stream<Integer> stream2 = Stream.of(4, 5, 6);Stream.concat(stream1, stream2).forEach(System.out::println);}
}
skip+limit(截取集合的一部分)
public class StreamAPITest09 {public static void main(String[] args) {Stream.of(1,2,3,4,5,6,7,8,9,10).skip(3).limit(3).forEach(System.out::println); // 4 5 6}
}
match
public class StreamAPITest10 {public static void main(String[] args) {// 匹配集合中元素是否都是3// allMatch 匹配所有System.out.println(Stream.of(1, 2, 3, 4).allMatch(value -> value.equals(3))); // falseSystem.out.println(Stream.of(3, 3, 3, 3).allMatch(value -> value.equals(3))); // true// 匿名內部類System.out.println(Stream.of(3, 3, 3, 3).allMatch(new Predicate<Integer>() {@Overridepublic boolean test(Integer value) {return value.equals(3);}}));// 匹配集合中元素是否包含3// anyMatch 匹配其中一個System.out.println(Stream.of(1, 2, 3, 4).anyMatch(value -> value.equals(3))); // true// 匹配集合中元素有沒有3// noneMatch 匹配不上System.out.println(Stream.of(1, 2, 3, 4).noneMatch(value -> value.equals(3))); // falseSystem.out.println(Stream.of(1, 2, 3, 4).noneMatch(value -> value.equals(100))); // true// 獲取流中第一個元素Optional<Integer> firstOptional = Stream.of(1, 2, 3, 4).findFirst();System.out.println(firstOptional.get());System.out.println(Stream.of(1, 2, 3, 4).findFirst().get());// 需求:匹配學生名字是否都為“zhangsan”System.out.println(StudentService.getStudents().stream().allMatch(student -> student.getName().equals("zhangsan"))); // false// 需求:匹配學生名字是否至少有一個為“zhangsan”System.out.println(StudentService.getStudents().stream().anyMatch(student -> student.getName().equals("zhangsan"))); // true// 需求:匹配學生名字中是否全部都沒有“lucy”System.out.println(StudentService.getStudents().stream().noneMatch(student -> student.getName().equals("lucy"))); // true// 需求:獲得第一個學生System.out.println(StudentService.getStudents().stream().findFirst().get());Optional<Student> first = StudentService.getStudents().stream().findFirst();/*if (first.isPresent()) {System.out.println(first.get());}*/first.ifPresent(System.out::println);// 需求:獲得第四個學生// 思路:跳過前面3個學生,然后再獲得第一個元素System.out.println(StudentService.getStudents().stream().skip(3).findFirst().get());}
}
Stream終止操作(reduce)
作用:將流中的所有數據,按照指定的規則,最終計算出一個結果。
其中預定義的函數式接口是 BinaryOperator
public class StreamAPITest11 {public static void main(String[] args) {// 將集合中的所有數字求和 累加器,初始值為0,也就是當前代碼中的 xSystem.out.println(Stream.of(1, 2, 3, 4).reduce((x, y) -> x + y).get()); // 10// 使用數學工具類(方法引用)System.out.println(Math.addExact(10, 20)); // 30System.out.println(Stream.of(1, 2, 3, 4).reduce((x, y) -> Math.addExact(x, y)).get()); // 10System.out.println(Stream.of(1, 2, 3, 4).reduce(Math::addExact).get());// 需求:獲得集合中所有元素“相乘”的結果System.out.println(Stream.of(1, 2, 3, 4).reduce(Math::multiplyExact).get());// 需求:獲得最大長度的元素System.out.println(Stream.of("abc", "def", "hello", "helloworld").reduce((s1, s2) -> s1.length() > s2.length() ? s1 : s2).get());// 需求:獲得所有學生的總年齡System.out.println(StudentService.getStudents().stream().map(Student::getAge).reduce(Math::addExact).get());// 需求:獲得10和集合中所有元素“相加”的結果System.out.println(Stream.of(1, 2, 3, 4).reduce(10, Math::addExact));}
}
count,max,min方法
public class StreamAPITest12 {public static void main(String[] args) {// 需求:獲得元素的個數System.out.println(StudentService.getStudents().stream().count());System.out.println(StudentService.getStudents().size());// 需求:獲得年齡“最大”的學生System.out.println(StudentService.getStudents().stream().max((s1, s2) -> s1.getAge() - s2.getAge()).get());// 需求:獲得學生的“最大”年齡System.out.println(StudentService.getStudents().stream().map(Student::getAge).max(Integer::compareTo).get());// 需求:獲得年齡“最小”的學生System.out.println(StudentService.getStudents().stream().min((s1, s2) -> s1.getAge() - s2.getAge()).get());// 需求:獲得學生的“最小”年齡System.out.println(StudentService.getStudents().stream().map(Student::getAge).min(Integer::compareTo).get());}
}
collect 收集
歸集:toList/toSet/toMap
public class StreamAPITest13 {public static void main(String[] args) {// 將流Stream中的數據全部收集到一個集合中。// 收集為List集合。(具體是哪種List集合,在這里是不知道的。)List<String> list = Stream.of("zhangsan", "lisi", "wangwu").collect(Collectors.toList()); // collect傳入的參數是收集器System.out.println(list);// 收集為Set集合。(具體是哪種Set集合,在這里是不知道的。)Set<String> set = Stream.of("zhangsan", "lisi", "wangwu", "wangwu").collect(Collectors.toSet());System.out.println(set);// 收集為Map集合。// 匿名內部類的方式/*Map<String, String> map = Stream.of("1:zhangsan", "2:lisi", "3:wangwu").collect(Collectors.toMap(new Function<String, String>() {@Overridepublic String apply(String s) {return s.substring(0, s.indexOf(":"));}}, new Function<String, String>() {@Overridepublic String apply(String s) {return s.substring(s.indexOf(":") + 1);}}));*/// Lambda表達式方式Map<String, String> map = Stream.of("1:zhangsan", "2:lisi", "3:wangwu").collect(Collectors.toMap(s -> s.substring(0, s.indexOf(":")), s -> s.substring(s.indexOf(":") + 1)));// 遍歷Map集合map.forEach((k, v) -> System.out.println(k + "===>" + v));}
}
toCollection
以指定的集合類型來進行收集
public class StreamAPITest14 {public static void main(String[] args) {// 注意:ArrayList::new 是構造方法引用// 以ArrayList集合進行收集ArrayList<String> arrayList = Stream.of("zhangsan", "lisi", "wangwu", "wangwu").collect(Collectors.toCollection(ArrayList::new));System.out.println(arrayList);// 以LinkedList集合進行收集LinkedList<String> linkedList = Stream.of("zhangsan", "lisi", "wangwu", "wangwu").collect(Collectors.toCollection(LinkedList::new));System.out.println(linkedList);// 以HashSet集合收集 無序,不可重復HashSet<Integer> hashSet = Stream.of(1, 1, 1, 1, 100, 300, 200, 50, 50).collect(Collectors.toCollection(HashSet::new));System.out.println(hashSet);// 以TreeSet集合收集TreeSet<Integer> treeSet = Stream.of(1, 1, 1, 1, 100, 300, 200, 50, 50).collect(Collectors.toCollection(TreeSet::new));System.out.println(treeSet);}
}
public class StreamAPITest15 {public static void main(String[] args) {ArrayList<Student> students = StudentService.getStudents().stream().filter(student -> student.getGender().equals("女")).filter(student -> student.getAge() > 18).sorted((s1, s2) -> s1.getAge() - s2.getAge()).collect(Collectors.toCollection(ArrayList::new));System.out.println(students);}
}
歸集:轉換為數組形式
public class StreamAPITest16 {public static void main(String[] args) {// 沒有指定數組的類型時,默認轉換成Object[]數組。Object[] array = Stream.of(1, 2, 3, 4).toArray();System.out.println(Arrays.toString(array));// 轉換為指定類型的數組Integer[] array1 = Stream.of(1, 2, 3, 4).toArray(Integer[]::new);System.out.println(Arrays.toString(array1));String[] array2 = Stream.of("a", "b", "c").toArray(String[]::new);System.out.println(Arrays.toString(array2)); // 字符串數組}
}
public class StreamAPITest17 {public static void main(String[] args) {// 需求:獲得元素的個數// 可以使用reducelong count = StudentService.getStudents().stream().count();System.out.println("學生總數:" + count);// 也可以使用collectLong count2 = StudentService.getStudents().stream().collect(Collectors.counting());System.out.println("學生總數:" + count2);// 需求:獲得學生的平均年齡Double avgAge = StudentService.getStudents().stream().collect(Collectors.averagingDouble(Student::getAge));System.out.println("學生平均年齡:" + avgAge);// 需求:獲得最大年齡的學生Student student = StudentService.getStudents().stream().collect(Collectors.maxBy((s1, s2) -> s1.getAge() - s2.getAge())).get();System.out.println("最大年齡的學生:" + student);// StudentService.getStudents().stream().max((s1, s2) -> s1.getAge() - s2.getAge()).get();
// System.out.println("最大年齡的學生02:" + student);// 需求:獲得所有學生年齡之和Integer ageSum = StudentService.getStudents().stream().collect(Collectors.summingInt(Student::getAge));System.out.println("所有學生年齡的和:" + ageSum);// 需求:獲得年齡的所有的信息DoubleSummaryStatistics collect = StudentService.getStudents().stream().collect(Collectors.summarizingDouble(Student::getAge));System.out.println(collect);System.out.println(collect.getAverage());System.out.println(collect.getMax());System.out.println(collect.getMin());System.out.println(collect.getSum());}
}
分組:groupingBy
public class StreamAPITest18 {public static void main(String[] args) {// 按照性別分組Map<String, List<Student>> map = StudentService.getStudents().stream().collect(Collectors.groupingBy(Student::getGender));map.forEach((k , v) -> System.out.println(k + "===>" + v));}
}
接合:joining
public class StreamAPITest19 {public static void main(String[] args) {// 需求:將所有學生的姓名連接成一個字符串,每個名字之間以“,”連接String s = StudentService.getStudents().stream().map(Student::getName).collect(Collectors.joining(","));System.out.println(s);// 通過 map(Student::getAge) 提取年齡屬性 通過 map(String::valueOf) 將整型年齡轉為字符串String s1 = StudentService.getStudents().stream().map(Student::getAge).map(String::valueOf).collect(Collectors.joining(","));System.out.println(s1);// 通過 map() 將每個學生對象轉換為字符串 再使用 Collectors.toList() 收集為字符串列表List<String> l1 = StudentService.getStudents().stream().map(student->student.getName() + "," + student.getAge()).collect(Collectors.toList());System.out.println(l1);}
}