在 Java 中使用哪个集合?

发布于 2024-09-29 15:39:49 字数 365 浏览 2 评论 0原文

我想将整数映射到字符串,它们是一对一的,例如:

60 : c
61 : c#
62 : d
63 : d#
64 : e
65 : f
66 : f#

但我需要能够执行以下操作:

  1. 从键获取值:“c”= getValue(60) [给它一个键60、返回字符串值]
  2. 从值中获取键: 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 :

  1. Get a value from a key : "c" = getValue(60) [ give it a key 60, return string value ]
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

落日海湾 2024-10-06 15:39:49

Commons CollectionsGoogle Guava 都有一个双向地图。

Commons Collections and Google Guava both have a bi-directional map.

清音悠歌 2024-10-06 15:39:49

如果您不想包含公共集合或番石榴,只需使用两个单独的映射 - 您可以使用简单的 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.

究竟谁懂我的在乎 2024-10-06 15:39:49

你需要的是 BiMap。谷歌番石榴库提供了这个集合。

what you need is a BiMap. google guava library provides this collection.

躲猫猫 2024-10-06 15:39:49

你好
我想插入几件事
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

朮生 2024-10-06 15:39:49

如果这些是具有一组固定键值对的音符,那么您可以考虑将 enum 与反向查找映射一起使用。您最终仍然会得到两个 HashMap,但至少它们在枚举中连接在一起。

enum Note {
    C("C",60), CSHARP("C#",61), D("D",62), DSHARP("D#",63); //etc..
    final int value;
    final String symbol;
    static final Map<Integer, Note> lookup = new HashMap<Integer, Note>();
    static {
        for(Note n : Note.values()) {
            lookup.put(n.value, n);
        }
    }

    Note(String symbol, int value) {
        this.symbol = symbol;
        this.value = value;
    }

    static Note get(int i) {
        return lookup.get(i);
    }
}

然后要获取值和符号,请使用这

System.out.println(Note.get(61));
System.out.println(Note.get(61).symbol);
System.out.println(Note.CSHARP.value);
System.out.println(Note.CSHARP.symbol);

将导致

CSHARP
C#
61
C#

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.

enum Note {
    C("C",60), CSHARP("C#",61), D("D",62), DSHARP("D#",63); //etc..
    final int value;
    final String symbol;
    static final Map<Integer, Note> lookup = new HashMap<Integer, Note>();
    static {
        for(Note n : Note.values()) {
            lookup.put(n.value, n);
        }
    }

    Note(String symbol, int value) {
        this.symbol = symbol;
        this.value = value;
    }

    static Note get(int i) {
        return lookup.get(i);
    }
}

Then to get the values and symbols, use this

System.out.println(Note.get(61));
System.out.println(Note.get(61).symbol);
System.out.println(Note.CSHARP.value);
System.out.println(Note.CSHARP.symbol);

which will result to

CSHARP
C#
61
C#

PS. Modifiers are removed for brevity.
PSS. Add the other lookup map for symbol.

秋千易 2024-10-06 15:39:49

在另一个论坛中找到了这个(链接文本

    public class ReversibleMap {
       private final Map keyToValue = new HashMap(8);
       private final Map valueToKey = new HashMap(8);

       public void put(Integer key, String value) {
           keyToValue.put(key, value);
           valueToKey.put(value, key);
       }

       public String get(Integer key) {
           return (String) keyToValue.get(key);
       }

       public Integer getKey(String value) {
           return (Integer) valueToKey.get(value);
      }
   }

这应该可以

found this in another forum (link text)

    public class ReversibleMap {
       private final Map keyToValue = new HashMap(8);
       private final Map valueToKey = new HashMap(8);

       public void put(Integer key, String value) {
           keyToValue.put(key, value);
           valueToKey.put(value, key);
       }

       public String get(Integer key) {
           return (String) keyToValue.get(key);
       }

       public Integer getKey(String value) {
           return (Integer) valueToKey.get(value);
      }
   }

This should do

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文