Map implementation with duplicate keys

后端 未结 18 1947
暗喜
暗喜 2020-11-22 15:23

I want to have a map with duplicate keys.

I know there are many map implementations (Eclipse shows me about 50), so I bet there must be one that allows this. I know

18条回答
  •  礼貌的吻别
    2020-11-22 15:55

    You are searching for a multimap, and indeed both commons-collections and Guava have several implementations for that. Multimaps allow for multiple keys by maintaining a collection of values per key, i.e. you can put a single object into the map, but you retrieve a collection.

    If you can use Java 5, I would prefer Guava's Multimap as it is generics-aware.

提交回复
热议问题