使用重复键映射实现 [英] Map implementation with duplicate keys

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

问题描述

我想要有一个重复键的地图,我知道有很多地图实现(eclipse显示我约50),所以我敢打赌必须有一个允许这个。我知道它很容易写出你自己的地图这样做,但我宁愿使用一些现有的解决方案。也许是在commons集合或谷歌集合中的东西?

解决方案

你正在寻找一个多重映射,番石榴有几个实现。 Multimaps允许通过维护每个键的值的集合来保存多个键,即您可以将单个对象放入地图,但是您可以检索一个集合。



如果可以使用Java 5,我宁愿Guava的 Multimap ,因为它是泛型感知的。


I want to have 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 its easy to write your own Map that does this, but i would rather use some existing solution. Maybe something in commons-collections or google-collections?

解决方案

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.

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

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