在 Java 中使用哪个集合?
我想将整数映射到字符串,它们是一对一的,例如:
60 : c
61 : c#
62 : d
63 : d#
64 : e
65 : f
66 : f#
但我需要能够执行以下操作:
- 从键获取值:“c”= getValue(60) [给它一个键60、返回字符串值]
- 从值中获取键: 65 = getKey("f") [给它一个字符串值“f”,返回一个键]
哪种集合模型最适合这种情况?我这样问是因为我已经看过其中的几个,没有一个可以做到 <2>部分。或者我是否必须编写代码来遍历每一对以查找哪个键具有值“f”?
编辑:jdk1.6 中没有这样做吗?
I want to map integers to strings, they are one-to-one such as :
60 : c
61 : c#
62 : d
63 : d#
64 : e
65 : f
66 : f#
But I need to have the ability to do the following :
- Get a value from a key : "c" = getValue(60) [ give it a key 60, return string value ]
- get key from a value : 65 = getKey("f") [ give it a string value "f", return a key ]
Which collection model is best suited for this ? I'm asking because I've looked at several of them, none can do the <2> part. Or do I have to write code to go through every pair to find which key has value "f" ?
Edit : Nothing in jdk1.6 does this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Commons Collections 和 Google Guava 都有一个双向地图。
Commons Collections and Google Guava both have a bi-directional map.
如果您不想包含公共集合或番石榴,只需使用两个单独的映射 - 您可以使用简单的 foreach 循环以编程方式从第一个映射创建反向映射。我建议设计您的代码,以便对这些映射的所有访问都在单个类的内部(因为它是一个实现细节,并且应该对系统的其余部分隐藏)。换句话说,不要将这些映射传递给任何方法。然后,如果您以后选择的话,可以很容易地切换到更合适的集合。
If you don't want to include commons collections or guava, just use two separate maps -- you can create the reverse mapping programmatically from the first with a simple foreach loop. I would suggest designing your code so that all the access to these maps is internal to a single class (since it's an implementation detail and should be hidden from the rest of the system). In other words, don't pass these maps to any methods. Then, you can very easily switch to a more appropriate collection should you later choose to.
你需要的是 BiMap。谷歌番石榴库提供了这个集合。
what you need is a BiMap. google guava library provides this collection.
你好
我想插入几件事
Java 中的标准映射是单向工作的:给出一个键,获取对象
我不知道 java jdk1.6 类可以实现
一些想法
1)创建一个类,其中有两个映射,一个与 ,另一个与 ,这并不难实现 ^^
2)如果您的数据与您发布的类似并且如果这是预定义的关联列表,为什么不尝试枚举?还是数组列表?
贾森
Hi
I'm would like to interject a couple of things
The standards maps in Java work unidirectionally: give a key get the object(s)
I'm not aware of a java jdk1.6 class that does it
a couple of ideas
1) create a class that has 2 maps one with the and the other with , this isn't so hard to implement ^^
2) IF your datas are like your posted AND IF this is a predefined associative list why not try and enumeration ? or a ArrayList?
Jason
如果这些是具有一组固定键值对的音符,那么您可以考虑将
enum
与反向查找映射一起使用。您最终仍然会得到两个 HashMap,但至少它们在枚举中连接在一起。然后要获取值和符号,请使用这
将导致
PS。为了简洁起见,删除了修饰符。
附言。添加符号的其他查找映射。
If these are musical notes with a fixed set of key-value pairs then you may consider using
enum
with a reverse-lookup map. You will still end up with two HashMaps but at least they are joined together within the enum.Then to get the values and symbols, use this
which will result to
PS. Modifiers are removed for brevity.
PSS. Add the other lookup map for symbol.
在另一个论坛中找到了这个(链接文本)
这应该可以
found this in another forum (link text)
This should do