How to find Count of element using stream?

Solution :- we will use Map to find Count of element using stream

package p2;

import java.util.Arrays;

import java.util.List;

import java.util.Map;

import java.util.stream.Collectors;

public class CountOfElement {

public static void main(String[] args) {

List<Integer> list=Arrays.asList(1,1,2,3,3,5,5,7,8,9,9);

Map<Integer,Long> map=list.stream().collect(Collectors.groupingBy(i->i,Collectors.counting()));

System.out.println(map);

map=map.entrySet().stream().filter(e->e.getValue()>1).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

System.out.println(map);

}

}

**************************************************************

Leave a Reply

Your email address will not be published. Required fields are marked *

*