티스토리 뷰

공부

[Java] Stream GroupBy

승가비 2021. 11. 5. 15:57
728x90
import java.util.*;
import java.util.stream.*;

class Test {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();

        list.add("Hello");
        list.add("Hello");
        list.add("World");

        Map<String, Long> counted = list.stream()
            .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

        System.out.println(counted);
    }
}

https://stackoverflow.com/questions/25441088/how-can-i-count-occurrences-with-groupby

 

How can I count occurrences with groupBy?

I want to collect the items in a stream into a map which groups equal objects together, and maps to the number of occurrences. List<String> list = Arrays.asList("Hello", "Hello...

stackoverflow.com

https://akageun.github.io/2019/08/06/java-stream-groupby.html

 

[jdk 8]Stream GroupBy 사용하기.

언제나 개발하기를 즐기는 개발자 입니다.

akageun.github.io

 

728x90

'공부' 카테고리의 다른 글

[Java] Call static generic method  (0) 2021.11.08
[Java] Dynamic cast in generic  (0) 2021.11.08
What is the purpose of List<?> if one can only insert a null value?  (0) 2021.11.05
[HDFS] hdfs dfs rm -r  (0) 2021.11.05
[Java] Encoding UTF-8  (0) 2021.11.05
댓글