I have a list of strings like this: "na","na","na","uk","uk". I want to count the frequency of each element in such a way that if the existing map's value is even, then I will add 1; otherwise 2.
List<String> streamer = Arrays.asList("na", "na", "na", "uk", "uk");
Map<String, Integer> m = new HashMap<>();
for(String s:streamer) {
if (m.containsKey(s)) {
if(m.get(s) % 2 == 0)
m.put(s, m.get(s) + 1);
else
m.put(s,m.get(s)+2);
}
else
m.put(s,1);
}
System.out.println("CUSTOM Frequency::::" + m);
Now, I want to achieve the exact same thing using streams and lambdas.
All I could do is this:
Map<String, Long>map4 = streamer.stream()
.collect(Collectors.groupingBy(
Function.identity(),
Collectors.counting())); // How can I get custom counting instead of Collectors.counting() ?