I have a map currently which is Map<String, Map<String, List<String>>>
I want to do an operation on each item in each nested list.
I can do it with lots of nested for-loops:
(EDIT - Updating parameters of performOperation function)
final Set<ResultType> resultSet = new HashSet<>();
for(Map.Entry<String, Map<String, List<String>>> topKeyEntry : inputNestedMap.entrySet()) {
for (Map.Entry<String, List<String>> innerKeyEntry : topKeyEntry.getValue().entrySet()) {
for (String listItem : innerKeyEntry.getValue()) {
resultSet.add(performOperation(listItem, topKeyEntry.getKey(), innerKeyEntry.getKey()));
}
}
}
How do I do it with streams?
I tried it with nesting stream
and apply map()
and eventually calling the operation in the innermost map, but that results in an error saying there is a return missing.
I tried to flatten the entry list with flatMap()
but could not get through.
1 Answer 1
Based on your updated question, I have posted the following. It is essentially what you already have but in less cluttered form using the forEach
method.
inputNestedMap.forEach((outerKey, innerMap) -> innerMap
.forEach((innerKey, innerList) -> innerList.forEach(
listItem -> resultSet.add(performOperation(
listItem, outerKey, innerKey)))));
And here is the modifed stream approach based on your changes. I would stick with the nested for loop solution.
Set<ResultType> resultSet= inputNestedMap.entrySet().stream()
.flatMap(outerEntrySet-> outerEntrySet.getValue()
.entrySet().stream() // flatten outer entrySet
.flatMap(innerEntrySet -> innerEntrySet
.getValue().stream(). // flatten inner entrySet
map(lstVal->performOperation(lstVal, // perform the operation
outerEntrySet.getKey(),
innerEntrySet.getKey())))).
collect(Collectors.toSet()); // return as as set
-
1While I agree with the sentiment, this doesn't answer the question :(CryptoFool– CryptoFool2021年01月29日 23:28:59 +00:00Commented Jan 29, 2021 at 23:28
-
You should know that it is acceptable to provide alternative, more efficient approaches if they are actually better. This is done extremely often on this site and a vast majority of the time, well received. Your down vote is completely unwarranted.WJS– WJS2021年01月29日 23:32:20 +00:00Commented Jan 29, 2021 at 23:32
-
Quite the contrary. I've removed the downvote after it had the desired effect. I consider your answer to be twice as good now, as it allows the OP and others that come later to both get what they were looking for and to benefit by your suggestion as to an alternative. - I would agree with you if it were clear that your alternative was universally better, but that isn't my opinion, and downvotes specifically exist to let me voice my opinion.CryptoFool– CryptoFool2021年01月29日 23:37:40 +00:00Commented Jan 29, 2021 at 23:37
-
@Steve. For your enlightenment. meta.stackoverflow.com/questions/386260/…WJS– WJS2021年01月29日 23:47:42 +00:00Commented Jan 29, 2021 at 23:47
-
1@nishant I updated my answer based on your correction.WJS– WJS2021年01月31日 14:50:51 +00:00Commented Jan 31, 2021 at 14:50
performOperation
on each item in the nested lusts and add it to a result set. that currently thats what I am doing with nest for's, but want to do with streams to be cleaner