1

if i make a map like this:

Map<Object, ArrayList<Object>> multiMap = new HashMap<Object, ArrayList<Object>>();

is there a way for me to sort on the values in the ArrayList<Object>?

i was thinking of just looping through the multimap as such:

for (Entry<Object, ArrayList<Object>> entry : multiMap.entrySet()) {
    for (int i = 0; i < entry.size(); i++) {
           //retrieve all array items and place in new array to sort
    }

so we have object ---> arrayList (contains 2 elements) object ---> arrayList (contains 2 elements) object ---> arrayList (contains 2 elements) object ---> arrayList (contains 2 elements)

i want to take all the elements in all of the arrayLists above and sort by those. it doesn't need to stay in the multimap.. i was just looking to see if there was a better way to do it..

EDIT: the point here is to sort all the array elements in every arrayList against all the other array elements in every other arrayList.... so it's not as simple as just calling sort but i'm wondering if there is a better/cleaner way of doing this? i'm not all that familiar with maps... any help would be appreciated..

BigBug
  • 6,202
  • 23
  • 87
  • 138
  • 2
    `Collections.sort()` - try this out. – Rahul Mar 19 '13 at 14:08
  • @R.J but i want to sort all the arrayLists against one another... like each value in all the arrays against every other value in all the other arrayLists... so it's not that simple.. – BigBug Mar 19 '13 at 14:10
  • @BlueMonster: it's not very clear what exactly you want. Can you provide some sample data and how you can to sort it? (Also: this data structure hints at possible [object denial](http://stackoverflow.com/a/3725728/40342)). – Joachim Sauer Mar 19 '13 at 14:11
  • so you want to reorder all the arrays in your map? could you be a little more clear. maybe post more code or an example with the expected output – Aboutblank Mar 19 '13 at 14:11

3 Answers3

1

Based on your edit, you seem to want to get all the Objects in your Map and sort them. There is not a direct way to sort the values in a map together so to sort all the items, the easiest would be to consolidate them into one list and Collections.sort() the whole thing.

public ArrayList<Object> sortAllValues() {
    ArrayList<Object> allEntries = new ArrayList<Object>();
    for (Entry<Object, ArrayList<Object>> entry : multiMap.entrySet())
        allEntries.addAll(entry.getValue());
    }
return Collections.sort(allEntries);
}

You will still need to implement Comparable to your Object if it does not have it. This is a good guide on how to do implement Comparable

Aboutblank
  • 697
  • 3
  • 14
  • 31
0

Actually you should not care that the ArrayList is the value in a Map this does not matter. You would sort this ArrayList<Object> just the way you would sort any such list. Problem here is that you will have to implement some kind of Comparator that casts the Object to the needed type. In general I would personally make the ArrayList of more specific type than Object to make sorting easier.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
0

Check out the MultiMap implementations of Apache Commons and Google Collections. They might have one that already supports sorting/ordering.

NilsH
  • 13,705
  • 4
  • 41
  • 59