2014年5月26日星期一

List <Map <String, int[]>> array adding

 This post last edited by lilewy on 2014-05-25 10:32:26
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 map0 = new HashMap ();
map0.put ("d", num1);
map0.put ("c", num2);
map0.put ("b", num3);
map0.put ("a", num4);
Map map1 = new HashMap ();
map1.put ("d", num1);
map1.put ("c", num2);
map1.put ("b", num3);
map1.put ("a", num4);

List > infoIds = new ArrayList > ();
infoIds.add (map0);
infoIds.add (map1);


think the result is
map0 with the same key adding map1
If the sum is a 38,40,42,44,46,48
b ...., c ...., d ......
return the new Map 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 > infoIds, trouble and then improve the next .
------ For reference only -------------------------------------- -
Map NewMap = new HashMap ();
for (Map maptemp: infoIds) {
Map map = new HashMap ; ( ) ;
for (Map.Entry entry: ; maptemp.entrySet ()) {
String key = entry.getKey ();
int [] value = entry.getValue ( ) ;
if (map.containsKey (key)) {
int [] num = {};
for (int i = 0; i num = new int [value.length];
num [i] = map.get (key) [i] + value [i];
}
map.put (key, num);
} else {
map.put (key, value);
}
}
System.out.println ("----");
System.out.println (" summed up :" + map.get ("a") [0] + "," ; + map.get ("a") [1] + "," + map.get ("a") [2] + "," + map.get ("a") [ 3] + "," + map.get ("a") [4] + "," + map.get ("a") [5]);
NewMap = map;
}
for (int a: NewMap.get ("a")) {
System.out.println (a + ",");
}
leave himself wrote for the collection. Thanks for the reply to the points.

没有评论:

发表评论