0

I have a List<String> which is like,

List<String> dummyList=Arrays.asList("a","b","c","a","b","e","f","e");

I have a list which contains names of students in a class, I need a lambda expression to fetch only the count of students with names "a","b","c".

I tried using,

dummyList.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

it gave count of entire students in the class, but I need the count of students "a","b","c", the final map should be of,

a:2
b:2
c:1
Maroun
  • 94,125
  • 30
  • 188
  • 241
Aravind.V
  • 1
  • 2
  • Look at the Java 8 solution there; you want to `filter`. In this case `.filter(s -> s.equals("a") || s.equals("b") || s.equals("c"))` – Tunaki Apr 18 '16 at 11:52
  • Use a `Predicate` to **filter** the stream: `Set whitelist = Stream.of('a', 'b', 'c').collect(Collectors.toSet()); Predicate condition = s -> s.length() > 0 && whitelist.contains(s.charAt(0));` – Peter Walser Apr 18 '16 at 11:59

0 Answers0