It's not too late yet? Java Stream API all method usage example (intermediate operation)

Begin

The Stream API started from Java8, but when and what to use Occasionally? So, as a memorandum, I will focus on intermediate processing this time. Therefore, I will omit detailed explanations and post more and more sample code.

** (Although it is written as all methods, some are omitted. I'm sorry) **

This time, only the usage example of stream using ArrayList is shown.

Technical elements

Benefits of using Stream API

Prior knowledge

Advance preparation

Book.java


class Book {
    private Integer id;         //Store ID
    private String title;       //Book title
    private String author;      //Author
    private Integer age;        //Target age

    Book(Integer id, String title, String author, Integer age) {
        this.id = id;
        this.title = title;
        this.author = author;
        this.age = age;
    }

    public Integer getId() { return id; }
    public String getTitle() { return title; }
    public String getAuthor() { return author; }
    public Integer getAge() { return age; }

    @Override
    public String toString() {
        return "[id: " + getId() + ", title: " + getTitle() + ", author: " + getAuthor() + ", age: " + getAge() + "]";
    }
}

Main.java


public class Main {
    public static void main(String[] args) {
        List<Book> bookList = Arrays.asList(
                  new Book(1, "book1", "author1", 20)
                , new Book(2, "book2", "author2", 12)
                , new Book(3, "book3", "author3", 6));


//Describe the processing used below here.

Introduction of each method

filter --filter by condition

Main.java


bookList.stream()
        .filter(e -> e.getAge() == 12)  //age=Extract only 12
        .forEach(System.out::println);

//Expected results
[id: 2, title: book2, author: author2, age: 12]

map --Perform processing such as type conversion (1: 1 conversion)

Main.java


bookList.stream()
        // [title:Author]Convert to the shape of
        .map(e -> "[" + e.getTitle() + " : " + e.getAuthor() + "]")
        .forEach(System.out::print);

//Expected results
[book1 : author1][book2 : author2][book3 : author3]

Performs processing such as flatMap type conversion (1: N conversion, N is a natural number)

Main.java


bookList.stream()
        .flatMap(e -> Stream.of(e.getId(), e.getTitle())) //Extract id and title
        .forEach(System.out::println);

//Expected results
1
book1
2
book2
3
book3

distinct --remove duplicates

Main.java


List<Integer> distinctList = Arrays.asList(1, 1, 2, 2, 2, 3, 4, 4, 5, 4, 6);

distinctList.stream()
            .distinct() //Duplicate removal
            .forEach(System.out::println);

//Expected results
1
2
3
4
5
6

sorted --Sort (you can sort differently by adding an argument to sorted)

Main.java


List<Integer> sortList = Arrays.asList(4, 2, 5, 10, 1);

sortList.stream()
        .sorted()      //Sort in natural order
        .forEach(System.out::println);

//Expected results
1
2
4
5
10

peek --Intermediate processing that does not change the stream.

Main.java


bookList.stream()
        .peek(System.out::println) //Log out, etc.
        .filter(e -> e.getAge() < 16)
        .forEach(System.out::println);

//Expected results
[id: 1, title: book1, author: author1, age: 20] // peek()Output by
[id: 2, title: book2, author: author2, age: 12] // peek()Output by
[id: 2, title: book2, author: author2, age: 12]
[id: 3, title: book3, author: author3, age: 6]  // peek()Output by
[id: 3, title: book3, author: author3, age: 6]

limit-Display up to (argument) elements.

Main.java


bookList.stream()
        .limit(2)   //Squeeze to two
        .forEach(System.out::println);

//Expected results
[id: 1, title: book1, author: author1, age: 20]
[id: 2, title: book2, author: author2, age: 12]

skip-skip (argument) elements and display the rest.

Main.java



bookList.stream()
        .skip(1)   //Skip one
        .forEach(System.out::println);

//Expected results
[id: 2, title: book2, author: author2, age: 12]
[id: 3, title: book3, author: author3, age: 6]

Summary

Recommended Posts

It's not too late yet? Java Stream API all method usage example (intermediate operation)
[Java] Stream API intermediate operation
[Java] Stream API --Stream intermediate processing
Java Stream API