如下表:
NUM         QTY
1                   2
3                   4
2                   1
4                   3
2                   2
3                   2
4                    1
1                   3
用java将其变为:
NUM       QTY
1                5
2                3
3                5
4                4
这是一道昨天碰见的面试题,应该是考察数组和map的,大家可以试试写一下。面试题

解决方案 »

  1.   

    sql 语句的话 直接分组查询就行了 java的 话 就放在map里 key为num value为qty 循环相加 在按照key进行排序
      

  2.   

    key 是num    value 是qty    往map里放 碰到已经有的改变value
      

  3.   

    感觉虽然实现了,但是比较麻烦,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;
    }}
      

  4.   

    先将原放到二维数组,然后再往map放,放入map前先get,如果已经有的话就取出来加,然后再set进去
    结果3应该对应6吧
      

  5.   

    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);
        }
    }