티스토리 뷰

728x90

counting: https://stackabuse.com/guide-to-java-8-collectors-counting/

 

Guide to Java 8 Collectors: counting()

In this guide, learn how to count the number of elements in a collection or stream in Java, as well as how to use the Collectors.counting() as a downstream function of other collectors, with examples.

stackabuse.com

mapping: https://www.javabrahman.com/java-8/java-8-how-to-use-collectors-mapping-collector-with-examples/

 

Java 8 – How to use Collectors.mapping Collector with examples

Tutorial explains how to use Java 8's predefined collector returned by Collectors.mapping() method with examples. It explains mapping Collector's working, and provides two Java 8 code examples showing how to use Collectors.mapping collector.

www.javabrahman.com

reducing: https://codechacha.com/ko/java8-stream-reduction/

 

Java8의 Stream reduction 사용 방법 및 예제

reduce는 Stream의 데이터를 변환하지 않고, 더하거나 빼는 등의 연산을 수행하여 하나의 값으로 만들 수 있습니다. 예를 들어 수열을 계산하는데 사용할 수 있습니다. 또한 병렬처리를 적용하여 연

codechacha.com

 

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
링크
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31