I need to count a number of occurrences for each string in
ArrayList<ArrayList<String>>
I know how to do it the old java 7 and below way, but want to do it using streams and collectors
Ideally, I would save the result in the HashMap<String, Integer>
, where integer is a number of occurrences and String is the corresponding string.
This can be vice versa - not a big deal.
I found solution how to do it for ArrayList
here
Map<String, Long> counts =
list.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
but my case is more complex since it's an ArrayList
of ArrayList
s.
The structure is following
String itemId = list.get(i).getProducts().get(j).getItemId();