如何创建带有两个键(键对、值)的HashMap ?

时间:2021-11-27 13:59:30

I have a 2D array of Integers. I want them to be put into a HashMap. But I want to access the elements from the HashMap based on Array Index. Something like:

我有一个二维的整数数组。我希望它们被放到一个HashMap中。但是我想要访问基于数组索引的HashMap中的元素。喜欢的东西:

For A[2][5], map.get(2,5) which returns a value associated with that key. But how do I create a hashMap with a pair of keys? Or in general, multiple keys: Map<((key1, key2,..,keyN), Value) in a way that I can access the element with using get(key1,key2,...keyN).

对于A[2][5], map.get(2,5)返回与该键相关联的值。但是如何用一对键创建hashMap呢?或者一般来说,多个键:Map<((key1, key2,..,keyN),值),我可以用get(key1,key2,…keyN)访问元素。

EDIT : 3 years after posting the question, I want to add a bit more to it

编辑:在发布这个问题3年后,我想再增加一点。

I came across another way for NxN matrix.

我找到了NxN矩阵的另一种方法。

Array indices, i and j can be represented as a single key the following way:

数组下标i和j可以表示为单个键:

int key = i * N + j;
//map.put(key, a[i][j]); // queue.add(key); 

And the indices can be retrevied from the key in this way:

这些指标可以用这种方法从关键字中得到修改:

int i = key / N;
int j = key % N;

11 个解决方案

#1


159  

There are several options:

有几种选择:

2 dimensions

Map of maps

Map<Integer, Map<Integer, V>> map = //...
//...

map.get(2).get(5);

Wrapper key object

public class Key {

    private final int x;
    private final int y;

    public Key(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Key)) return false;
        Key key = (Key) o;
        return x == key.x && y == key.y;
    }

    @Override
    public int hashCode() {
        int result = x;
        result = 31 * result + y;
        return result;
    }

}

Implementing equals() and hashCode() is crucial here. Then you simply use:

在这里实现equals()和hashCode()是非常重要的。那么可以直接使用:

Map<Key, V> map = //...

and:

和:

map.get(new Key(2, 5));

Table from Guava

Table<Integer, Integer, V> table = HashBasedTable.create();
//...

table.get(2, 5);

Table uses map of maps underneath.

表格使用地图下面的地图。

N dimensions

Notice that special Key class is the only approach that scales to n-dimensions. You might also consider:

注意,特殊的键类是扩展到n维的唯一方法。你也可以考虑:

Map<List<Integer>, V> map = //...

but that's terrible from performance perspective, as well as readability and correctness (no easy way to enforce list size).

但是从性能的角度来看,这是很糟糕的,而且可读性和正确性(没有简单的方法来执行列表大小)。

Maybe take a look at Scala where you have tuples and case classes (replacing whole Key class with one-liner).

也许您可以看看Scala,那里有tuple和case类(用一行代码替换整个键类)。

#2


15  

When you create your own key pair object, you should face a few thing.

当您创建自己的密钥对对象时,您应该面对一些事情。

First, you should be aware of implementing hashCode() and equals(). You will need to do this.

首先,您应该知道实现hashCode()和equals()。你需要这样做。

Second, when implementing hashCode(), make sure you understand how it works. The given user example

其次,在实现hashCode()时,确保您理解它是如何工作的。给定用户的例子

public int hashCode() {
    return this.x ^ this.y;
}

is actually one of the worst implementations you can do. The reason is simple: you have a lot of equal hashes! And the hashCode() should return int values that tend to be rare, unique at it's best. Use something like this:

实际上是最糟糕的实现之一。原因很简单:你有很多相同的哈希!hashCode()应该返回值,这些值往往是很少的,唯一的是最好的。使用这样的:

public int hashCode() {
  return (X << 16) + Y;
}

This is fast and returns unique hashes for keys between -2^16 and 2^16-1 (-65536 to 65535). This fits in almost any case. Very rarely you are out of this bounds.

这是快速和回报之间独特的散列键2 ^ 16和2 ^ 16:1(-65536 - 65535)。这几乎适用于任何情况。你很少跳出这个界限。

Third, when implementing equals() also know what it is used for and be aware of how you create your keys, since they are objects. Often you do unnecessary if statements cause you will always have the same result.

第三,当实现equals()时,也要知道它的用途,并知道如何创建键,因为它们是对象。通常情况下,你会做不必要的陈述,因为你总是会得到同样的结果。

