[Java learning] get to know Stream API

Keywords: Java Lambda

Preface

Stream is a new feature added in Java 1.8, which greatly simplifies the processing of collection objects and so on.

As the name implies, Stream is a Stream. It regards the input set elements as a Stream. Like pipeline processing, it can filter, sort, aggregate and other operations on set elements.

Stream detailed explanation

For the next example, first define a User class.

public class User {
    private String name;
    private double height;

//To omit the constructor get and set methods and rewrite toString() method
}

Then generate a User List

List<User> users = new ArrayList<>();
for (int i = 0; i < 5; i++) {
     User user = new User("user" + i, 120.0 + 10 * i); // nameļ¼Œ height
     users.add(user);
}

1 acquisition flow

Java supports a variety of data flow acquisition methods. Such as collections, arrays, values, and files. The most common data sources are collections and arrays.
Collection

List<User> users = new ArrayList<>();

Stream<User> userStream = users.stream()

Array

int[] intArray = {1,2,3,4};
IntStream streamInt = Arrays.stream(intArray);

2 filter

Filter user s over 130 in height

List userStreamFilter = users.stream().filter(s -> s.getHeight() > 130).collect(Collectors.toList());
users.forEach(s -> System.out.println("origin: " + s));
userStreamFilter.forEach(s -> System.out.println("filter: " + s));

Output:

origin: User{ name='user0', height=120.0}
origin: User{ name='user1', height=130.0}
origin: User{ name='user2', height=140.0}
origin: User{ name='user3', height=150.0}
origin: User{ name='user4', height=160.0}

filter: User{ name='user2', height=140.0}
filter: User{ name='user3', height=150.0}
filter: User{ name='user4', height=160.0}

3. De duplication

If you override the equals() method in the User class, the one with the same name and height will return true.

users.add(new User("user0", 120.0)); //Add a duplicate user data
List userStreamDistinct = users.stream().distinct().collect(Collectors.toList());
users.forEach(s -> System.out.println("origin: " + s));
userStreamDistinct.forEach(s -> System.out.println("distinct: " + s));

Output:

origin: User{ name='user0', height=120.0}
origin: User{ name='user1', height=130.0}
origin: User{ name='user2', height=140.0}
origin: User{ name='user3', height=150.0}
origin: User{ name='user4', height=160.0}
origin: User{ name='user0', height=120.0} //The duplicate data is in the original data
distinct: User{ name='user0', height=120.0}
distinct: User{ name='user1', height=130.0}
distinct: User{ name='user2', height=140.0}
distinct: User{ name='user3', height=150.0}
distinct: User{ name='user4', height=160.0}//Duplicate data removed

4 skip the previous element skip

Skip the first three user s of the list.

List userStreamSkip = users.stream().skip(3).collect(Collectors.toList());
users.forEach(s -> System.out.println("origin: " + s));
userStreamSkip.forEach(s -> System.out.println("distinct: " + s));

output

origin: User{ name='user0', height=120.0}
origin: User{ name='user1', height=130.0}
origin: User{ name='user2', height=140.0}
origin: User{ name='user3', height=150.0}
origin: User{ name='user4', height=160.0}
skip: User{ name='user3', height=150.0}
skip: User{ name='user4', height=160.0}

5 intercept the previous elements limit

Only the first three user s are returned

List userStreamLimit = users.stream().limit(3).collect(Collectors.toList());
users.forEach(s -> System.out.println("origin: " + s));
userStreamLimit.forEach(s -> System.out.println("limit: " + s));

Output:

origin: User{ name='user0', height=120.0}
origin: User{ name='user1', height=130.0}
origin: User{ name='user2', height=140.0}
origin: User{ name='user3', height=150.0}
origin: User{ name='user4', height=160.0}
limit: User{ name='user0', height=120.0}
limit: User{ name='user1', height=130.0}
limit: User{ name='user2', height=140.0}

6 mapping map

Each element in the stream performs a function that converts the element to another type of output. The flow passes each element to the map function, executes the Lambda expression in the map, and finally stores the execution result in a new flow

For example, get the height property of each user in the userList

List userStreamMap = users.stream().map(User::getHeight).collect(Collectors.toList());
users.forEach(s -> System.out.println("origin: " + s));
userStreamMap.forEach(s -> System.out.println("map: " + s));

Output:

origin: User{ name='user0', height=120.0}
origin: User{ name='user1', height=130.0}
origin: User{ name='user2', height=140.0}
origin: User{ name='user3', height=150.0}
origin: User{ name='user4', height=160.0}
map: 120.0
map: 130.0
map: 140.0
map: 150.0
map: 160.0

Posted by hand on Tue, 31 Dec 2019 19:07:13 -0800