Table of Contents
1. Why experienced veterans prefer to use Stream
2. How to use Stream
3. Creation of Stream
Home Java javaTutorial How to use Stream in Java8

How to use Stream in Java8

May 30, 2023 pm 02:22 PM
java stream

1. Why experienced veterans prefer to use Stream

  • Performance advantages, (large data volume) compared to iterators, it is faster

  • Supports serial and parallel processing, Parallel processing can make full use of CPU resources

  • Stream is a stream of calculation data. It does not store data itself

  • Supports functional programming

  • The code is elegant, making the code more efficient, clean and concise

2. How to use Stream

Three steps:

  • CreateStream

  • Intermediate operation

  • Termination operation

3. Creation of Stream

# The creation of ##Stream will depend on the data source, usually a container or an array Stream The creation of a stream is roughly divided into 4 types, the most commonly used is to create it through a collection

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class CreateStreamDemo {
    public static void main(String[] args) {
        // 1 通过集合创建Stream也是用的最多的一种形式
        List<String> strList = new ArrayList<>();
        strList.add("a");
        strList.add("b");
        strList.add("c");
        // 创建串行操作流
        Stream<String> stream = strList.stream();
        // 创建并行操作流
        Stream<String> parallelStream = strList.parallelStream();
        // 2 通过数组创建Stream
        int[] arr = new int[]{1,2,3};
        IntStream intStream = Arrays.stream(arr);
        // 3 通过Stream.of
        Stream<Integer> integerStream = Stream.of(1,2,3);
        Stream<String> stringStream = Stream.of("a","b","c");
        // 4 无限流
        // 每隔五个数取一个
        Stream.iterate(0, t -> t + 5).forEach(System.out::println); // 迭代
        Stream.generate(Math::random).forEach(System.out::println); // 生成
    }
}

4 . Stream intermediate operations

Stream Intermediate operations, our most commonly used ones are filtering, deduplication, and sorting. This chapter includes the most commonly used deduplication of objects and updating of objects in our development. Sorting attribute combinations

import com.zhj.java8.bean.Student;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Stream;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

