I'm looking for a class in java that has key-value association, but without using hashes. Here is what I'm currently doing:
I suggest a
LinkedHashMap or 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;
O(log n) performance for
remove, according to the Javadocs, while
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,
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.