如何用两个键(Key-Pair,Value)创建一个HashMap? [英] How to create a HashMap with two keys (Key-Pair, Value)?
问题描述
我有一个整数的二维数组。我希望他们被放入一个HashMap。但我想从基于数组索引的HashMap中访问元素。例如:
对于A [2] [5], map.get(2,5)
,它返回与该键相关联的值。但是,我如何使用一对密钥创建一个hashMap?或者一般来说,我可以通过使用get(key1,key1,key2,...,keyN),Value)来获取多个键: Map< ,key2,... keyN)。
编辑:发布问题3年后,我想添加更多内容
对于 NxN矩阵
,我遇到了另一种方式。
可以表示数组索引 i
和 j
作为一个单一的键
以下面的方式:
int key = i * N + j;
//map.put(key,a [i] [j]); // queue.add(key);
指数可以从键
以这种方式:
int i = key / N;
int j = key%N;
有几种选择:
$ b
2维
地图地图
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).这篇关于如何用两个键(Key-Pair,Value)创建一个HashMap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!