0

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.

Community
  • 1
  • 1
Sebastian Wramba
  • 10,087
  • 8
  • 41
  • 58
  • 1
    I think it is a good solution. If you know those mapping properties you can add and find an object very easily. Of course if you have an Id and you need to find the object, you have to iterate over the whole tree. Of course this is not more complex than iterating over a list. – Piotr Gwiazda Jan 23 '12 at 15:56
  • Agree with Peter. If you have to do it with Lists, then you will have to sort the list by Comparator on first parameter. Then each subset of that sorted list using second parameter and so on. Doesn't look that elegant. Nested TreeMap looks a better approach to me. http://docs.oracle.com/javase/6/docs/api/java/util/TreeMap.html – Bhushan Jan 23 '12 at 16:01
  • Alright, thank you. fmucar is right if you don't actually need the hierarchical structure - I could just sort the entries the way I want. And if you need the hierarchy my approach does the job - am I right? – Sebastian Wramba Jan 23 '12 at 16:07

2 Answers2

0

If you don't need the hierarchy you can use a list and sort it 3 times in reverse order of criteria. E.g. you want it ordered by state, age and then gender then you need to sort by gender first, then by age and then by state.

Ensure that your sorting algorithm is stable so that the sort order of equal valued entries is not changed.

If you use the Map, better use more explicit types then String. Age should be an integer type and gender an enum. State can be an enum or String, depending on how many you have and how much effort you want to invest.

Konrad Höffner
  • 11,100
  • 16
  • 60
  • 118
0

You can implement a custom Comparator that compares only properties you need and skipping others. Then you can use Collections.sort(list, comparator) method to sort the object

fmucar
  • 14,361
  • 2
  • 45
  • 50