Table of Contents
3.1 简介
3.2 Stream API
3.2.1 Stream.of(T… t)
3.2.2 Stream.collect(Collector<? super T, A, R> collector)
3.2.2 Stream.map(Function<? super T, ? extends R> mapper)
3.2.3 Stream.filter(Predicate<? super T> predicate)
3.2.4 Stream.flatMap(Function<? super T, ? extends Stream<? extends R>> mapper)
3.2.5 Stream.max(Comparator<? super T> comparator)
3.2.7 Sream.reduce(T identity, BinaryOperator<T> binaryOperator)
3.2.8 综合操作
Home Java javaTutorial Detailed explanation of Lambda and Stream in Java8 (with code)

Detailed explanation of Lambda and Stream in Java8 (with code)

Oct 16, 2018 pm 02:42 PM
java8 lambda stream

This article brings you a detailed explanation of Lambda and Stream in Java8 (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Preface

This article mainly introduces the two main new features of Java8, lambda expression and Stream API. The two provide a higher level of abstraction and simplify development. ,Increase productivity.

2. Lambda expression

2.1 First introduction to Lambda expression

Create a thread and use an Runnableanonymous The internal class

Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Hello Aron.");
            }
        });
Copy after login

may not seem like a big problem, but in fact the disadvantages are quite obvious: there are too many template syntaxes, and the only statements that really have business significance are System.out.println("Hello Aron."), because of this, also seriously interferes with our reading of the code.

After introducing lambda expression, you can write like this

Thread thread = new Thread(() -> System.out.println("Hello Aron."));
Copy after login

It’s too concise, is there any idea?

2.2 More Lambda expressions

 Runnable runnable = () -> System.out.println("Hello World.");
 Consumer<T> tConsumer = bean -> System.out.println("Hello World.");
 Runnable runnable1 = () -> {
         System.out.println("Hello World.");
         System.out.println("Hello World.");
     };
Copy after login

The syntax is divided into 3 sections: parameters, -> and statements, that is, (...)- > { ...}

2.3 Function interface

Java is a strongly typed language, and method parameters have fixed types. So here’s the problem. Lambda expressions, if regarded as a bunch of code fragments, will also express a clear intention. This intention can be understood as a functional interface for the time being.

In the process of programming, you will always encounter many functional interfaces. The following are some of the most important functional interfaces in JDK

Examples of interface parameter return types

##PredicateTboolean Is the value equal to "Hello"? ConsumerTvoidOutput a valueFunctionTRGet a property of the objectSupplierNoneTFactory MethodUnaryOperatorTT Logical NOT (!)BinaryOperator(T, T)TFind the sum of 2 numbers ( )

2.4 类型推断

先看一段熟悉的集合代码

ArrayList<Java8Test> list = new ArrayList<>();
Copy after login

在ArrayList中申明了存储的元素的类型,于是在ArrayList<>()这里的类型可以缺省,编译器可以根据左侧(即上文)推断出来。

同理,在lambda表达式也是一样的。

BinaryOperator<Long> addLongs = (x, y) -> x + y;
Copy after login

在上面的表达 式中,我们注意到 (x, y)这里是没有申明方法的参数类型的,却能执行数学运算 +

这里根据函数接口指定的泛型类为Long,即可推断方法的参数为Long,然后执行x + y

2.5 Lambda小结

Lambda表达式是一个匿名方法,简化了匿名内部类的写法,把模板语法屏蔽,突出业务语句,传达的更像一种行为。

Lambda表达式是有类型的,JDK内置了众多函数接口

Lambda的3段式结构:(...)-> { ...}

3. Stream 流

3.1 简介

流(Stream)是Java8的新特性,是一种使程序员得以站在更高的抽象层次上对集合进行操作。在思路上,类似于SQL的存储过程,有几个步骤:

  1. 先定义一些操作的集合,注意:这里只定义,不真正执行

  2. 触发执行,获取结果

  3. 对结果进一步处理,筛选、打印、使用

