0

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.

asked Jan 29, 2021 at 22:09
5
  • 1
    What exactly are you trying to do? Commented Jan 29, 2021 at 22:12
  • @Matze._ I am trying to perform 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 Commented Jan 29, 2021 at 22:15
  • I am not sure of a safe way, doing that for that many nested things, i prefer to keep it as simple as I can, so i don't come to an situation like this. Is there a way to simplify this all like unnesting some things? Commented Jan 29, 2021 at 22:20
  • unfortunately i cannot change the structure of the input map here Commented Jan 29, 2021 at 22:31
  • Why do you not want to use what you have? I don't get the recent drive to want to convert all logic to streams and get it all packed onto one line. I find the code you have to be very readable. I expect that a streams equivalent would be less so. Commented Jan 29, 2021 at 22:51

1 Answer 1

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
answered Jan 29, 2021 at 23:22
7
  • 1
    While I agree with the sentiment, this doesn't answer the question :( Commented 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. Commented 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. Commented Jan 29, 2021 at 23:37
  • @Steve. For your enlightenment. meta.stackoverflow.com/questions/386260/… Commented Jan 29, 2021 at 23:47
  • 1
    @nishant I updated my answer based on your correction. Commented Jan 31, 2021 at 14:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.