日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java Stream中的API你都用过了吗?

發布時間:2023/11/23 java 39 coder
生活随笔 收集整理的這篇文章主要介紹了 Java Stream中的API你都用过了吗? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

公眾號「架構成長指南」,專注于生產實踐、云原生、分布式系統、大數據技術分享。

在本教程中,您將通過大量示例來學習 Java 8 Stream API。

Java 在 Java 8 中提供了一個新的附加包,稱為 java.util.stream。該包由類、接口和枚舉組成,允許對元素進行函數式操作。 您可以通過在程序中導入 java.util.stream包來使用流。

Stream提供以下功能:

Stream不存儲元素。它只是通過計算操作的管道傳送來自數據結構、數組或 I/O 通道等源的元素。

Stream本質上是函數式的,對流執行的操作不會修改其源。例如,過濾從集合獲取的 Stream 會生成一個沒有過濾元素的新 Stream,而不是從源集合中刪除元素。

Stream是惰性的,僅在需要時才計算代碼,在流的生命周期中,流的元素僅被訪問一次。

與迭代器一樣,必須生成新流才能重新訪問源中的相同元素。
您可以使用 Stream 來 過濾、收集、打印以及 從一種數據結構轉換為其他數據結構等。

Stream API 示例

1. 創建一個空的Stream

在創建空流時,應使用 empty() 方法:

Stream<String> stream = Stream.empty();
stream.forEach(System.out::println);

通常情況下,在創建時會使用 empty() 方法,以避免在沒有元素的流中返回 null:

public Stream<String> streamOf(List<String> list) {
    return list == null || list.isEmpty() ? Stream.empty() : list.stream();
}

2.從集合中創建流

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;

public class StreamCreationExamples {
    public static void main(String[] args) throws IOException {

        Collection<String> collection = Arrays.asList("JAVA", "J2EE", "Spring", "Hibernate");
        Stream<String> stream2 = collection.stream();
        stream2.forEach(System.out::println);

        List<String> list = Arrays.asList("JAVA", "J2EE", "Spring", "Hibernate");
        Stream<String> stream3 = list.stream();
        stream3.forEach(System.out::println);

        Set<String> set = new HashSet<>(list);
        Stream<String> stream4 = set.stream();
        stream4.forEach(System.out::println);
    }
}

輸出

JAVA
J2EE
Spring
Hibernate
JAVA
J2EE
Spring
Hibernate
JAVA
Hibernate
J2EE
Spring

3. 從數組中創建流對象

數組可以是流的源,也可以從現有數組或數組的一部分創建數組:

import java.util.Arrays;
import java.util.stream.Stream;

public class StreamCreationExample {
    public static void main(String[] args) {
        // 使用Arrays.stream()創建流
        int[] numbers = {1, 2, 3, 4, 5};
        Stream<Integer> stream1 = Arrays.stream(numbers);
        System.out.println("Using Arrays.stream():");
        stream1.forEach(System.out::println);

        // 使用Stream.of()創建流
        String[] names = {"Alice", "Bob", "Charlie"};
        Stream<String> stream2 = Stream.of(names);
        System.out.println("Using Stream.of():");
        stream2.forEach(System.out::println);

        // 使用Stream.builder()創建流
        String[] colors = {"Red", "Green", "Blue"};
        Stream.Builder<String> builder = Stream.builder();
        for (String color : colors) {
            builder.add(color);
        }
        Stream<String> stream3 = builder.build();
        System.out.println("Using Stream.builder():");
        stream3.forEach(System.out::println);
    }
}

輸出

Using Arrays.stream():
1
2
3
4
5
Using Stream.of():
Alice
Bob
Charlie
Using Stream.builder():
Red
Green
Blue

4. 使用Stream過濾一個集合示例

在下面的示例中,我們不使用流過濾數據,看看代碼是什么樣的,同時我們在給出一個使用stream過濾的示例,對比一下

不使用Stream過濾一個集合示例

import java.util.ArrayList;
import java.util.List;

public class FilterWithoutStreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();
        numbers.add(10);
        numbers.add(20);
        numbers.add(30);
        numbers.add(40);
        numbers.add(50);

        List<Integer> filteredNumbers = new ArrayList<>();
        for (Integer number : numbers) {
            if (number > 30) {
                filteredNumbers.add(number);
            }
        }

        System.out.println("Filtered numbers (without Stream):");
        for (Integer number : filteredNumbers) {
            System.out.println(number);
        }
    }
}

輸出:

Filtered numbers (without Stream):
40
50

使用 Stream 過濾集合示例:

import java.util.ArrayList;
import java.util.List;

public class FilterWithStreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();
        numbers.add(10);
        numbers.add(20);
        numbers.add(30);
        numbers.add(40);
        numbers.add(50);

        List<Integer> filteredNumbers = numbers.stream()
                .filter(number -> number > 30)
                .toList();

        System.out.println("Filtered numbers (with Stream):");
        filteredNumbers.forEach(System.out::println);
    }
}

輸出:

Filtered numbers (with Stream):
40
50

