Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Class that implements Map and keeps insertion order?

I'm looking for a class in java that has key-value association, but without using hashes. Here is what I'm currently doing:

  1. Add values to a Hashtable.
  2. Get an iterator for the Hashtable.entrySet().
  3. Iterate through all values and:
    1. Get a Map.Entry for the iterator.
    2. Create an object of type Module (a custom class) based on the value.
    3. Add the class to a JPanel.
  4. Show the panel.

The problem with this is that I do not have control over the order that I get the values back, so I cannot display the values in the a given order (without hard-coding the order).

I would use an ArrayList or Vector for this, but later in the code I need to grab the Module object for a given Key, which I can't do with an ArrayList or Vector.

Does anyone know of a free/open-source Java class that will do this, or a way to get values out of a Hashtable based on when they were added?

Thanks!

like image 479
Shane Avatar asked Mar 25 '09 21:03

Shane


People also ask

How do you keep an insertion order on a map?

This class extends HashMap and maintains a linked list of the entries in the map, in the order in which they were inserted. This allows insertion-order iteration over the map. That is, when iterating a LinkedHashMap, the elements will be returned in the order in which they were inserted.

Does java map preserve order?

HashMap does not maintains insertion order in java. Hashtable does not maintains insertion order in java. LinkedHashMap maintains insertion order in java. TreeMap is sorted by natural order of keys in java.

Which map maintains the order of entry in java?

Java TreeMapA TreeMap is a Map that maintains its entries in ascending order, sorted according to the keys' natural ordering, or according to a Comparator provided at the time of the TreeMap constructor argument. The TreeMap class is efficient for traversing the keys in a sorted order.

Does LinkedHashMap maintain insertion order?

It maintains a linkedlist of the entries in the map in the order in which they were inserted. This helps to maintain iteration order and elements will be returned in the order they were first added in.


2 Answers

I suggest a LinkedHashMap or a TreeMap. A LinkedHashMap keeps the keys in the order they were inserted, while a TreeMap is kept sorted via a Comparator or the natural Comparable ordering of the elements.

Since it doesn't have to keep the elements sorted, LinkedHashMap should be faster for most cases; TreeMap has O(log n) performance for containsKey, get, put, and remove, according to the Javadocs, while LinkedHashMap is O(1) for each.

If your API that only expects a predictable sort order, as opposed to a specific sort order, consider using the interfaces these two classes implement, NavigableMap or SortedMap. This will allow you not to leak specific implementations into your API and switch to either of those specific classes or a completely different implementation at will afterwards.

like image 188
Michael Myers Avatar answered Sep 17 '22 03:09

Michael Myers


LinkedHashMap will return the elements in the order they were inserted into the map when you iterate over the keySet(), entrySet() or values() of the map.

Map<String, String> map = new LinkedHashMap<String, String>();  map.put("id", "1"); map.put("name", "rohan"); map.put("age", "26");  for (Map.Entry<String, String> entry : map.entrySet()) {     System.out.println(entry.getKey() + " = " + entry.getValue()); } 

This will print the elements in the order they were put into the map:

id = 1 name = rohan  age = 26  
like image 37
Praveen Kishor Avatar answered Sep 20 '22 03:09

Praveen Kishor