If you create keys like this: map.put(new Key(x,y),V); you will never compare the references of your keys. Cause everytime you want to acces the map, you will do something like map.get(new Key(x,y));. Therefore your equals() does not need a statement like if (this == obj). It will never occure.

如果你创建了这样的键:map。把(新键(x,y),V);你永远不会比较你的钥匙的参考资料。因为每次你想看地图的时候,你都会做一些类似地图的事情。(新键(x,y));。因此,您的equals()不需要像if (this == obj)这样的语句。它永远不会发生血。

Instead of if (getClass() != obj.getClass()) in your equals() better use if (!(obj instanceof this)). It will be valid even for subclasses.

在您的equals()中,如果(!(obj instanceof))。它甚至对于子类也是有效的。

So the only thing you need to compare is actually X and Y. So the best equals() implementation in this case would be:

所以你唯一需要比较的是X和y,所以在这个例子中最好的等于()的实现是:

public boolean equals (final Object O) {
  if (!(O instanceof Key)) return false;
  if (((Key) O).X != X) return false;
  if (((Key) O).Y != Y) return false;
  return true;
}

So in the end your key class is like this:

最后,你的关键课程是这样的:

public class Key {

  public final int X;
  public final int Y;

  public Key(final int X, final int Y) {
    this.X = X;
    this.Y = Y;
  }

  public boolean equals (final Object O) {
    if (!(O instanceof Key)) return false;
    if (((Key) O).X != X) return false;
    if (((Key) O).Y != Y) return false;
    return true;
  }

  public int hashCode() {
    return (X << 16) + Y;
  }

}

You can give your dimension indices X and Y a public access level, due to the fact they are final and do not contain sensitive information. I'm not a 100% sure whether private access level works correctly in any case when casting the Object to a Key.

您可以给您的维度索引X和Y一个公共访问级别,因为它们是最终的,并且不包含敏感信息。在将对象转换为键时,我不是100%确定私有访问级别是否正确工作。

If you wonder about the finals, I declare anything as final which value is set on instancing and never changes - and therefore is an object constant.

如果你想知道期末考试的情况,我宣布任何东西作为最终的值,它的值是设定在实例上的,并且不会改变——因此是一个对象常量。

#3


5  

You can't have an hash map with multiple keys, but you can have an object that takes multiple parameters as the key.

您不能有一个带有多个键的散列映射,但是您可以有一个对象,它将多个参数作为键。

Create an object called Index that takes an x and y value.

创建一个名为Index的对象,它使用x和y值。

public class Index {

    private int x;
    private int y;

    public Index(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public int hashCode() {
        return this.x ^ this.y;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Index other = (Index) obj;
        if (x != other.x)
            return false;
        if (y != other.y)
            return false;
        return true;
    }
}

Then have your HashMap<Index, Value> to get your result. :)

然后使用HashMap 来得到结果。:) ,值>

#4


3  

Two possibilities. Either use a combined key:

两种可能。使用组合键:

class MyKey {
    int firstIndex;
    int secondIndex;
    // important: override hashCode() and equals()
}

Or a Map of Map:

或地图地图:

Map<Integer, Map<Integer, Integer>> myMap;

#5


3  

