Try the following:
LinkedHashMap<Character, Long> counts = Pattern.compile(",")
.splitAsStream(subjects)
.collect(Collectors.groupingBy(
s -> s.charAt(0),
LinkedHashMap::new,
Collectors.counting()));
If you must have a counter Integer, you can wrap the collection counter as follows:
Collectors.collectingAndThen(Collectors.counting(), Long::intValue)
Another option (thanks @Holger ):
Collectors.summingInt(x -> 1)
As an additional note, you can replace the loop update as follows:
map.merge(v, 1, Integer::sum);
source
share