package tool;
import java.util.HashMap;
import java.util.Map;
/**
* 将两个数组的值合并为一个Map,如果值重复,则相加
*
*/
public class MergeArray
{
public static void main(String[] args)
{
String[] strArray =
{ "a", "b", "c", "a", "b" };
String[] intArray =
{ "1", "3", "4", "5" };
Map map = mergeArray(strArray, intArray);
System.out.println(map);
}
public static Map mergeArray(String[] strArray, String[] intArray)
{
Map map = new HashMap();
for (int i = 0; i < strArray.length; i++)
{
if (map.containsKey(strArray[i]))
{
int value = Integer.parseInt((String) map.get(strArray[i]));
if (intArray.length > i)
{
value += Integer.parseInt(intArray[i]);
map.put(strArray[i], value + "");
}
}
else
{
if (intArray.length > i)
{
map.put(strArray[i], intArray[i]);
}
}
}
return map;
}
}