3

I have a list of Objects that I want to convert into a hash map. Is it possible to create a hash-map of this type Map<String, Map<String, List<Object>>?

The reason for this is that I need to sort these by some properties in the objects so i can iterate through it later. So below is Json of what the objects currently look like:

[
  object1: {plan: "11", relationship: "A", etc...},
  object2: {plan: "12", relationship: "A", etc...},
  object3: {plan: "11", relationship: "B", etc...},
]

I was trying to use Collectors to get it into a HashMap but i am confused on the second hash map, I can't seem to get the list of relationships into a Map.

Map<String, Map<String, List<myObject>> myHash = objectList
    .stream()
    .collect(Collectors.toMap(myObject::plan, 
        a -> a.stream().collect(Collectors.groupingBy(myObject::relationship))));

What I want to get is something like this

[
  {
    key: "11", 
    value: [
             {key: "A", value: [ object1 ]},
             {key: "B", value: [ object3 ]}
           ]
  },
  {
    key: "12",
    value: [{key: "A", value: [ object2 ]}]
  }
]
5
  • Just a hint: Java is an object oriented language so I would suggest that you try to avoid containers in containers and create your own object(s) which represent(s) what you want. This would lead to code that's easier to read and easier to understand because your object will have a name. Commented Apr 17, 2020 at 6:07
  • @thehandofNOD Is this still the case if its just a one off use in a function that just returns a boolean? The only need for this is to run it through some for-loops to check for invalidations. The object will only be used once. Commented Apr 17, 2020 at 6:15
  • Why not have two different HashMap's, with two different keys. You can then check for all objects which have a key of "12" in one HashMap and a key of "A" in the second. Commented Apr 17, 2020 at 6:17
  • @NomadMaker only because I need to compare the subset of objects to each other, so everything with the same plan and relationship will be compared. This way its just a nested for-loop and I dont have to keep checking against the list for the correct object. Commented Apr 17, 2020 at 6:24
  • @BStill: I personally do it independent on how often an object will be used. The goal for me is code that's easy to understand for another developer (who can't know my thoughts). Easiest way to achieve that (without loads of comments in the source) is to give "things" meaningful names (which is quite hard sometimes). If you want to restrict your object you could use a static nested class for that purpose. So other developers will immediately know that this class/object only has 1 purpose. But I think we are in an object oriented language with strong typisation. So let's use that :-) Commented Apr 17, 2020 at 6:57

1 Answer 1

3

You can group twice, with the "relationship" grouping collector being the downstream to the "plan" grouping collector:

Map<String, Map<String, List<myObject>> myHash = objectList
    .stream()
    .collect(Collectors.groupingBy(myObject::getPlan, 
                Collectors.groupingBy(myObject::getRelationship)));

That will make "relationship" groups under each "plan" group.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.