i have a map, need to operate on each entry's value, and return the modified map. I managed to get it working, but the resulted map contains entries with empty value, and I want to remove those entries but cannot with Java 8 stream API.
here is my original code:
Map<String, List<Test>> filtered = Maps.newHashMap();
for (String userId : userTests.keySet()) {
List<Test> tests = userTests.get(userId);
List<Test> filteredTests = filterByType(tests, supportedTypes);
if (!CollectionUtils.isEmpty(filteredTests)) {
filtered.put(userId, filteredTests);
}
}
return filtered;
and here is my Java 8 stream API version:
userTests.entrySet().stream()
.forEach(entry -> entry.setValue(filterByType(entry.getValue(), supportedTypes)));
userTests.entrySet().stream().filter(entry -> !entry.getValue().isEmpty());
return userTests;
- how can i remove entries with empty/null value from the map?
- is there better way to write the code in stream API, so far I don't see it's better than my original code