在Java中使用哪个集合?

时间:2021-06-10 19:03:13

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

  3. get key from a value : 65 = getKey("f") [ give it a string value "f", return a key ]
  4. 从值获取键:65 = getKey(“f”)[给它一个字符串值“f”,返回一个键]

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" ?

哪种收集模型最适合这种情况?我问,因为我看了几个,没有人可以做<2>部分。或者我是否必须编写代码来遍历每一对以查找哪个键的值为“f”?

Edit : Nothing in jdk1.6 does this ?

编辑:jdk1.6中没有这样做吗?

6 个解决方案

#1


0  

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

这应该做

#2


14  

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

Commons Collections和Google Guava都有双向地图。

#3


6  

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.

如果您不想包含commons集合或guava,只需使用两个单独的映射 - 您可以使用简单的foreach循环以编程方式从第一个映射创建反向映射。我建议您设计代码,以便对这些映射的所有访问都是单个类的内部(因为它是一个实现细节,应该从系统的其余部分隐藏)。换句话说,不要将这些映射传递给任何方法。然后,如果您以后选择,您可以非常轻松地切换到更合适的集合。

#4


5  

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

你需要的是一个BiMap。 google guava库提供此集合。

#5


1  

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

嗨,我想插入一些东西Java中的标准映射是单向工作的:给一个键获取对象我不知道java jdk1.6类就是这样做的

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 ^^

一些想法1)创建一个具有2个映射的类,其中一个与另一个,这不是很难实现^^

2) IF your datas are like your posted AND IF this is a predefined associative list why not try and enumeration ? or a ArrayList?

2)如果您的数据与发布的数据类似,如果这是预定义的关联列表,为什么不尝试和枚举?还是一个ArrayList?

Jason

#6


1  

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.

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

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.

PS。为简洁起见,删除了修饰符。 PSS。添加符号的其他查找映射。

#1


0  

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

这应该做

#2


14  

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

Commons Collections和Google Guava都有双向地图。

#3


6  

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.

如果您不想包含commons集合或guava,只需使用两个单独的映射 - 您可以使用简单的foreach循环以编程方式从第一个映射创建反向映射。我建议您设计代码,以便对这些映射的所有访问都是单个类的内部(因为它是一个实现细节,应该从系统的其余部分隐藏)。换句话说,不要将这些映射传递给任何方法。然后,如果您以后选择,您可以非常轻松地切换到更合适的集合。

#4


5  

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

你需要的是一个BiMap。 google guava库提供此集合。

#5


1  

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

嗨,我想插入一些东西Java中的标准映射是单向工作的:给一个键获取对象我不知道java jdk1.6类就是这样做的

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 ^^

一些想法1)创建一个具有2个映射的类,其中一个与另一个,这不是很难实现^^

2) IF your datas are like your posted AND IF this is a predefined associative list why not try and enumeration ? or a ArrayList?

2)如果您的数据与发布的数据类似,如果这是预定义的关联列表,为什么不尝试和枚举?还是一个ArrayList?

Jason

#6


1  

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.

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

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.

PS。为简洁起见,删除了修饰符。 PSS。添加符号的其他查找映射。