Implemented in common-collections [MultiKeyMap] (https://commons.apache.org/proper/commons-collections/javadocs/api-release/org/apache/commons/collections4/map/MultiKeyMap.html)

在公共集合中实现[MultiKeyMap] (https://commons.apache.org/proper/commons-collections/javadocs/api-release/org/apache/commons/collections4/map/MultiKeyMap.html)

#6


1  

Create a value class that will represent your compound key, such as:

创建一个表示复合键的值类,例如:

class Index2D {
  int first, second;

  // overrides equals and hashCode properly here
}

taking care to override equals() and hashCode() correctly. If that seems like a lot of work, you might consider some ready made generic containers, such as Pair provided by apache commons among others.

注意重载equals()和hashCode()正确。如果这看起来像很多工作,您可能会考虑一些现成的通用容器,比如apache commons提供的一对。

There are also many similar questions here, with other ideas, such as using Guava's Table, although allows the keys to have different types, which might be overkill (in memory use and complexity) in your case since I understand your keys are both integers.

这里也有许多类似的问题,比如使用Guava的表,虽然允许键有不同的类型,但在您的情况下可能会被过度使用(内存使用和复杂性),因为我理解您的密钥都是整数。

#7


1  

If they are two integers you can try a quick and dirty trick: Map<String, ?> using the key as i+"#"+j.

如果它们是两个整数,那么您可以尝试一个快速和下流的技巧:Map ,>

If the key i+"#"+j is the same as j+"#"+i try min(i,j)+"#"+max(i,j).

如果键我+“#”+ j一样+“#”+我尝试min(i,j)+“#”+马克斯(i,j)。

#8


1  

You could create your key object something like this:

你可以这样创建你的关键对象:

public class MapKey {

公开课MapKey {

public  Object key1;
public Object key2;

public Object getKey1() {
    return key1;
}

public void setKey1(Object key1) {
    this.key1 = key1;
}

public Object getKey2() {
    return key2;
}

public void setKey2(Object key2) {
    this.key2 = key2;
}

public boolean equals(Object keyObject){

    if(keyObject==null)
        return false;

    if (keyObject.getClass()!= MapKey.class)
        return false;

    MapKey key = (MapKey)keyObject;

    if(key.key1!=null && this.key1==null)
        return false;

    if(key.key2 !=null && this.key2==null)
        return false;

    if(this.key1==null && key.key1 !=null)
        return false;

    if(this.key2==null && key.key2 !=null)
        return false;

    if(this.key1==null && key.key1==null && this.key2 !=null && key.key2 !=null)
        return this.key2.equals(key.key2);

    if(this.key2==null && key.key2==null && this.key1 !=null && key.key1 !=null)
        return this.key1.equals(key.key1);

    return (this.key1.equals(key.key1) && this.key2.equals(key2));
}

public int hashCode(){
    int key1HashCode=key1.hashCode();
    int key2HashCode=key2.hashCode();
    return key1HashCode >> 3 + key2HashCode << 5;
}

}

}

The advantage of this is: It will always make sure you are covering all the scenario's of Equals as well.

这样做的好处是:它将始终确保覆盖所有的场景。

NOTE: Your key1 and key2 should be immutable. Only then will you be able to construct a stable key Object.

注意:您的key1和key2应该是不可变的。只有这样,您才能构建一个稳定的密钥对象。

#9


1  

we can create a class to pass more than one key or value and the object of this class can be used as a parameter in map.

我们可以创建一个类来传递多个键或值,这个类的对象可以作为映射中的参数使用。

import java.io.BufferedReader; 
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

 public class key1 {
    String b;
    String a;
    key1(String a,String b)
    {
        this.a=a;
        this.b=b;
    }
  }

public class read2 {

private static final String FILENAME = "E:/studies/JAVA/ReadFile_Project/nn.txt";

public static void main(String[] args) {

    BufferedReader br = null;
    FileReader fr = null;
    Map<key1,String> map=new HashMap<key1,String>();
    try {

        fr = new FileReader(FILENAME);
        br = new BufferedReader(fr);

        String sCurrentLine;

        br = new BufferedReader(new FileReader(FILENAME));

        while ((sCurrentLine = br.readLine()) != null) {
            String[] s1 = sCurrentLine.split(",");
            key1 k1 = new key1(s1[0],s1[2]);
            map.put(k1,s1[2]);
        }
        for(Map.Entry<key1,String> m:map.entrySet()){  
            key1 key = m.getKey();
            String s3 = m.getValue();
               System.out.println(key.a+","+key.b+" : "+s3);  
              }  
  //            }   
        } catch (IOException e) {

        e.printStackTrace();

    } finally {

        try {

            if (br != null)
                br.close();

            if (fr != null)
                fr.close();

        } catch (IOException ex) {

            ex.printStackTrace();

        }

    }

    }

 }

#10


0  

Use a Pair as keys for the HashMap. JDK has no Pair, but you can either use a 3rd party libraray such as http://commons.apache.org/lang or write a Pair taype of your own.

使用一对作为HashMap的键。JDK没有对,但是您可以使用第三方libraray,比如http://commons.apache.org/lang或您自己的一对taype。

#11


0  

You can also use guava Table implementation for this.

您还可以使用guava表实现。

Table represents a special map where two keys can be specified in combined fashion to refer to a single value. It is similar to creating a map of maps.

表表示一个特殊的映射,其中两个键可以以组合方式指定,以引用单个值。它类似于创建地图地图。

//create a table
  Table<String, String, String> employeeTable = HashBasedTable.create();

  //initialize the table with employee details
  employeeTable.put("IBM", "101","Mahesh");
  employeeTable.put("IBM", "102","Ramesh");
  employeeTable.put("IBM", "103","Suresh");

  employeeTable.put("Microsoft", "111","Sohan");
  employeeTable.put("Microsoft", "112","Mohan");
  employeeTable.put("Microsoft", "113","Rohan");

  employeeTable.put("TCS", "121","Ram");
  employeeTable.put("TCS", "122","Shyam");
  employeeTable.put("TCS", "123","Sunil");

  //get Map corresponding to IBM
  Map<String,String> ibmEmployees =  employeeTable.row("IBM");

#1


159  

There are several options:

有几种选择:

2 dimensions

Map of maps

Map<Integer, Map<Integer, V>> map = //...
//...

map.get(2).get(5);

Wrapper key object

public class Key {

    private final int x;
    private final int y;

    public Key(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Key)) return false;
        Key key = (Key) o;
        return x == key.x && y == key.y;
    }

    @Override
    public int hashCode() {
        int result = x;
        result = 31 * result + y;
        return result;
    }

}

