I know that ArrayList provide ability to get item at position get(position), and LinkedHashTable provide sorting, but it not able to get item at position. So, question: is any generic collection in java with features:
-
sorting
-
get by position
-
provide Key/Value generic type.
Please, give me code example of listed features, if required generic exists.
I apologize for over complicating things earlier!
Use a TreeMap. When you want to
just do the following:
==========================
My old over complex incorrect answer from before:
You can use an
ArrayList<Pair<K implements Comparable,V>>where you implement comparable on thePair<K,V>and K has Comparable implemented. Then you can useCollections.sort(List<Pair<K,V>)on the Arraylist.Then you would maintain a
HashMap<K,V>so that you can retrieve elements by K. That means you will have to remember to update both data structures. That means O(N) to add an element 🙁 . Also, to simplify the logic, you would wrap the HashMap and ArrayList in an object.