-1

I have this hashmap:

       HashMap<String, ArrayList<String>> allhotels = new HashMap<>();

I want to sort all the values in each Arraylist alphabetically. This is my current method:

public void sortHotels() {

    for(int i = 0; i < allhotels.size(); i++) {

        Collections.sort(allhotels.values().toArray(), String.CASE_INSENSITIVE_ORDER);
    }

}

I want to sort the strings in the ArrayList for every entry in the hashmap.

  • What do you want to sort? The keys of the HashMap? The Strings in each of the ArrayList values? It's not clear. – Eran Aug 22 '14 at 14:52
  • It depends on what you want to sort keys or values. You can use [Comparator](http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html). – Mandar Pandit Aug 22 '14 at 14:53
  • @Eran I want to sort the strings in the ArrayList for every entry in the hashmap. – user3456349 Aug 22 '14 at 14:53
  • 1
    The question should rather be "How to sort *lists that are values in* a HashMap". The map itself can not be sorted. – Marco13 Aug 22 '14 at 14:58

5 Answers5

6

You're not sorting the lists contained in the map. You're getting all the lists from the map as an array containing lists, and trying to sort this array of lists with a comparator which is used to sort Strings.

What you want is to sort each value of the map. So iterate over the values, and sort them:

for (List<String> list : allhotels.values()) {
    Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
2

This should do it:

for (List<String> value : allhotels.values())
{
    Collections.sort(value, String.CASE_INSENSITIVE_ORDER);
}

Just iterate over all values (each of the values being a list), and sort the list. The list is sorted in-place, so the sorting will be directly applied to the values of the map.

Marco13
  • 53,703
  • 9
  • 80
  • 159
0

Based on your answer to my comment, you need to sort each list separately :

for(ArrayList<String> list : allhotels.values()) {

    Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
}
Eran
  • 387,369
  • 54
  • 702
  • 768
0

toArray() Returns an array containing all of the elements in this collection. If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order. The returned array will be "safe" in that no references to it are maintained by this collection.

So any change on allhotels.values().toArray() will not be reflected to the allhotels map. Intended use of HashMap is not sorting, but still you can check from this topic to find some possibilities and ideas -and possibly the answer- here.

Community
  • 1
  • 1
Seyf
  • 889
  • 8
  • 16
0

If you need to get key too during iteration as mentioned on a comment for JB Nizet's answer, you can code as follows:

Iterator itr = allhotels.entrySet().iterator();
while(itr.hasNext()){
    Entry<String, ArrayList<String>> hotelEntry = (Entry<String, ArrayList<String>>)itr.next();
    String currentKey = hotelEntry.getKey();
    Collections.sort(hotelEntry.getValue(), String.CASE_INSENSITIVE_ORDER);
}
bijoshtj
  • 299
  • 4
  • 14