java Map 的去重

可以把需要的东西存到map中

map的key会自动去重

containsKey()来比较;
Map pfwMap = new HashMap();    
            for (Iterator iterator = pfw.iterator();iterator.hasNext();)
            {
                   Customerflow Cfw =(Customerflow) iterator.next();
                   
                   if (pfwMap.containsKey(Cfw.getDate()))
                   {
                       Integer  k =  pfwMap.get(Cfw.getDate())+Cfw.getSum();
                       pfwMap.put(Cfw.getDate(),k);
                   }else
                   {
                       pfwMap.put(Cfw.getDate(), Cfw.getSum());
                   }
            }

可以用keySet 来取出map中的key值。 然后再用get(key)就可以取出value

    Set keySet = pfwMap.keySet();
            totalListInfo = new ArrayList();
            for (Iterator it = keySet.iterator(); it.hasNext();)
            {
                   Long nxt = it.next();
                   Customerflow C = new Customerflow();
                   C.setDate(nxt);
                   C.setSum(pfwMap.get(nxt));
                   totalListInfo.add(C);
            }

 

转载于:https://www.cnblogs.com/Mischief/p/5715566.html

你可能感兴趣的:(java Map 的去重)