Java8 streams and filters

Viewed 276

I have a set multimap that stores some strings against a month(integer).

MonthlyCount = {1:["hello"],2:["hello","hi"]}   

I have a map that counts the number of strings per month and adds them. I use containsKey filter to check if the key is present in the collection, and add them to the map.

final String[] months = new DateFormatSymbols().getShortMonths();
final Map<String, Object> metaData = new LinkedHashMap<>();
private SetMultimap<Integer, String> MonthlyCount;

for (int i = 0; i <= month; i++) {
    final int count = MonthlyCount.containsKey(i + 1) ? MonthlyCount.get(i + 1).size() : 0;
    metaData.put(months[i], count);
}

Is there a way to achieve the same behavior with Java8's streams and filters ?

2 Answers
Related