How to find Count of element using stream?

Interview Question ?

How to find Count of element using stream?

Solution :-

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);

}

}

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

 

Comments

Popular posts from this blog

What are POJO classes in spring? What is the use of POJO Class, and How to create POJO classes.

How to create React project using command prompt?