Implementing equals() and hashCode() is crucial here. Then you simply use:

在这里实现equals()和hashCode()是非常重要的。那么可以直接使用:

Map<Key, V> map = //...

and:

和:

map.get(new Key(2, 5));

Table from Guava

Table<Integer, Integer, V> table = HashBasedTable.create();
//...

table.get(2, 5);

Table uses map of maps underneath.

表格使用地图下面的地图。

N dimensions

Notice that special Key class is the only approach that scales to n-dimensions. You might also consider:

注意,特殊的键类是扩展到n维的唯一方法。你也可以考虑:

Map<List<Integer>, V> map = //...

but that's terrible from performance perspective, as well as readability and correctness (no easy way to enforce list size).

但是从性能的角度来看,这是很糟糕的,而且可读性和正确性(没有简单的方法来执行列表大小)。

Maybe take a look at Scala where you have tuples and case classes (replacing whole Key class with one-liner).

也许您可以看看Scala,那里有tuple和case类(用一行代码替换整个键类)。

#2


15  

When you create your own key pair object, you should face a few thing.

当您创建自己的密钥对对象时,您应该面对一些事情。

First, you should be aware of implementing hashCode() and equals(). You will need to do this.

首先,您应该知道实现hashCode()和equals()。你需要这样做。

Second, when implementing hashCode(), make sure you understand how it works. The given user example

其次,在实现hashCode()时,确保您理解它是如何工作的。给定用户的例子

public int hashCode() {
    return this.x ^ this.y;
}

is actually one of the worst implementations you can do. The reason is simple: you have a lot of equal hashes! And the hashCode() should return int values that tend to be rare, unique at it's best. Use something like this:

实际上是最糟糕的实现之一。原因很简单:你有很多相同的哈希!hashCode()应该返回值,这些值往往是很少的,唯一的是最好的。使用这样的:

public int hashCode() {
  return (X << 16) + Y;
}

This is fast and returns unique hashes for keys between -2^16 and 2^16-1 (-65536 to 65535). This fits in almost any case. Very rarely you are out of this bounds.

这是快速和回报之间独特的散列键2 ^ 16和2 ^ 16:1(-65536 - 65535)。这几乎适用于任何情况。你很少跳出这个界限。

Third, when implementing equals() also know what it is used for and be aware of how you create your keys, since they are objects. Often you do unnecessary if statements cause you will always have the same result.

第三,当实现equals()时,也要知道它的用途,并知道如何创建键,因为它们是对象。通常情况下,你会做不必要的陈述,因为你总是会得到同样的结果。

If you create keys like this: map.put(new Key(x,y),V); you will never compare the references of your keys. Cause everytime you want to acces the map, you will do something like map.get(new Key(x,y));. Therefore your equals() does not need a statement like if (this == obj). It will never occure.

如果你创建了这样的键:map。把(新键(x,y),V);你永远不会比较你的钥匙的参考资料。因为每次你想看地图的时候,你都会做一些类似地图的事情。(新键(x,y));。因此,您的equals()不需要像if (this == obj)这样的语句。它永远不会发生血。

Instead of if (getClass() != obj.getClass()) in your equals() better use if (!(obj instanceof this)). It will be valid even for subclasses.

