I have an entity that needs to be grouped several times by three of their properties, which enable a finer selection from left to right. Let's say I have this class:
public class Person {
private String gender;
private String state;
private String age;
}
Each property can hold the same value multiple times and I would like to group them accordingly. So that in the end I have the following data structure:
m --> France --> 20 --> Person1
Person2
21 --> Person3
Person4
Germany --> 20 --> Person5
...
I hope you get the idea. Currently I use a deeply nested Map
construction to achieve this: Map<String, Map<String, Map<String, Person>>>
Then I check for existing keys as in this answer: Group by field name in Java for each level of the Map and finally add my object to the "deepest" map.
This should work although not tested yet, but is there any better way to do this? As in better readable or faster execution
Since it is only one for
loop the execution time should be O(n) which is quite alright, but still, this does not seem to be the best solution.