前后我們對比一下,可以看到,使用 Stream 進行集合過濾可以更加簡潔和直觀,減少了手動迭代和添加元素的步驟。它提供了一種聲明式的編程風格,使代碼更易讀、可維護和可擴展。

5. 使用Stream過濾和遍歷集合

在下面的示例中,我們使用 filter() 方法進行過濾,使用 forEach() 方法對數據流進行迭代:

import java.util.ArrayList;
import java.util.List;

public class FilterAndIterateWithStreamExample {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>();
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");
        names.add("David");
        names.add("Eve");

        System.out.println("Filtered names starting with 'A':");
        names.stream()
                .filter(name -> name.startsWith("A"))
                .forEach(System.out::println);
    }
}

輸出

Filtered names starting with 'A':
Alice

在上述示例中,我們有一個字符串列表 names,其中包含了一些名字。
我們使用 Stream 進行過濾和迭代操作以查找以字母 "A" 開頭的名字。

6.使用Collectors方法求和

我們還可以使用Collectors計算數值之和。

在下面的示例中,我們使用Collectors類及其指定方法計算所有產品價格的總和。

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class SumByUsingCollectorsMethods {
    public static void main(String[] args) {
        List < Product > productsList = new ArrayList < Product > ();
        productsList.add(new Product(1, "HP Laptop", 25000f));
        productsList.add(new Product(2, "Dell Laptop", 30000f));
        productsList.add(new Product(3, "Lenevo Laptop", 28000f));
        productsList.add(new Product(4, "Sony Laptop", 28000f));
        productsList.add(new Product(5, "Apple Laptop", 90000f));
        
        double totalPrice3 = productsList.stream()
            .collect(Collectors.summingDouble(product -> product.getPrice()));
        System.out.println(totalPrice3);

    }
}

輸出

201000.0

7. 使用Stream查找年齡最大和最小的學生

假設有一個 Student 類具有 name 和 age 屬性。我們可以使用 Stream 來查找學生集合中年齡的最大和最小值,并打印出相應的學生信息。以下是一個示例:

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

public class StudentStreamExample {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        students.add(new Student("Alice", 20));
        students.add(new Student("Bob", 22));
        students.add(new Student("Charlie", 19));
        students.add(new Student("David", 21));

        // 查找年齡最大的學生
        Optional<Student> maxAgeStudent = students.stream()
                .max(Comparator.comparingInt(Student::getAge));

        // 查找年齡最小的學生
        Optional<Student> minAgeStudent = students.stream()
                .min(Comparator.comparingInt(Student::getAge));

        // 打印最大和最小年齡的學生信息
        System.out.println("Student with maximum age:");
        maxAgeStudent.ifPresent(System.out::println);

        System.out.println("Student with minimum age:");
        minAgeStudent.ifPresent(System.out::println);
    }
}

class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

輸出:

Student with maximum age:
Student{name='Bob', age=22}
Student with minimum age:
Student{name='Charlie', age=19}

8. 使用Stream轉換List為Map

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StudentStreamToMapExample {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        students.add(new Student("Alice", 20));
        students.add(new Student("Bob", 22));
        students.add(new Student("Charlie", 19));
        students.add(new Student("David", 21));

        // 將學生列表轉換為 Map,以姓名為鍵,學生對象為值
        Map<String, Student> studentMap = students.stream()
                .collect(Collectors.toMap(Student::getName, student -> student));

        // 打印學生 Map
        for (Map.Entry<String, Student> entry : studentMap.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}

class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

輸出

David: Student{name='David', age=21}
Bob: Student{name='Bob', age=22}
Charlie: Student{name='Charlie', age=19}
Alice: Student{name='Alice', age=20}

在上面示例中,我們使用Collectors.toMap() 方法將學生列表轉換為 Map。我們指定了鍵提取器 Student::getName,將學生的姓名作為鍵。對于值提取器,我們使用了一個匿名函數 student -> student,它返回學生對象本身作為值。

9. 使用Stream把List對象轉換為另一個List對象

假設我們有一個 Person 類,其中包含姓名和年齡屬性。我們可以使用 Stream 來將一個 List 對象轉換為另一個 List 對象,其中只包含人員的姓名。以下是一個示例:

  import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class ListTransformationExample {
    public static void main(String[] args) {
        List<Person> persons = new ArrayList<>();
        persons.add(new Person("Alice", 20));
        persons.add(new Person("Bob", 22));
        persons.add(new Person("Charlie", 19));

        // 將 Person 列表轉換為只包含姓名的 String 列表
        List<String> names = persons.stream()
                .map(Person::getName)
                .collect(Collectors.toList());

        // 打印轉換后的姓名列表
        System.out.println(names);
    }
}

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

輸出:

  [Alice, Bob, Charlie]

在上述示例中,我們有一個 Person 類,其中包含姓名和年齡屬性。我們創建了一個 persons 列表,并添加了幾個 Person 對象。

使用Stream,我們通過調用 map() 方法并傳入一個方法引用 Person::getName,最后,我們使用 collect() 方法和 Collectors.toList() 將轉換后的姓名收集到一個新的列表中。

API

https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html

總結

以上是生活随笔為你收集整理的Java Stream中的API你都用过了吗?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。