HashMap<String, List<Integer>> map = new HashMap<String, List<Integer>>();
HashMap<String, int[]> map = new HashMap<String, int[]>();
键,例如
HashMap<String, List<Integer>> map = new HashMap<String, List<Integer>>();
map.put("Something", new ArrayList<Integer>());
for (int i=0;i<numarulDeCopii; i++) {
map.get("Something").add(coeficientUzura[i]);
}
或仅仅是
HashMap<String, int[]> map = new HashMap<String, int[]>();
map.put("Something", coeficientUzura);
public class TestRun
{
public static void main(String [] args)
{
Map<String, Integer[]> prices = new HashMap<String, Integer[]>();
prices.put("milk", new Integer[] {1, 3, 2});
prices.put("eggs", new Integer[] {1, 1, 2});
}
}
int[] val = {1, 2, 3};
Map<String, int[]> map = new HashMap<String, int[]>();
map.put("KEY1", val);
也,根据您的用例,您可能需要查看
Guava.
支持。
如果您想为密钥存储多个值(如果我正确理解您),则可以尝试使用Multihashmap(在各种库中可用,不仅在Commons-Collections中)。 如果您可以将列表保存为值而不是该地图中的数组,那么您的生活将容易得多。
您可以将对象存储在哈希姆普中。代码的问题在于您如何使用数组(int [])作为hashmap中的键。在Java中,数组不会从对象覆盖Equals()和HashCode()方法。结果,当您将数组用作hashmap中的键时,HashCode()的默认实现基于内存地址,而不是数组的内容。因此,两个具有相同元素的数组被视为不同的键。
要解决此问题,您应该使用正确实现Equals()和HashCode()的密钥类型。一个很好的替代方法是使用列表(例如ArrayList)或数组的字符串表示为键。仅通过字符串或arraylist,您可以比较并使用包含方法