public class MiddleStreamDemo {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        students.add(new Student(1,"小华",23,1));
        students.add(new Student(1,"小华",23,2));
        students.add(new Student(2,"小米",20,2));
        students.add(new Student(3,"小果",30,3));
        students.add(new Student(4,"小维",18,2));
        // 过滤
        students.stream().filter(stu -> stu.getAge() > 20).forEach(System.out::println);
        // 去重
        // 对对象去重是根据引用去重,内容重复并不会去重,除非重写equals和hashCode方法
        System.out.println("----------去重----------");
        System.out.println("去重1----------");
        students.stream().distinct().forEach(System.out::println);
        // 对集合中对象某些属性去重,不重写equals和hashCode方法,只能借助其他数据结构来辅助去重
        // 单个属性可以stu -> stu.getId()
        // 多个属性可以stu -> stu.getId() + ";" + stu.getName()
        System.out.println("去重2----------");
        ArrayList<Student> distinctList = students.stream().collect(
                collectingAndThen(toCollection(() -> new TreeSet<>(Comparator.comparing(stu -> stu.getId() + ";" + stu.getName()))), ArrayList::new)
        );
        distinctList.stream().forEach(System.out::println);
        // 排序 支持定义排序方式
        // sorted 默认使用 自然序排序, 其中的元素必须实现Comparable 接口
        System.out.println("----------排序----------");
        System.out.println("排序1----------");
        students.stream().sorted().forEach(System.out::println);
        // sorted(Comparator<? super T> comparator) :我们可以使用lambada 来创建一个Comparator 实例。可以按照升序或着降序来排序元素。
        System.out.println("排序2----------");
        students.stream()
                .sorted(Comparator.comparing(Student::getAge,Comparator.reverseOrder())) // ,Comparator.reverseOrder() 逆序
                .forEach(System.out::println);
        // 创建比较器,通过对比较器内容的定义实现对多个属性进行排序,类似sql中连续的orderBy
        System.out.println("排序3----------");
        students.stream().sorted(
                (s1,s2) -> {
                    if (s1.getAge() == s2.getAge()) {
                        return s1.getSex().compareTo(s2.getSex());
                    } else {
                        return -s1.getAge().compareTo(s2.getAge());
                    }
                }
        ).forEach(System.out::println);
        System.out.println("排序4----------");
        Comparator<Student> studentComparator = (s1,s2) -> {
            Integer age1 = s1.getAge();
            Integer age2 = s2.getAge();
            if (age1 != age2) return age1 - age2;
            Integer sex1 = s1.getSex();
            Integer sex2 = s2.getSex();
            if (sex1 != sex2) return sex2 - sex1;
            return 0;
        };
        students.stream().sorted(studentComparator).forEach(System.out::println);
        // 截取 截取前三个元素
        System.out.println("----------截取----------");
        students.stream().limit(3).forEach(System.out::println);
        // 跳过 跳过前3个元素
        System.out.println("----------跳过----------");
        students.stream().skip(3).forEach(System.out::println);
        // 映射
        System.out.println("----------映射----------");
        System.out.println("映射Map----------");
        // map接收Lambda,将元素转换其他形式,或者是提取信息,并将其映射成一个新的元素
        Stream<Stream<Student>> streamStream1 = students.stream().map(str -> filterStudent(str));
        streamStream1.forEach(sm -> sm.forEach(System.out::println));
        System.out.println("映射flatMap----------");
        // map接收Lambda,将流中的每一个元素转换成另一个流,然后把所有流连成一个流 扁平化映射
        Stream<Student> studentStream2 = students.stream().flatMap(str -> filterStudent(str));
        studentStream2.forEach(System.out::println);
        // 消费
        System.out.println("----------消费----------");
        students.stream().peek(stu -> stu.setAge(100)).forEach(System.out::println);
    }
    public static Stream<Student> filterStudent(Student student) {
        student = new Student();
        return Stream.of(student);
    }
}

Student

