0

I have a ConcurrentHashMap , everytime I put data into it,it will be sorted in some custom order so I don't need to sort the map manully .

so how to implement that ?

Thanks

rellocs wood
  • 1,381
  • 4
  • 21
  • 36
  • 1
    *The table is internally partitioned to try to permit the indicated number of concurrent updates without contention. Because placement in hash tables is essentially random, ...* – Scary Wombat Nov 05 '18 at 08:16
  • You can't sort a hash map, or a concurrent hash map. The easiest thing to do is synchronize a [tree map](https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html). Performance will probably be worse than a concurrent hash map. – Michael Nov 05 '18 at 08:19
  • Collections in Java can only be sorted by key. To sort by value in one collection you need to add it as the key of a sorted "secondary index" e.g. a ConcurrentSkipListMap. – Peter Lawrey Nov 05 '18 at 08:21
  • Use `ConcurrentSkipListMap` it is probably what you need – Krzysztof Cichocki Nov 05 '18 at 08:30

0 Answers0