0

I am trying to get values from an ArrayList that is sorted and want to store it in a HashMap, where the values of the ArrayList become keys of the HashMap. Will the order of the values in the HashMap still be the same as that of ArrayList?

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
sdwaraki
  • 382
  • 1
  • 4
  • 12
  • 2
    You can use a LinkedHashMap or a TreeMap depending on your use case – assylias Mar 20 '14 at 04:52
  • TreeMap is the one. Thanks – sdwaraki Mar 20 '14 at 04:56
  • A HashMap is not guaranteed to hold the sort order. If you want to maintain the order in which they were inserted into the arraylist. I would recommend LinkedHashMap over TreeMap. Reference : http://stackoverflow.com/a/2889800/919858 – seeker Mar 20 '14 at 05:19

4 Answers4

2

No. Use a TreeMap instead. This will preserve the order of insertion.

jhn
  • 371
  • 2
  • 8
  • I was not aware of TreeMap data structure. Now I just read the documentation for it. Thank you. – sdwaraki Mar 20 '14 at 04:56
  • Note that TreeMap only preserves order of insertion *if the order of insertion is sorted order*. It is in this case, but it's not an insignificant detail. – Andreas Troelsen Mar 20 '14 at 05:06
  • @AndreasTroelsen He explicitly said that the ArrayList was sorted. – jhn Mar 20 '14 at 05:11
  • 1
    @jhn Read my comment again. I acknowledge this specific use case. The TreeMap will preserve the order of insertion in this specific case, but as I pointed out in my answer, the TreeMap may add unnecessary complexity, and it *requires* a Comparator identical to the one used to sort the ArrayList (if it wasn't sorted due to the natural ordering). My comment was directed at the lack of possibly significant information, not at correctness. – Andreas Troelsen Mar 20 '14 at 05:18
1

HashMap makes no guarantees as to the order the mappings are stored or iterated, so simply running through the ArrayList and putting them into the HashMap as keys will very likely result in unordered iterations.

As others have pointed out, LinkedHashMap does preserve insertion order for iterations. An additional run of insertions will result in unordered iterations again, though. Both HashMap and LinkedHashMap support constant time lookup - LinkedHashMap pays for its extra feature in space (by maintaining pointers between the keys).

As others have also pointed out, TreeMap preserves order after updates, so this might be a better option for you, or not. Of course, if the ArrayList is sorted with a specific Comparator, you must feed that same Comparator to the TreeMap on construction for the sorting to be the same. Note that TreeMap does not have constant time lookup, due to being implemented as a Red-Black search tree.

0

As your ArrayList has been ordered, no need to use a TreeMap because this will compare to order again and it's not necessary. You should use a LinkedHashMap that will keep the exact order of your ArrayList when you put your value in.

binhdv83
  • 21
  • 2
-1

Check This: Insert Values of ArrayList into HashMap

HashMap<String, Item> itemMap = new HashMap<String, Item>();

for (Item item : itemList) 
{
   itemMap.put(item.getitemCode(), item);
}
Surya Rawat
  • 101
  • 3
  • 13
  • This does not answer the question. Also, OP specifically said *values of the ArrayList become keys of the HashMap*, but with this code, the values of the list become values of the map. – Andreas Troelsen Mar 20 '14 at 05:11