其中,第1步的定义操作叫惰性求值,给你套路(返回Stream),但是不会执行返回结果。

第2步的触发操作叫及早求值,这个人说干就干,立马要结果(返回结果数据)。

第3步的筛选类似SQL的where子句,对结果进一步的筛选。

3.2 Stream API

Stream 类位于java.util.stream包,是Java8核心的类之一,拥有众多方法,下面罗列了一些比较重要的方法进行讲解。更多的是抛砖引玉,任何教程都比不上自己的悟性来得爽快,先找点感觉,先掌握基本用法尝试使用起来,慢慢自然就会了。

3.2.1 Stream.of(T… t)

要使用Stream,那就必须得先创建一个String类型的Stream

Stream<String> StrStream = Stream.of("a", "b");
Copy after login

3.2.2 Stream.collect(Collector collector)

使用收集器CollectorStrStream转化为熟悉的集合Collection

 List<String> collect = StrStream.collect(Collectors.toList());
Copy after login

3.2.2 Stream.map(Function mapper)

所谓map,从字面理解就是映射。这里指的是对象关系的映射,

比如从对象集合映射到属性结合:

List<String> names = Stream.of(new Student("zhangSan"), new Student("liSi"))
                        .map(student -> student.getName())
                        .collect(toList());
Copy after login

从小写字母映射到大写字母:

List<String> collected = Stream.of("a", "b", "hello")
                        .map(string -> string.toUpperCase())
                        .collect(toList());
Copy after login

将 字符串流 根据空格分割成 字符串数组流

Stream<String> stringStream = Stream.of("Hello Aron.");
Stream<String[]> stringArrayStream = stringStream.map(word -> word.split(" "));
Copy after login

3.2.3 Stream.filter(Predicate predicate)

filter顾名思义,过滤筛选。这里的参数函数接口是一个条件,筛选出满足条件的元素

// 筛选年龄大于19的学生
List<Student> stu = Stream.of(new Student("zhangSan", 19), new Student("liSi"), 20)
                        .filter(student -> student.getAge() > 19)
                        .collect(toList());
Copy after login

3.2.4 Stream.flatMap(Function> mapper)

flatMap扁平化映射,即将数据元素为数组的Stream转化为单个元素

Stream<String> stringStream = Stream.of("Hello Aron.");
// 返回值为数组
Stream<String[]> stringArrayStream = stringStream.map(word -> word.split(" "));
// flatMap扁平化映射后,元素都合并了
Stream<String> flatResult = stringArrayStream.flatMap(arr -> Arrays.stream(arr))
Copy after login

3.2.5 Stream.max(Comparator comparator)

max即最大,类似SQL中的函数max(),从数据中根据一定的条件筛选出最值

// 筛选年龄最大/小的学生
Stream<Student> studentStream = Stream.of(new Student("zhangSam", 19), new Student("liSi", 20));
Optional<Student> max = studentStream.max(Comparator.comparing(student -> student.getAge()));
// Optional<Student> max = studentStream.min(Comparator.comparing(student -> student.getAge()));
// 年龄最大/小的学生
Student student = max.get();
Copy after login

3.2.7 Sream.reduce(T identity, BinaryOperator binaryOperator)

reduce操作实现从一组值中生成一个值,上面的maxmin实际上都是reduce操作。

参数Identity 表示初始值,

参数binaryOperator是一个函数接口,表示二元操作,可用于数学运算

// 使用reduce() 求和 (不推荐生产环境使用)
int count = Stream.of(1, 2, 3).reduce(0, (acc, element) -> acc + element);
Copy after login

上面代码,展开reduce() 操作

BinaryOperator<Integer> accumulator = (acc, element) -> acc + element;
int count = accumulator.apply( accumulator.apply(accumulator.apply(0, 1),2), 3);
Copy after login

3.2.8 综合操作