public class Student implements Comparable<Student> {
    private Integer id;
    private String name;
    private Integer age;
    private Integer sex;
    public Student() {
    }
    public Student(Integer id, String name, Integer age, Integer sex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Integer getSex() {
        return sex;
    }
    public void setSex(Integer sex) {
        this.sex = sex;
    }
    @Override
    public String toString() {
        return "Student{" +
                "id=&#39;" + id + &#39;\&#39;&#39; +
                ", name=&#39;" + name + &#39;\&#39;&#39; +
                ", age=&#39;" + age + &#39;\&#39;&#39; +
                ", sex=" + sex +
                &#39;}&#39;;
    }
    @Override
    public int compareTo(Student o) {
        return this.getAge() - o.getAge();
    }
}

5. Stream termination operation

The termination operation of Stream , the most commonly used The purpose is to collect the processed data into a new container, and at the same time, it can achieve some effects of grouping to Sql aggregation functions

package com.zhj.java8.stream;
import com.zhj.java8.bean.Student;
import java.util.*;
import java.util.stream.Collectors;
public class TerminationStreamDemo {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        students.add(new Student(1,"小华",23,1));
        students.add(new Student(2,"小米",20,2));
        students.add(new Student(3,"小果",30,3));
        students.add(new Student(4,"小维",18,2));
        students.add(new Student(5,"小华",23,2));
        System.out.println("--------------------匹配聚合操作--------------------");
        // allMatch:接收一个 Predicate 函数,当流中每个元素都符合该断言时才返回true,否则返回false
        boolean allMatch = students.stream().allMatch(stu -> stu.getAge() > 10);
        System.out.println("全部符合大于10岁条件:" + allMatch);
        // noneMatch:接收一个 Predicate 函数,当流中每个元素都不符合该断言时才返回true,否则返回false
        boolean noneMatch = students.stream().noneMatch(stu -> stu.getAge() > 10);
        System.out.println("全部不符合大于10岁条件:" + noneMatch);
        // anyMatch:接收一个 Predicate 函数,只要流中有一个元素满足该断言则返回true,否则返回false
        boolean anyMatch = students.stream().anyMatch(stu -> stu.getAge() > 20);
        System.out.println("含有任意符合大于20岁条件:" + anyMatch);
        // findFirst:返回流中第一个元素
        Student findFirst = students.stream().findFirst().get();
        System.out.println("第一个学生:" + findFirst);
        // findAny:返回流中的任意元素
        Student findAny = students.stream().findAny().get();
        System.out.println("任意一个学生:" + findAny);
        //  count:返回流中元素的总个数
        long count = students.stream().count();
        System.out.println("学生总数:" + count);
        // max:返回流中元素最大值
        Student max = students.stream().max(Student::compareTo).get();
        System.out.println("年龄最大学生:" + max);
        // max:返回流中元素最大值
        Student min = students.stream().min(Student::compareTo).get();
        System.out.println("年龄最小学生:" + min);
        System.out.println("--------------------规约操作--------------------");
        System.out.println("学生年龄总和:" + students.stream().map(Student::getAge).reduce(Integer::sum));
        System.out.println("学生年龄最大:" + students.stream().map(Student::getAge).reduce(Integer::max));
        System.out.println("--------------------收集操作--------------------");
        List<Student> list = students.stream().collect(Collectors.toList());
        Set<Student> set = students.stream().collect(Collectors.toSet());
        Map<Integer, String> map = students.stream().collect(Collectors.toMap(Student::getId, Student::getName));
        String joinName = students.stream().map(Student::getName).collect(Collectors.joining(",", "(", ")"));
        // 总数
        students.stream().collect(Collectors.counting());
        // 最大年龄
        students.stream().map(Student::getAge).collect(Collectors.maxBy(Integer::compare)).get();
        // 年龄和
        students.stream().collect(Collectors.summingInt(Student::getAge));
        // 平均年龄
        students.stream().collect(Collectors.averagingDouble(Student::getAge));
        // 信息合集
        DoubleSummaryStatistics statistics = students.stream().collect(Collectors.summarizingDouble(Student::getAge));
        System.out.println("count:" + statistics.getCount() + ",max:" + statistics.getMax() + ",sum:" + statistics.getSum() + ",average:" + statistics.getAverage());
        // 分组
        Map<Integer, List<Student>> collect = students.stream().collect(Collectors.groupingBy(Student::getSex));
        System.out.println(collect);
        //多重分组,先根据性别分再根据年龄分
        Map<Integer, Map<Integer, List<Student>>> typeAgeMap = list.stream().collect(Collectors.groupingBy(Student::getSex, Collectors.groupingBy(Student::getAge)));
        //分区
        //分成两部分,一部分大于20岁,一部分小于等于20岁
        Map<Boolean, List<Student>> partMap = list.stream().collect(Collectors.partitioningBy(v -> v.getAge() > 20));
        //规约
        Integer allAge = list.stream().map(Student::getAge).collect(Collectors.reducing(Integer::sum)).get();
        System.out.println(allAge);
    }
}

6. Stream features

Lazy execution of intermediate operations

If there are multiple intermediate operations, they will not cycle multiple times.

Multiple conversion operations will only be merged when the operation is terminated, and one cycle will be completed.

  • Internal iteration

  • The iteration after finding the data that meets the conditions will not proceed

  • Flow There is only one end operation for

Exception: stream has already been operated upon or closed

means that the stream has already been operated upon Closed, this is because when we use the end operation, the

stream is closed and cannot be called again. If we want to call it repeatedly, we can only reopen a new stream.

The above is the detailed content of How to use Stream in Java8. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

How to add a JAR file to the classpath in Java? How to add a JAR file to the classpath in Java? Sep 21, 2025 am 05:09 AM