在您的equals()中,如果(!(obj instanceof))。它甚至对于子类也是有效的。

So the only thing you need to compare is actually X and Y. So the best equals() implementation in this case would be:

所以你唯一需要比较的是X和y,所以在这个例子中最好的等于()的实现是:

public boolean equals (final Object O) {
  if (!(O instanceof Key)) return false;
  if (((Key) O).X != X) return false;
  if (((Key) O).Y != Y) return false;
  return true;
}

So in the end your key class is like this:

最后,你的关键课程是这样的:

public class Key {

  public final int X;
  public final int Y;

  public Key(final int X, final int Y) {
    this.X = X;
    this.Y = Y;
  }

  public boolean equals (final Object O) {
    if (!(O instanceof Key)) return false;
    if (((Key) O).X != X) return false;
    if (((Key) O).Y != Y) return false;
    return true;
  }

  public int hashCode() {
    return (X << 16) + Y;
  }

}

You can give your dimension indices X and Y a public access level, due to the fact they are final and do not contain sensitive information. I'm not a 100% sure whether private access level works correctly in any case when casting the Object to a Key.

您可以给您的维度索引X和Y一个公共访问级别,因为它们是最终的,并且不包含敏感信息。在将对象转换为键时,我不是100%确定私有访问级别是否正确工作。

If you wonder about the finals, I declare anything as final which value is set on instancing and never changes - and therefore is an object constant.

如果你想知道期末考试的情况,我宣布任何东西作为最终的值,它的值是设定在实例上的,并且不会改变——因此是一个对象常量。

#3


5  

You can't have an hash map with multiple keys, but you can have an object that takes multiple parameters as the key.

您不能有一个带有多个键的散列映射,但是您可以有一个对象,它将多个参数作为键。

Create an object called Index that takes an x and y value.

创建一个名为Index的对象,它使用x和y值。

public class Index {

    private int x;
    private int y;

    public Index(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public int hashCode() {
        return this.x ^ this.y;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Index other = (Index) obj;
        if (x != other.x)
            return false;
        if (y != other.y)
            return false;
        return true;
    }
}

Then have your HashMap<Index, Value> to get your result. :)

然后使用HashMap 来得到结果。:) ,值>

#4


3  

Two possibilities. Either use a combined key:

两种可能。使用组合键:

class MyKey {
    int firstIndex;
    int secondIndex;
    // important: override hashCode() and equals()
}

Or a Map of Map:

或地图地图:

Map<Integer, Map<Integer, Integer>> myMap;

#5


3  

