一道面试题 如下表:NUM QTY1 23 42 14 32 23 24 11 3用java将其变为:NUM QTY1 52 33 54 4这是一道昨天碰见的面试题,应该是考察数组和map的,大家可以试试写一下。面试题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 sql 语句的话 直接分组查询就行了 java的 话 就放在map里 key为num value为qty 循环相加 在按照key进行排序 key 是num value 是qty 往map里放 碰到已经有的改变value 感觉虽然实现了,但是比较麻烦,LZ可以看一下package writeTest;import java.util.*;public class Test1 { /** * 将传参list中Map的key是NUM的值相同的相加,让NUM值相同的不重复出现 */ public static void main(String[] args) { // TODO Auto-generated method stub List<Map> list = new ArrayList(); Map map = new HashMap(); map.put("NUM", 1); map.put("QTY", 3); list.add(map); map = new HashMap(); map.put("NUM", 1); map.put("QTY", 2); list.add(map); map = new HashMap(); map.put("NUM", 2); map.put("QTY", 3); list.add(map); map = new HashMap(); map.put("NUM", 2); map.put("QTY", 4); list.add(map); map = new HashMap(); map.put("NUM", 3); map.put("QTY", 1); list.add(map); map = new HashMap(); map.put("NUM", 3); map.put("QTY", 2); list.add(map); map = new HashMap(); map.put("NUM", 4); map.put("QTY", 2); list.add(map); map = new HashMap(); map.put("NUM", 4); map.put("QTY", 2); list.add(map); System.out.println(list); List<Map> result = new ArrayList(); result = sum(list); } //实现方法 private static List<Map> sum(List<Map> list){ List<Map> result = new ArrayList(); Map tempMap = new HashMap(); //将NUM相同的值相加,放到Map中{1=3,2=4,3=6...} for(Map map:list){ if(tempMap.containsKey(map.get("NUM"))){ int value = Integer.valueOf(tempMap.get(map.get("NUM")).toString())+Integer.valueOf(map.get("QTY").toString()); tempMap.put(map.get("NUM"), value); }else{ tempMap.put(map.get("NUM"), map.get("QTY")); } } //将Map中的值转化为{QTY=3, NUM=1}这种形式的List Set keySet = tempMap.keySet(); Iterator it = keySet.iterator(); while(it.hasNext()){ Map resultMap = new HashMap(); int key = (Integer) it.next(); resultMap.put("NUM", key); resultMap.put("QTY", tempMap.get(key)); result.add(resultMap); } System.out.println("result:"+result); return result; }} 先将原放到二维数组,然后再往map放,放入map前先get,如果已经有的话就取出来加,然后再set进去结果3应该对应6吧 public class TestArray{ public static void main(String[] args) { int [][] orig = new int[8][2]; orig[0][0] = 1; orig[0][1] = 2; orig[1][0] = 3; orig[1][1] = 4; orig[2][0] = 2; orig[2][1] = 1; orig[3][0] = 4; orig[3][1] = 3; orig[4][0] = 2; orig[4][1] = 2; orig[5][0] = 3; orig[5][1] = 2; orig[6][0] = 4; orig[6][1] = 1; orig[7][0] = 1; orig[7][1] = 3; Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i<orig.length; i++) { int [] tem = orig[i]; if (null != map.get(tem[0])) { map.put(tem[0], tem[1] + map.get(tem[0])); } else { map.put(tem[0], tem[1]); } } System.out.println(map); }} 配置spring MVC的时候,启动报异常? Struts2参数传递的问题- -! hibernate自带的分页问题 tomcat6.0数据源配置 有关jboss服务器包导入的问题 请问何如使用Servicemix的FTP组件 有谁知道用arale爬虫软件在百度图片上面寻找到的图片都打不开啊? 请教:java中可否调用matlab程序? 没有ActionForm可以实现struts么? 求救高手,jdbc连接数据库问题。调试一百遍都出错。 异常:org.springframework.jdbc.UncategorizedSQLException 求个js
* 将传参list中Map的key是NUM的值相同的相加,让NUM值相同的不重复出现
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Map> list = new ArrayList();
Map map = new HashMap();
map.put("NUM", 1);
map.put("QTY", 3);
list.add(map);
map = new HashMap();
map.put("NUM", 1);
map.put("QTY", 2);
list.add(map);
map = new HashMap();
map.put("NUM", 2);
map.put("QTY", 3);
list.add(map);
map = new HashMap();
map.put("NUM", 2);
map.put("QTY", 4);
list.add(map);
map = new HashMap();
map.put("NUM", 3);
map.put("QTY", 1);
list.add(map);
map = new HashMap();
map.put("NUM", 3);
map.put("QTY", 2);
list.add(map);
map = new HashMap();
map.put("NUM", 4);
map.put("QTY", 2);
list.add(map);
map = new HashMap();
map.put("NUM", 4);
map.put("QTY", 2);
list.add(map);
System.out.println(list);
List<Map> result = new ArrayList();
result = sum(list);
}
//实现方法
private static List<Map> sum(List<Map> list){
List<Map> result = new ArrayList();
Map tempMap = new HashMap();
//将NUM相同的值相加,放到Map中{1=3,2=4,3=6...}
for(Map map:list){
if(tempMap.containsKey(map.get("NUM"))){
int value = Integer.valueOf(tempMap.get(map.get("NUM")).toString())+Integer.valueOf(map.get("QTY").toString());
tempMap.put(map.get("NUM"), value);
}else{
tempMap.put(map.get("NUM"), map.get("QTY"));
}
}
//将Map中的值转化为{QTY=3, NUM=1}这种形式的List
Set keySet = tempMap.keySet();
Iterator it = keySet.iterator();
while(it.hasNext()){
Map resultMap = new HashMap();
int key = (Integer) it.next();
resultMap.put("NUM", key);
resultMap.put("QTY", tempMap.get(key));
result.add(resultMap);
}
System.out.println("result:"+result);
return result;
}}
结果3应该对应6吧
{ public static void main(String[] args)
{
int [][] orig = new int[8][2];
orig[0][0] = 1;
orig[0][1] = 2;
orig[1][0] = 3;
orig[1][1] = 4;
orig[2][0] = 2;
orig[2][1] = 1;
orig[3][0] = 4;
orig[3][1] = 3;
orig[4][0] = 2;
orig[4][1] = 2;
orig[5][0] = 3;
orig[5][1] = 2;
orig[6][0] = 4;
orig[6][1] = 1;
orig[7][0] = 1;
orig[7][1] = 3;
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i<orig.length; i++)
{
int [] tem = orig[i];
if (null != map.get(tem[0]))
{
map.put(tem[0], tem[1] + map.get(tem[0]));
}
else {
map.put(tem[0], tem[1]);
}
}
System.out.println(map);
}
}