2021-06-08 java8 Stream 工作中一些可能用到的總結

package com.example.jsch_smb;

import cn.hutool.core.collection.CollUtil;

import cn.hutool.core.map.MapUtil;

import java.util.*;

import java.util.stream.Collectors;

import java.util.stream.Stream;

public class Java8StreamDemo {

//并行流 parallelStream forEachOrdered

? ? private void parallelStream() {

List nums = CollUtil.newArrayList(1, 2, 3, 4, 5);

? ? ? ? //順序打印

? ? ? ? nums.stream().forEach(System.out::print);

? ? ? ? nums.stream().forEachOrdered(System.out::print);

? ? ? ? //并行流

? ? ? ? nums.parallelStream().forEach(System.out::print);

? ? ? ? nums.parallelStream().forEachOrdered(System.out::print);

? ? }

//流連接 flatMap

? ? private void flatMap() {

List list = Arrays.asList("a,b,c", "1,2,3");

? ? ? ? List newList = list.stream().flatMap(s -> {

//將每個元素轉換成一個stream

? ? ? ? ? ? String[] split = s.split(",");

? ? ? ? ? ? Stream s2 = Arrays.stream(split);

? ? ? ? ? ? return s2;

? ? ? ? }).collect(Collectors.toList());

? ? ? ? newList.forEach(System.out::println); // a b c 1 2 3

? ? }

//分組排序 Comparator.comparing Comparator.naturalOrder Collectors.partitioningBy

? ? private void groupAndSort() {

Student s1 =new Student("aa", 10);

? ? ? ? Student s2 =new Student("bb", 20);

? ? ? ? Student s3 =new Student("aa",30);

? ? ? ? Student s4 =new Student("dd", 40);

? ? ? ? List students = CollUtil.newArrayList(s1, s2, s3, s4);

? ? ? ? //二次排序

? ? ? ? students.stream().sorted(

Comparator.comparing(Student::getName,Comparator.naturalOrder())

.thenComparing(Student::getAge,Comparator.reverseOrder())).collect(Collectors.toList());

? ? ? ? //一次分組 分成兩部分,一部分大于10歲,一部分小于等于10歲

? ? ? ? Map>> collect2 = students.stream().collect(

Collectors.partitioningBy(e -> e.getAge() >10, Collectors.groupingBy(Student::getName)));

? ? ? ? //二次分組? 先第一個提交 在第二個條件

? ? ? ? Map>> collect3 = students.stream().collect(

Collectors.partitioningBy(e -> e.getAge() >10, Collectors.groupingBy(k -> {

if (k.getAge() ==30) {

return "equal";

? ? ? ? ? ? ? ? ? ? }else {

return "unequal";

? ? ? ? ? ? ? ? ? ? }

})));

? ? ? ? //分組統(tǒng)計

? ? ? ? LinkedHashMap collect = students.stream().collect(

Collectors.groupingBy(Student::getName, LinkedHashMap::new, Collectors.counting()));

? ? ? ? //分組取最大值

? ? ? ? Map collect1 = students.stream().collect(

Collectors.groupingBy(Student::getName, Collectors.collectingAndThen(

Collectors.maxBy(Comparator.comparingInt(Student::getAge)), Optional::get)));

? ? }

//匹配,聚合

? ? private void match() {

List list = Arrays.asList(1, 2, 3, 4, 5);

? ? ? ? boolean allMatch = list.stream().allMatch(e -> e >10); //false

? ? ? ? boolean noneMatch = list.stream().noneMatch(e -> e >10); //true

? ? ? ? boolean anyMatch = list.stream().anyMatch(e -> e >4); //true

? ? ? ? Integer findFirst = list.stream().findFirst().get(); //1

? ? ? ? Integer findAny = list.stream().findAny().get(); //1

? ? ? ? long count = list.stream().count(); //5

? ? ? ? Integer max = list.stream().max(Integer::compareTo).get(); //5

? ? ? ? Integer min = list.stream().min(Integer::compareTo).get(); //1

? ? }

//積累

? ? private void experience() {

Student s1 =new Student("aa", 10);

? ? ? ? Student s2 =new Student("bb", 20);

? ? ? ? Student s3 =new Student("aa", 30);

? ? ? ? Student s4 =new Student("dd", 40);

? ? ? ? List students = CollUtil.newArrayList(s1, s2, s3, s4);

? ? ? ? // 1. 取max 值

//List

? ? ? ? IntSummaryStatistics collect = CollUtil.newArrayList(1, 2, 3, 4, 5).stream().collect(Collectors.summarizingInt(Integer::intValue));

? ? ? ? //IntSummaryStatistics{count=5, sum=15, min=1, average=3.000000, max=5}

// List

? ? ? ? students.stream().collect(Collectors.summarizingInt(Student::getAge)).getMax();

? ? ? ? // List

? ? ? ? List maps = CollUtil.newArrayList();

? ? ? ? students.parallelStream().forEach(e -> {

maps.add(MapUtil.of(e.getName(), e.getAge()));

? ? ? ? });

? ? ? ? maps.stream().mapToInt(

m -> m.get("age") !=null ? Integer.parseInt(m.get("age").toString()) :0

? ? ? ? ).max().getAsInt();

? ? ? ? // 2. 取總值 等聚合值 sum average ...

// List<對象>

? ? ? ? students.stream().mapToInt(n -> n.getAge()).summaryStatistics().getSum();

? ? ? ? // List

? ? ? ? int sum = maps.stream().mapToInt(

m -> m.get("age") !=null ? Integer.parseInt(m.get("age").toString()) :0

? ? ? ? ).sum();

? ? }

public static void main(String[] args) {

Java8StreamDemo demo =new Java8StreamDemo();

? ? ? ? demo.groupAndSort();

? ? }

static class Student {

private Stringname;

? ? ? ? private int age;

? ? ? ? public Student(String name, int age) {

this.name = name;

? ? ? ? ? ? this.age = age;

? ? ? ? }

public StringgetName() {

return name;

? ? ? ? }

public void setName(String name) {

this.name = name;

? ? ? ? }

public int getAge() {

return age;

? ? ? ? }

public void setAge(int age) {

this.age = age;

? ? ? ? }

}

}

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 一、概述 Stream 是 Java8 中處理集合的關鍵抽象概念,它可以指定你希望對集合進行的操作,可以執(zhí)行非常復...
    叢林胖虎閱讀 389評論 0 0
  • 因為學習了Lambda表達式,再學習Java8新特性之Stream的API常用函數(shù)使用及說明,兩者結合起來,代碼就...
    安仔夏天勤奮閱讀 852評論 0 0
  • JAVA8新特性 速度更快 Lambda表達式 Stream API 便于并行 最大化的減少空指針異常 速度更快 ...
    Levi_wen閱讀 190評論 0 0
  • 表情是什么,我認為表情就是表現(xiàn)出來的情緒。表情可以傳達很多信息。高興了當然就笑了,難過就哭了。兩者是相互影響密不可...
    Persistenc_6aea閱讀 126,098評論 2 7
  • 16宿命:用概率思維提高你的勝算 以前的我是風險厭惡者,不喜歡去冒險,但是人生放棄了冒險,也就放棄了無數(shù)的可能。 ...
    yichen大刀閱讀 6,112評論 0 4