티스토리 뷰
728x90
counting: https://stackabuse.com/guide-to-java-8-collectors-counting/
mapping: https://www.javabrahman.com/java-8/java-8-how-to-use-collectors-mapping-collector-with-examples/
reducing: https://codechacha.com/ko/java8-stream-reduction/
1. counting을 groupingBy function과 함께 사용하면 통계를 내는데 유용하다.
package com.code.test;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<Book> books = Arrays.asList(
new Book("The Fellowship of the Ring", "J.R.R. Tolkien", 1954, 30),
new Book("The Hobbit", "J.R.R. Tolkien", 1937, 40),
new Book("Animal Farm", "George Orwell", 1945, 37),
new Book("Nineteen Eighty-Four", "George Orwell", 1949, 55),
new Book("The Road to Wigan Pier", "George Orwell", 1937, 25),
new Book("Lord of the Flies", "William Golding", 1954, 44)
);
Map<String, Long> soldCopiesStats = books.stream()
.collect(Collectors.groupingBy(Book::getAuthor, Collectors.counting()));
System.out.println(soldCopiesStats);
}
public static class Book {
private String title;
private String author;
private int releaseYear;
private int soldCopies;
public Book(String title, String author, int releaseYear, int soldCopies) {
this.title = title;
this.author = author;
this.releaseYear = releaseYear;
this.soldCopies = soldCopies;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getReleaseYear() {
return releaseYear;
}
public void setReleaseYear(int releaseYear) {
this.releaseYear = releaseYear;
}
public int getSoldCopies() {
return soldCopies;
}
public void setSoldCopies(int soldCopies) {
this.soldCopies = soldCopies;
}
}
}
결과
{J.R.R. Tolkien=2, William Golding=1, George Orwell=3} |
2. downstream기준으로 mapper를 mapping시킨다.
package com.code.test;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<Book> books = Arrays.asList(
new Book("The Fellowship of the Ring", "J.R.R. Tolkien", 1954, 30),
new Book("The Hobbit", "J.R.R. Tolkien", 1937, 40),
new Book("Animal Farm", "George Orwell", 1945, 37),
new Book("Nineteen Eighty-Four", "George Orwell", 1949, 55),
new Book("The Road to Wigan Pier", "George Orwell", 1937, 25),
new Book("Lord of the Flies", "William Golding", 1954, 44)
);
List<String > titles = books.stream()
.collect(Collectors.mapping(Book::getTitle, Collectors.toList()));
System.out.println(titles);
Optional<Integer> maxSoldCopies = books.stream()
.collect(Collectors.mapping(Book::getSoldCopies, Collectors.maxBy(Integer::compareTo)));
System.out.println(maxSoldCopies);
}
public static class Book {
private String title;
private String author;
private int releaseYear;
private int soldCopies;
public Book(String title, String author, int releaseYear, int soldCopies) {
this.title = title;
this.author = author;
this.releaseYear = releaseYear;
this.soldCopies = soldCopies;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getReleaseYear() {
return releaseYear;
}
public void setReleaseYear(int releaseYear) {
this.releaseYear = releaseYear;
}
public int getSoldCopies() {
return soldCopies;
}
public void setSoldCopies(int soldCopies) {
this.soldCopies = soldCopies;
}
}
}
결과
[The Fellowship of the Ring, The Hobbit, Animal Farm, Nineteen Eighty-Four, The Road to Wigan Pier, Lord of the Flies] Optional[55] |
3. 데이터를 더하거나 빼는 등의 연산을 수행한다.
package com.code.test;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<Book> books = Arrays.asList(
new Book("The Fellowship of the Ring", "J.R.R. Tolkien", 1954, 30),
new Book("The Hobbit", "J.R.R. Tolkien", 1937, 40),
new Book("Animal Farm", "George Orwell", 1945, 37),
new Book("Nineteen Eighty-Four", "George Orwell", 1949, 55),
new Book("The Road to Wigan Pier", "George Orwell", 1937, 25),
new Book("Lord of the Flies", "William Golding", 1954, 44)
);
Optional<Integer> sum = books.stream()
.collect(Collectors.mapping(Book::getSoldCopies, Collectors.toList()))
.stream()
.reduce((x, y) -> x + y);
System.out.println(sum);
}
public static class Book {
private String title;
private String author;
private int releaseYear;
private int soldCopies;
public Book(String title, String author, int releaseYear, int soldCopies) {
this.title = title;
this.author = author;
this.releaseYear = releaseYear;
this.soldCopies = soldCopies;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getReleaseYear() {
return releaseYear;
}
public void setReleaseYear(int releaseYear) {
this.releaseYear = releaseYear;
}
public int getSoldCopies() {
return soldCopies;
}
public void setSoldCopies(int soldCopies) {
this.soldCopies = soldCopies;
}
}
}
결과
Optional[231] |
728x90
'JAVA' 카테고리의 다른 글
List<Integer> <-> int[] 변환 (0) | 2022.04.19 |
---|---|
LTS vs non-LTS version (0) | 2022.04.05 |
public static void main(String[] args) (0) | 2022.04.02 |
method 매개변수 final (0) | 2022.04.02 |
AssertJ 자주쓰이는 예외 처리 (0) | 2022.04.02 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크