有一个包含集合的二维数组,我想将它作为键放在 map 上。请有人建议如何正确地做到这一点。预期和实际产出附于下文。
public class trysome
{
static int[][] points = {{1,2}, {1,1},{5,7}};
public static void some()
{
HashMap<int[], Integer> map= new HashMap<int[], Integer>();
for(int i =0;i<points.length;i++)
{
map.put(points[i], 1);
}
for(Entry<int[], Integer> entry : map.entrySet())
{
System.out.println(entry.getKey() + " "+entry.getValue());
}
}
public static void main(String[] args)
{
trysome.some();
}
}
Actual Output:
[I@16b4a017 1
[I@8807e25 1
[I@2a3046da 1
Expected Output:
{1,2} 1
{1,1} 1
{5,7} 1
最佳答案
What's the simplest way to print a Java array? 中解释了您观察到的输出的原因。 。底线是:输出[I@16b4a017
基本上是数组的“内存位置”,而不是数组的内容。
我之所以不将其作为重复项关闭,是因为这里的输出只是一个更大缺陷的不相关症状:您的方法概念上是错误的。
您不能使用 int[]
数组作为基于哈希的数据结构中的键!
有人可能会说,如果依赖于数组的身份,它就会起作用。但这种情况很少发生。
原因是equals
和hashCode
方法没有按照其工作所需的方式在数组上实现。省略一些可以在其他地方阅读的技术细节。
如果您的代码应该处理平面上的 2D 点,那么您应该使用适当的类来表示这些点。然后,此类可以包含 hashCode
的正确实现, equals
和toString
.
幸运的是,Java标准API中已经有这样一个类,即 java.awt.Point
。
下面显示了为什么您的原始实现在 usingArraysAsKeys
中无法按预期工作。方法,以及如何在 usingPointsAsKeys
中正确实现它方法:
import java.awt.Point;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class PointsAsKeys
{
public static void main(String[] args)
{
usingArraysAsKeys();
usingPointsAsKeys();
}
public static void usingPointsAsKeys()
{
Point points[] =
{
new Point(1, 2),
new Point(1, 1),
new Point(5, 7)
};
Map<Point, Integer> map = new HashMap<Point, Integer>();
for (int i = 0; i < points.length; i++)
{
map.put(points[i], 1);
}
for (Entry<Point, Integer> entry : map.entrySet())
{
Point p = entry.getKey();
String s = "{" + p.x + ", " + p.y + "}";
System.out.println(s + " " + entry.getValue());
}
//=====================================================================
// Important: This shows that it WILL work as expected!
Point somePoint = new Point(1, 2);
Integer value = map.get(somePoint);
System.out.println("Value for " + somePoint + " is " + value);
}
public static void usingArraysAsKeys()
{
int[][] points =
{
{ 1, 2 },
{ 1, 1 },
{ 5, 7 }
};
HashMap<int[], Integer> map = new HashMap<int[], Integer>();
for (int i = 0; i < points.length; i++)
{
map.put(points[i], 1);
}
for (Entry<int[], Integer> entry : map.entrySet())
{
// This would print the arrays as "[I@139a55"
//System.out.println(entry.getKey() + " " + entry.getValue());
// This will print the arrays as [1, 2]:
System.out.println(
Arrays.toString(entry.getKey()) + " " + entry.getValue());
}
//=====================================================================
// Important: This shows that it will NOT work as expected!
int somePoint[] = { 1, 2 };
Integer value = map.get(somePoint);
System.out.println(
"Value for " + Arrays.toString(somePoint) + " is " + value);
}
}
使用 Point
的另一个优点类的最大优点是它已经有几个方便的方法 - 最重要的是 Point2D#distance
允许您计算一个点到另一个点的距离的方法:
// Computes the distance of the point to the point at (0,0) (i.e. the origin)
double distanceToOrigin = point.distance(new Point(0,0));
(顺便说一句:如果 map 应该存储距离,那么它的值类型可能应该是 Double
而不是 Integer
)
关于java - 如何将二维数组的条目作为键放入 map 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49593373/