具有重复键的映射实现 [英] Map implementation with duplicate keys

查看:39
本文介绍了具有重复键的映射实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个带有重复键的地图.

I want to have a map with duplicate keys.

我知道有很多地图实现(Eclipse 向我展示了大约 50 个),所以我敢打赌一定有一个允许这样做.我知道编写自己的地图来执行此操作很容易,但我更愿意使用一些现有的解决方案.

I know there are many map implementations (Eclipse shows me about 50), so I bet there must be one that allows this. I know it's easy to write your own map that does this, but I would rather use some existing solution.

也许是在 commons-collections 或 google-collections 中?

Maybe something in commons-collections or google-collections?

推荐答案

您正在寻找一个多重映射,实际上 commons-collections 和 Guava 都有几个实现.多重映射通过维护每个键的值集合来允许多个键,即您可以将单个对象放入映射中,但您检索一个集合.

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.

如果你可以使用 Java 5,我更喜欢 Guava 的 Multimap 因为它是泛型感知的.

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

这篇关于具有重复键的映射实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