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.
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.
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]
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]
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
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
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
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]
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]
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]