Is there a sorted java collection which handles duplicates?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 21:57:20

问题


I need a collection that behaves something like C++ multimap, but I also need to be able to get elements by a range of keys.


回答1:


There is no built-in multimap collection in Java. To solve this you can map to every key a list of values: Map<String, List<String>>, for example. Otherwise there are third-party libraries with implemented multimaps - here is one of them.




回答2:


You can look into Google Collections. It has multiple implementations for MultiMap.




回答3:


There is a simple hack around creating multimap sortable collections in java...Use the dataset TreeMap and for keys enter key*10^4+counter. This way you are storing duplicate key values in the map (by adding counter they are actually not duplicates, so you can store the in treeMap, but you know not to use the last four digits of the integer key values), however your dataset is being sorted using your original key values. Note that depending how large is your dataset you might want to adjust 10^n to make sure that it is larger then the number of entries in your data.



来源:https://stackoverflow.com/questions/2658356/is-there-a-sorted-java-collection-which-handles-duplicates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!