Implemented in common-collections [MultiKeyMap] (https://commons.apache.org/proper/commons-collections/javadocs/api-release/org/apache/commons/collections4/map/MultiKeyMap.html)

在公共集合中实现[MultiKeyMap] (https://commons.apache.org/proper/commons-collections/javadocs/api-release/org/apache/commons/collections4/map/MultiKeyMap.html)

#6


1  

Create a value class that will represent your compound key, such as:

创建一个表示复合键的值类,例如:

class Index2D {
  int first, second;

  // overrides equals and hashCode properly here
}

taking care to override equals() and hashCode() correctly. If that seems like a lot of work, you might consider some ready made generic containers, such as Pair provided by apache commons among others.

注意重载equals()和hashCode()正确。如果这看起来像很多工作,您可能会考虑一些现成的通用容器,比如apache commons提供的一对。

There are also many similar questions here, with other ideas, such as using Guava's Table, although allows the keys to have different types, which might be overkill (in memory use and complexity) in your case since I understand your keys are both integers.

这里也有许多类似的问题,比如使用Guava的表,虽然允许键有不同的类型,但在您的情况下可能会被过度使用(内存使用和复杂性),因为我理解您的密钥都是整数。

#7


1  

If they are two integers you can try a quick and dirty trick: Map<String, ?> using the key as i+"#"+j.

如果它们是两个整数,那么您可以尝试一个快速和下流的技巧:Map ,>

If the key i+"#"+j is the same as j+"#"+i try min(i,j)+"#"+max(i,j).

如果键我+“#”+ j一样+“#”+我尝试min(i,j)+“#”+马克斯(i,j)。

#8


1  

You could create your key object something like this:

你可以这样创建你的关键对象:

public class MapKey {

公开课MapKey {

public  Object key1;
public Object key2;

public Object getKey1() {
    return key1;
}

public void setKey1(Object key1) {
    this.key1 = key1;
}

public Object getKey2() {
    return key2;
}

public void setKey2(Object key2) {
    this.key2 = key2;
}

public boolean equals(Object keyObject){

    if(keyObject==null)
        return false;

    if (keyObject.getClass()!= MapKey.class)
        return false;

    MapKey key = (MapKey)keyObject;

    if(key.key1!=null && this.key1==null)
        return false;

    if(key.key2 !=null && this.key2==null)
        return false;

    if(this.key1==null && key.key1 !=null)
        return false;

    if(this.key2==null && key.key2 !=null)
        return false;

    if(this.key1==null && key.key1==null && this.key2 !=null && key.key2 !=null)
        return this.key2.equals(key.key2);

    if(this.key2==null && key.key2==null && this.key1 !=null && key.key1 !=null)
        return this.key1.equals(key.key1);

    return (this.key1.equals(key.key1) && this.key2.equals(key2));
}

public int hashCode(){
    int key1HashCode=key1.hashCode();
    int key2HashCode=key2.hashCode();
    return key1HashCode >> 3 + key2HashCode << 5;
}

}

}

The advantage of this is: It will always make sure you are covering all the scenario's of Equals as well.

这样做的好处是:它将始终确保覆盖所有的场景。

NOTE: Your key1 and key2 should be immutable. Only then will you be able to construct a stable key Object.

注意:您的key1和key2应该是不可变的。只有这样,您才能构建一个稳定的密钥对象。

#9


1  

we can create a class to pass more than one key or value and the object of this class can be used as a parameter in map.

我们可以创建一个类来传递多个键或值,这个类的对象可以作为映射中的参数使用。

import java.io.BufferedReader; 
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

 public class key1 {
    String b;
    String a;
    key1(String a,String b)
    {
        this.a=a;
        this.b=b;
    }
  }

public class read2 {

private static final String FILENAME = "E:/studies/JAVA/ReadFile_Project/nn.txt";

public static void main(String[] args) {

    BufferedReader br = null;
    FileReader fr = null;
    Map<key1,String> map=new HashMap<key1,String>();
    try {

        fr = new FileReader(FILENAME);
        br = new BufferedReader(fr);

        String sCurrentLine;

        br = new BufferedReader(new FileReader(FILENAME));

        while ((sCurrentLine = br.readLine()) != null) {
            String[] s1 = sCurrentLine.split(",");
            key1 k1 = new key1(s1[0],s1[2]);
            map.put(k1,s1[2]);
        }
        for(Map.Entry<key1,String> m:map.entrySet()){  
            key1 key = m.getKey();
            String s3 = m.getValue();
               System.out.println(key.a+","+key.b+" : "+s3);  
              }  
  //            }   
        } catch (IOException e) {

        e.printStackTrace();

    } finally {

        try {

            if (br != null)
                br.close();

            if (fr != null)
                fr.close();

        } catch (IOException ex) {

            ex.printStackTrace();

        }

    }

    }

 }

#10


0  

Use a Pair as keys for the HashMap. JDK has no Pair, but you can either use a 3rd party libraray such as http://commons.apache.org/lang or write a Pair taype of your own.

使用一对作为HashMap的键。JDK没有对,但是您可以使用第三方libraray,比如http://commons.apache.org/lang或您自己的一对taype。

#11


0  

You can also use guava Table implementation for this.

您还可以使用guava表实现。

Table represents a special map where two keys can be specified in combined fashion to refer to a single value. It is similar to creating a map of maps.

表表示一个特殊的映射,其中两个键可以以组合方式指定,以引用单个值。它类似于创建地图地图。

//create a table
  Table<String, String, String> employeeTable = HashBasedTable.create();

  //initialize the table with employee details
  employeeTable.put("IBM", "101","Mahesh");
  employeeTable.put("IBM", "102","Ramesh");
  employeeTable.put("IBM", "103","Suresh");

  employeeTable.put("Microsoft", "111","Sohan");
  employeeTable.put("Microsoft", "112","Mohan");
  employeeTable.put("Microsoft", "113","Rohan");

  employeeTable.put("TCS", "121","Ram");
  employeeTable.put("TCS", "122","Shyam");
  employeeTable.put("TCS", "123","Sunil");

  //get Map corresponding to IBM
  Map<String,String> ibmEmployees =  employeeTable.row("IBM");