1

I'm working on a project where i need to store key/values having multiple duplicate keys. Though there are many new implementations like apache commons and guava, they are mostly based on single key with multiple values as like below.

Map<Integer, List<String>>

My need is to have duplicate keys along with a capability to sort the map by values rather than by keys. It means, a map data structure which is completely based on position and sorting by values.

Please do let me know if there is any existing implementations or any ideas to implement the same.

  • 3
    It sounds like you're asking 2 question: 1) [How to create a map with duplicate keys?](http://stackoverflow.com/questions/1062960/map-implementation-with-duplicate-keys) 2) [How to sort a map by value?](http://stackoverflow.com/questions/109383/sort-a-mapkey-value-by-values-java) – shmosel Nov 15 '16 at 05:33
  • 1
    if you separate it as two questions, then there are many answers already available for it.... but i need the map with duplicate keys to be sorted by values... – Ashwini Narayana Murthy Nov 15 '16 at 05:37
  • By "capability to sort the map", do you mean that you iterate often, so it has to maintain a sorted order, or do you just mean you iterate occasionally, so performance of sorting at beginning of an iteration is ok? – Andreas Nov 15 '16 at 05:37
  • Ahhh, so you are [asking us to recommend or find a software library](http://stackoverflow.com/help/on-topic)? Sorry, that is off-topic for StackOverflow (see #4). – Andreas Nov 15 '16 at 05:39
  • 7
    If you want to allow duplicate keys - which value do you want to retrieve when you request the value for a key? If you don't want to retrieve a value by key, then you don't want a Map. A List of tuples would be good enough - and you can sort that list any way you want by implementing a Comparator. – Erwin Bolwidt Nov 15 '16 at 05:42
  • it is not recommendation. But I have been looking for such an implementation as solution. I thought i would be getting some pointers or direction on any existing libraries or code logic to implement it. – Ashwini Narayana Murthy Nov 15 '16 at 05:46
  • Thanks for the awesome direction @ErwinBolwidt I just needed what you suggested. Implemented successfully!! – Ashwini Narayana Murthy Nov 15 '16 at 07:35

2 Answers2

1

I this case you should create your own Map class and write code by your own way.

Or you can simply copy the code from HashMap class to your class and then make changes in that code to work by your way. You can find the code of HashMap class from src.zip file which will be present inside your JDK installation directory.

Naresh Joshi
  • 4,188
  • 35
  • 45
0

Create a custom class with your key as a property and some identifier to determine whether two objects of that class are same....Override equals and hashcode method of your custom class and use objects of your custom class as the key of your map.....the value can be the value corresponding to the original key.....the trick here is to come up with optimal hashcode and equals implementations to ensure less collisions...

prashant
  • 1,382
  • 1
  • 13
  • 19