更新时间:2022-12-11 08:27:37
有几种选择:
$ b
Map< Integer,Map< Integer,V>> map = // ...
// ...
map.get(2).get(5);
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))返回false;
Key key =(Key)o;
return x == key.x&& y == key.y;
}
@Override
public int hashCode(){
int result = x;
结果= 31 *结果+ y;
返回结果;
实现equals()
和hashCode()
在这里至关重要。然后你只需使用:Map< Key,V> map = // ...
和:
map.get(new Key(2,5));
Table
来自Guava表< Integer,Integer,V> table = HashBasedTable.create();
// ...
table.get(2,5);
表
使用地图 code> class是缩放到n维的唯一方法。您可能还会考虑:Map< List< Integer>,V> map = // ...
但从性能角度来看这很糟糕,以及可读性和正确性没有简单的方法来执行列表大小)。
也许看看你有元组的Scala和
case Key
类)。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:
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).EDIT : 3 years after posting the question, I want to add a bit more to it
I came across another way for
NxN matrix
.Array indices,
i
andj
can be represented as a singlekey
the following way: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;
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()
andhashCode()
is crucial here. Then you simply use:Map<Key, V> map = //...
and:
map.get(new Key(2, 5));
Table
from GuavaTable<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: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 wholeKey
class with one-liner).