Home  >  Article  >  Java  >  Simple example of Lambda expression and Stream class in Java

Simple example of Lambda expression and Stream class in Java

黄舟
黄舟Original
2017-01-17 15:14:071236browse

Ask a question

How to use Java's lambda expressions and Stream? ? ?

Solve the problem

The syntax of Lambda expression

Basic syntax:

[code](parameters) -> expression
或
(parameters) ->{ statements; }

Look at the examples to learn!

Example 1: Define an AyPerson class to prepare for subsequent testing.

[code]package com.evada.de;

import java.util.Arrays;
import java.util.List;

class AyPerson{

    private String id;
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public AyPerson(String id, String name) {
        this.id = id;
        this.name = name;
    }
}

/**
 * Created by Ay on 2016/5/9.
 */
public class LambdaTest {

    public static void main(String[] args) {
        List<String> names = Arrays.asList("Ay", "Al", "Xy", "Xl");
        names.forEach((name) -> System.out.println(name + ";"));
    }
}

The above example names.forEach((name) -> System.out.println(name + “;”));

is similar to:

function(name){//name为参数 

System.out.println(name + “;”);//方法体 

}
Result:
[code]Ay;
Al;
Xy;
Xl;

Example 2: Copy the following code into the main function above

[code]List<Student> personList = new ArrayList<>();
personList.add(new Student("00001","Ay"));
personList.add(new Student("00002","Al"));
personList.add(new Student("00003","To"));
personList.forEach((person) -> System.out.println(person.getId()+ ":" + person.getName()));

Result:

[code]00001:Ay
00002:Al
00003:To

Example 3: f**ilter( in Lambda and Stream classes )**Method

[code]List<AyPerson> personList = new ArrayList<>();
    personList.add(new AyPerson("00001","Ay"));
    personList.add(new AyPerson("00002","Al"));
    personList.add(new AyPerson("00003", "To"));
    //stream类中的filter方法
    personList.stream()
            //过滤集合中person的id为00001
            .filter((person) -> person.getId().equals("00001"))
            //将过滤后的结果循环打印出来
            .forEach((person) -> System.out.println(person.getId() + ":" + person.getName()));

Result:

[code]00001:Ay

Example 4: collect() method in Stream class,

[code]    List<AyPerson> personList = new ArrayList<>();
    List<AyPerson> newPersonList = null;

    personList.add(new AyPerson("00001","Ay"));
    personList.add(new AyPerson("00002","Al"));
    personList.add(new AyPerson("00003", "To"));
    //将过滤后的结果返回到一个新的List中
    newPersonList = personList.stream()
            .filter((person) -> person.getId().equals("00002")).collect(Collectors.toList());
    //打印结果集
    newPersonList.forEach((person) -> System.out.println(person.getId() + ":" + person.getName()));

##In addition to Put it in List, you can also put it in Set, etc...
Result:
[code]00002:Al

There are many useful methods that can be used in Stream, such as count, limit, etc. Go to the API to learn by yourself

The above is the content of simple examples of Lambda expressions and Stream classes in Java. For more related content, please pay attention to the PHP Chinese website ( www.php.cn)!


Statement:
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