Use the -cp parameter to add the JAR to the classpath, so that the JVM can load its internal classes and resources, such as java-cplibrary.jarcom.example.Main, which supports multiple JARs separated by semicolons or colons, and can also be configured through CLASSPATH environment variables or MANIFEST.MF.

How to create a file in Java How to create a file in Java Sep 21, 2025 am 03:54 AM

UseFile.createNewFile()tocreateafileonlyifitdoesn’texist,avoidingoverwriting;2.PreferFiles.createFile()fromNIO.2formodern,safefilecreationthatfailsifthefileexists;3.UseFileWriterorPrintWriterwhencreatingandimmediatelywritingcontent,withFileWriterover

Why do real-time systems need deterministic response guarantees? Why do real-time systems need deterministic response guarantees? Sep 22, 2025 pm 04:03 PM

Real-time systems require deterministic responses, because correctness depends on the result delivery time; hard real-time systems require strict deadlines, missed will lead to disasters, while soft real-time allows occasional delays; non-deterministic factors such as scheduling, interrupts, caches, memory management, etc. affect timing; the construction plan includes the selection of RTOS, WCET analysis, resource management, hardware optimization and rigorous testing.

How to force scaling web pages by UC browser_UC browser's forced scaling web pages by UC browser How to force scaling web pages by UC browser_UC browser's forced scaling web pages by UC browser Sep 24, 2025 pm 04:54 PM

First, enable the built-in scaling function of UC browser, go to Settings → Browse Settings → Font and Typesetting or Page Scaling, and select a preset ratio or custom percentage; second, you can force the page display size by opening or pinching gestures with two fingers; for web pages that restrict scaling, you can request the desktop version of the website to unlock the restrictions; advanced users can also modify the viewport attributes by executing JavaScript code in the address bar to achieve a more flexible forced scaling effect.

How to get the calling method's name in Java? How to get the calling method's name in Java? Sep 24, 2025 am 06:41 AM

The answer is to use Thread.currentThread().getStackTrace() to get the call method name, and obtain the someMethod name of the call anotherMethod through index 2. Since index 0 is getStackTrace, 1 is the current method, and 2 is the caller, the example output is "Calledbymethod:someMethod", which can also be implemented by Throwable, but attention should be paid to performance, obfuscation, security and inline impact.

Microsoft Edge high CPU usage Microsoft Edge high CPU usage Sep 24, 2025 am 12:17 AM

Edge occupies a high CPU because of the high consumption of resources based on Chromium kernel, plus factors such as multi-tab pages, plug-in running, website scripts and rendering mechanisms; solutions include: 1. Close unnecessary extensions to reduce the burden on the background; 2. Enable the "Sleep Tag" function to reduce the use of idle tag resources; 3. Clean up the background process and close GPU rendering related settings; 4. Update the browser and system to ensure compatibility and performance optimization.

How do you handle exceptions in Java? How do you handle exceptions in Java? Sep 23, 2025 am 04:44 AM

Java exception handling catches exceptions through try-catch blocks, finally blocks ensure resource cleanup, try-with-resources automatically manage resources, throws declare exceptions, custom exceptions to deal with specific errors, and follows best practices such as catching specific exceptions, not ignoring exceptions, and avoiding empty catch blocks, thereby achieving robust and maintainable code.

How to use the Optional class to avoid NullPointerException in Java? How to use the Optional class to avoid NullPointerException in Java? Sep 25, 2025 am 06:04 AM

The Optional class is used to safely handle values ​​that may be null, avoiding null pointer exceptions. 1. Create an instance using Optional.ofNullable to handle null values. 2. Check and access values ​​through isPresent or ifPresent security to avoid direct call to get to cause exceptions. 3. Use orElse and orElseGet to provide default values, or use orElseThrow to throw a custom exception. 4. Convert or filter values ​​through map and filter chain operations to improve code readability and robustness.

See all articles