object
------ Solution ------------------- -------------------------
int[] num1 = { 1, 2, 3, 4, 5, 6 };
int[] num2 = { 7, 8, 9, 10, 11, 12 };
int[] num3 = { 13, 14, 15, 16, 17, 18 };
int[] num4 = { 19, 20, 21, 22, 23, 24 };
Map<String, int[]> map0 = new HashMap<String, int[]>();
map0.put("d", num1);
map0.put("c", num2);
map0.put("b", num3);
map0.put("a", num4);
Map<String, int[]> map1 = new HashMap<String, int[]>();
map1.put("d", num1);
map1.put("c", num2);
map1.put("b", num3);
map1.put("a", num4);
Map<String, int[]> map2 = new HashMap<String, int[]>();
map2.put("d", num1);
map2.put("c", num2);
map2.put("b", num3);
map2.put("a", num4);
List<Map<String, int[]>> infoIds = new ArrayList<Map<String, int[]>>();
infoIds.add(map0);
infoIds.add(map1);
infoIds.add(map2);
// 需要返回的map对象
Map<String, int[]> newMap = new HashMap<String, int[]>();
Set<String> keys = infoIds.get(0).keySet();
// 遍历map,将其中数组与别一个map相加
for (String key : keys)
{
//新数组
int[] newArr = new int[infoIds.get(0).get(key).length];
//遍历list中的map
for (Map<String, int[]> map : infoIds)
{
int[] array = map.get(key);
for (int i = 0; i < array.length; i++)
{
newArr[i] += array[i];
}
}
newMap.put(key, newArr);
}
System.out.println(newMap.get("a")[0]);
------ For reference only ------- --------------------------------
I'm here with map1 considered map0 the key are the same, and the corresponding key in the array is the same situation
int[] num1 = { 1, 2, 3, 4, 5, 6 };
int[] num2 = { 7, 8, 9, 10, 11, 12 };
int[] num3 = { 13, 14, 15, 16, 17, 18 };
int[] num4 = { 19, 20, 21, 22, 23, 24 };
Map<String, int[]> map0 = new HashMap<String, int[]>();
map0.put("d", num1);
map0.put("c", num2);
map0.put("b", num3);
map0.put("a", num4);
Map<String, int[]> map1 = new HashMap<String, int[]>();
map1.put("d", num1);
map1.put("c", num2);
map1.put("b", num3);
map1.put("a", num4);
//需要返回的map对象
Map<String, int[]> newMap = new HashMap<String, int[]>();
//遍历map,将其中数组与别一个map相加
Set<String> keys = map0.keySet();
for (String key : keys)
{
int[] array0 = map0.get(key);
int[] array1 = map1.get(key);
int[] newArray = new int[array0.length];
for (int i = 0, len = array0.length; i < len; i++)
{
newArray[i] = array0[i] + array1[i];
}
//将相加后的数组存入新map中
newMap.put(key, newArray);
}
System.out.println(newMap.get("a")[0]);
------ For reference only ----------------------------------- ----
I want to remove the data from the List
没有评论:
发表评论