// 查找所有姓张的同学并按字典顺序排序,存储到list
List<Student> newList = studentList.Stream()
            .filter(student -> student.getName().startsWith("张"))
            .sorted(Comparator.comparing(student -> student.getName())
            .collect(toList());
Copy after login

Interface Parameters Return type Example

The above is the detailed content of Detailed explanation of Lambda and Stream in Java8 (with code). 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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How do lambda expressions handle exceptions in C++? How do lambda expressions handle exceptions in C++? Apr 17, 2024 pm 12:42 PM

In C++, there are two ways to handle exceptions using Lambda expressions: catch the exception using a try-catch block, and handle or rethrow the exception in the catch block. Using a wrapper function of type std::function, its try_emplace method can catch exceptions in Lambda expressions.

What is the meaning of closure in C++ lambda expression? What is the meaning of closure in C++ lambda expression? Apr 17, 2024 pm 06:15 PM

In C++, a closure is a lambda expression that can access external variables. To create a closure, capture the outer variable in the lambda expression. Closures provide advantages such as reusability, information hiding, and delayed evaluation. They are useful in real-world situations such as event handlers, where the closure can still access the outer variables even if they are destroyed.

How to calculate date one year ago or one year later in Java 8? How to calculate date one year ago or one year later in Java 8? Apr 26, 2023 am 09:22 AM

Java8 calculates the date one year ago or one year later using the minus() method to calculate the date one year ago packagecom.shxt.demo02;importjava.time.LocalDate;importjava.time.temporal.ChronoUnit;publicclassDemo09{publicstaticvoidmain(String[]args ){LocalDatetoday=LocalDate.now();LocalDatepreviousYear=today.minus(1,ChronoUni

What are the advantages of using C++ lambda expressions for multi-threaded programming? What are the advantages of using C++ lambda expressions for multi-threaded programming? Apr 17, 2024 pm 05:24 PM

The advantages of lambda expressions in C++ multi-threaded programming include simplicity, flexibility, ease of parameter passing, and parallelism. Practical case: Use lambda expressions to create multi-threads and print thread IDs in different threads, demonstrating the simplicity and ease of use of this method.

How to implement closure in C++ Lambda expression? How to implement closure in C++ Lambda expression? Jun 01, 2024 pm 05:50 PM

C++ Lambda expressions support closures, which save function scope variables and make them accessible to functions. The syntax is [capture-list](parameters)->return-type{function-body}. capture-list defines the variables to capture. You can use [=] to capture all local variables by value, [&] to capture all local variables by reference, or [variable1, variable2,...] to capture specific variables. Lambda expressions can only access captured variables but cannot modify the original value.

How to debug Java Stream operations in IntelliJ IDEA How to debug Java Stream operations in IntelliJ IDEA May 09, 2023 am 11:25 AM

Stream operation is a highlight of Java8! Although java.util.stream is very powerful, there are still many developers who rarely use it in actual work. One of the most complained reasons is that it is difficult to debug. This was indeed the case at the beginning, because streaming operations such as stream cannot be used in DEBUG When it is one line of code, when it comes to the next step, many operations are actually passed at once, so it is difficult for us to judge which line in it is the problem. Plug-in: JavaStreamDebugger If the IDEA version you are using is relatively new, this plug-in is already included and does not need to be installed. If it is not installed yet, install it manually and then continue below.

How does a C++ lambda expression capture external variables? How does a C++ lambda expression capture external variables? Apr 17, 2024 pm 04:39 PM

There are three ways to capture lambda expressions of external variables in C++: Capture by value: Create a copy of the variable. Capture by reference: Get a variable reference. Capture by value and reference simultaneously: Allows capturing of multiple variables, either by value or by reference.

How to get max value from stream in java8 How to get max value from stream in java8 May 14, 2023 pm 03:43 PM

java8's stream takes maxpublicstaticvoidmain(String[]args){Listlist=Arrays.asList(1,2,3,4,5,6);Integermax=list.stream().max((a,b)->{if (a>b){return1;}elsereturn-1;}).get();System.out.println(max);}Note: The size is determined here through positive and negative numbers and 0 values. Instead of writing it directly if(a>b){returna;}elseretur

See all articles