现在有一个封装好的List<ChaobiaoJilu>里面装的是N个抄表记录
要将具有相同用气证号的抄表记录合并,合并同时将金额向加,最终将其合并为一条抄表记录(最终合并后的抄表记录ID是这些相同中任意一个即可)
例如:
合并前:::001 111111 10.0
合并前:::005 111111 10.0
合并前:::006 111111 10.0
合并前:::007 111111 10.0
合并前:::002 222222 10.0
合并前:::003 333333 10.0
合并前:::004 444444 10.0
============================
合并后:::001 111111 40.0
合并后:::002 222222 10.0
合并后:::003 333333 10.0
合并后:::004 444444 10.0以下代码已经实现该功能,但是实际中的数据会可能比较大,所以我想寻求最高效的解答方法。import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class ListOpera
{
public List<ChaobiaoJilu> distinctByYqzh(List<ChaobiaoJilu> cjs)
{// 将同一用户多条记录的金额合并为一条记录
List<String> unique_acount_lst = new ArrayList<String>();
for (ChaobiaoJilu cbjl : cjs)
{
if (!unique_acount_lst.contains(cbjl.getYqzh()))
{
unique_acount_lst.add(cbjl.getYqzh());
}
}
Map<String, ChaobiaoJilu> u_cbjl_map_cal = new HashMap<String, ChaobiaoJilu>();
for (ChaobiaoJilu cbjl : cjs)
{
String account = cbjl.getYqzh();
if (!u_cbjl_map_cal.keySet().contains(account))
{
ChaobiaoJilu cbjl_new = new ChaobiaoJilu();
cbjl_new.setCbjlId(cbjl.getCbjlId());
cbjl_new.setJinE(cbjl.getJinE());
cbjl_new.setYqzh(account); u_cbjl_map_cal.put(account, cbjl_new);
} else
{
ChaobiaoJilu cbjltemp = u_cbjl_map_cal.get(account);
cbjltemp.setJinE(cbjltemp.getJinE() + cbjl.getJinE());
}
}
Iterator it = u_cbjl_map_cal.keySet().iterator();
List<ChaobiaoJilu> cbjls = new ArrayList<ChaobiaoJilu>();
while (it.hasNext())
{
String key = (String) it.next();
cbjls.add(u_cbjl_map_cal.get(key));
}

sortByYqzh(cbjls);//排序(单纯的排序)
for (ChaobiaoJilu cj : cbjls)
{
System.out.println("合并后:::"+cj.getCbjlId() + "\t" + cj.getYqzh() + "\t" + cj.getJinE());
}
return cbjls;
}
public List<ChaobiaoJilu> sortByYqzh(List<ChaobiaoJilu> cjs)
{
Collections.sort(cjs, new Comparator<ChaobiaoJilu>()
{
public int compare(ChaobiaoJilu cbjl01, ChaobiaoJilu jbjlo2)
{
return cbjl01.getYqzh().compareTo(jbjlo2.getYqzh());
}
});
return cjs;
} public List<ChaobiaoJilu> init()
{
List<ChaobiaoJilu> cjs = new ArrayList<ChaobiaoJilu>();
ChaobiaoJilu cj01 = new ChaobiaoJilu();
cj01.setCbjlId("001");
cj01.setYqzh("111111");
cj01.setJinE(10); ChaobiaoJilu cj02 = new ChaobiaoJilu();
cj02.setCbjlId("002");
cj02.setYqzh("222222");
cj02.setJinE(10); ChaobiaoJilu cj03 = new ChaobiaoJilu();
cj03.setCbjlId("003");
cj03.setYqzh("333333");
cj03.setJinE(10);

ChaobiaoJilu cj06 = new ChaobiaoJilu();
cj06.setCbjlId("006");
cj06.setYqzh("111111");
cj06.setJinE(10);

ChaobiaoJilu cj04 = new ChaobiaoJilu();
cj04.setCbjlId("004");
cj04.setYqzh("444444");
cj04.setJinE(10); ChaobiaoJilu cj05 = new ChaobiaoJilu();
cj05.setCbjlId("005");
cj05.setYqzh("111111");
cj05.setJinE(10);

ChaobiaoJilu cj07 = new ChaobiaoJilu();
cj07.setCbjlId("007");
cj07.setYqzh("111111");
cj07.setJinE(10); cjs.add(cj01);
cjs.add(cj02);
cjs.add(cj03);
cjs.add(cj04);
cjs.add(cj05);
cjs.add(cj06);
cjs.add(cj07);

sortByYqzh(cjs);//排序(单纯的排序)
for (ChaobiaoJilu cj : cjs)
{
System.out.println("合并前:::"+cj.getCbjlId() + "\t" + cj.getYqzh() + "\t" + cj.getJinE());
} return cjs;
} public static void main(String[] str)
{
ListOpera lstOpe = new ListOpera();
lstOpe.distinctByYqzh(lstOpe.init());
}
}package zdx;public class ChaobiaoJilu
{
private String cbjlId;// 抄表记录ID
private String yqzh;// 用气证号
private double jinE;// 金额
public String getCbjlId()
{
return cbjlId;
}
public void setCbjlId(String cbjlId)
{
this.cbjlId = cbjlId;
}
public String getYqzh()
{
return yqzh;
}
public void setYqzh(String yqzh)
{
this.yqzh = yqzh;
}
public double getJinE()
{
return jinE;
}
public void setJinE(double jinE)
{
this.jinE = jinE;
}
}

解决方案 »

  1.   


    List <ChaobiaoJilu> cjs = new ArrayList <ChaobiaoJilu>(); 
    ChaobiaoJilu cj01 = new ChaobiaoJilu(); 
    cj01.setCbjlId("001"); 
    cj01.setYqzh("111111"); 
    cj01.setJinE(10); ChaobiaoJilu cj02 = new ChaobiaoJilu(); 
    cj02.setCbjlId("002"); 
    cj02.setYqzh("222222"); 
    cj02.setJinE(10); ChaobiaoJilu cj03 = new ChaobiaoJilu(); 
    cj03.setCbjlId("003"); 
    cj03.setYqzh("333333"); 
    cj03.setJinE(10); ChaobiaoJilu cj06 = new ChaobiaoJilu(); 
    cj06.setCbjlId("006"); 
    cj06.setYqzh("111111"); 
    cj06.setJinE(10); ChaobiaoJilu cj04 = new ChaobiaoJilu(); 
    cj04.setCbjlId("004"); 
    cj04.setYqzh("444444"); 
    cj04.setJinE(10); ChaobiaoJilu cj05 = new ChaobiaoJilu(); 
    cj05.setCbjlId("005"); 
    cj05.setYqzh("111111"); 
    cj05.setJinE(10); ChaobiaoJilu cj07 = new ChaobiaoJilu(); 
    cj07.setCbjlId("007"); 
    cj07.setYqzh("111111"); 
    cj07.setJinE(10); 如果已经有对象了,何必还要这样加?不是很明白意思。
    我觉得可以优化下,不太明白你的需求。
    一个循环不可以控制么?
      

  2.   

    插入到内存数据库里面然后在sum会不会快一点,楼下试过的,回个话。
      

  3.   

    LZ怎么这么喜欢高效率的List排序啊?还不喜欢用数据库?
      

  4.   

    同意楼上java用LinkedHashMap可能方便些,yqzh作为key
    如果先排序的话,用List就可以了
      

  5.   

    感觉你选择了在List里进行计算合并的话,就不太可能提高效率了...
      

  6.   


    如果从数据库中直接检索,那么sql语句该如何写呢?我试试。
      

  7.   

    如果直接从数据库中检索
    表  chaobiaojilu如果表中有数据
    例如: Id  yqzh    jinE
    -----------------
    007 111111 10.0 
    002 222222 10.0 
    003 333333 10.0 
    001 111111 10.0 
    005 111111 10.0 
    004 444444 10.0 
    006 111111 10.0 
    007 444444 10.0 要查处表中所有的记录,但是yqzh相同的要将金额相加,并且只留一条相同的记录,这样的SQL语句似乎很难那
      

  8.   


    public class ChaobiaoJilu {    private String cbjlId;// 抄表记录ID
        private String yqzh;// 用气证号
        private double jinE;// 金额
        public String getCbjlId() {
            return cbjlId;
        }    public void setCbjlId(String cbjlId) {
            this.cbjlId = cbjlId;
        }    public String getYqzh() {
            return yqzh;
        }    public void setYqzh(String yqzh) {
            this.yqzh = yqzh;
        }    public double getJinE() {
            return jinE;
        }    public void setJinE(double jinE) {
            this.jinE = jinE;
        }
    }public class ListOpera {    public List<ChaobiaoJilu> init() {
            List<ChaobiaoJilu> cjs = new ArrayList<ChaobiaoJilu>();
            ChaobiaoJilu cj01 = new ChaobiaoJilu();
            cj01.setCbjlId("001");
            cj01.setYqzh("111111");
            cj01.setJinE(10);        ChaobiaoJilu cj02 = new ChaobiaoJilu();
            cj02.setCbjlId("002");
            cj02.setYqzh("222222");
            cj02.setJinE(10);        ChaobiaoJilu cj03 = new ChaobiaoJilu();
            cj03.setCbjlId("003");
            cj03.setYqzh("333333");
            cj03.setJinE(10);        ChaobiaoJilu cj06 = new ChaobiaoJilu();
            cj06.setCbjlId("006");
            cj06.setYqzh("111111");
            cj06.setJinE(10);        ChaobiaoJilu cj04 = new ChaobiaoJilu();
            cj04.setCbjlId("004");
            cj04.setYqzh("444444");
            cj04.setJinE(10);        ChaobiaoJilu cj05 = new ChaobiaoJilu();
            cj05.setCbjlId("005");
            cj05.setYqzh("111111");
            cj05.setJinE(10);        ChaobiaoJilu cj07 = new ChaobiaoJilu();
            cj07.setCbjlId("007");
            cj07.setYqzh("111111");
            cj07.setJinE(10);        cjs.add(cj01);
            cjs.add(cj02);
            cjs.add(cj03);
            cjs.add(cj04);
            cjs.add(cj05);
            cjs.add(cj06);
            cjs.add(cj07);        for (ChaobiaoJilu cj : cjs) {
                System.out.println("合并前:::" + cj.getCbjlId() + "\t" + cj.getYqzh() + "\t" + cj.getJinE());
            }        return cjs;
        }    public void distinctByYqzh(List<ChaobiaoJilu> cjs) {
            HashMap m = new HashMap();
            for (ChaobiaoJilu c : cjs)
            {
                ChaobiaoJilu cbjl = (ChaobiaoJilu) m.get(c.getYqzh()); //寻找yqzh
                if(cbjl == null)    //相同yqzh进行相加
                {
                    m.put(c.getYqzh(), c);
                }else
                {
                    cbjl.setJinE(cbjl.getJinE()+c.getJinE());
                }
            }
            System.out.println("------------------");
            Iterator it = m.values().iterator();
            while(it.hasNext())
            {
                ChaobiaoJilu cj = (ChaobiaoJilu) it.next();
                System.out.println("合并后:::"+cj.getCbjlId() + "\t" + cj.getYqzh() + "\t" + cj.getJinE());
            }
        }    public static void main(String argas[])
        {
            ListOpera l = new ListOpera();
            l.distinctByYqzh(l.init());
        }
    }
    代码按照你的意思写的,不知道对不对,我没有进行排序,是因为你没有提供排序程序,我也懒的写
      

  9.   

    LZ呀,你的算法已经挺好的了.
    只是有两个地方稍微改一下就可以了.
    for (ChaobiaoJilu cbjl : cjs) 

    String account = cbjl.getYqzh(); //把这里拿到for外面去,要不然每循环一次都会创建一个对像,没有的.
    if (!u_cbjl_map_cal.keySet().contains(account)) 

    ChaobiaoJilu cbjl_new = new ChaobiaoJilu(); //这里就更没有必要了,接直用cbjl就可以了.
    cbjl_new.setCbjlId(cbjl.getCbjlId()); 
    cbjl_new.setJinE(cbjl.getJinE()); 
    cbjl_new.setYqzh(account); u_cbjl_map_cal.put(account, cbjl_new); 
    } else 

    ChaobiaoJilu cbjltemp = u_cbjl_map_cal.get(account); 
    cbjltemp.setJinE(cbjltemp.getJinE() + cbjl.getJinE()); 
    } 这段没有必要这么写了,改成下面这样就可以了.
    String account = "";
    for (ChaobiaoJilu cbjl : cjs) {
        account = cbjl.getYqzh();
        if (!u_cbjl_map_cal.keySet().contains(account)) {
            u_cbjl_map_cal.put(account, cbjl);
        } else {
            ChaobiaoJilu cbjltemp = u_cbjl_map_cal.get(account);
            cbjltemp.setJinE(cbjltemp.getJinE() + cbjl.getJinE());
        }
    }
      

  10.   

    Id  yqzh    jinE
    -----------------
    007 111111 10.0
    002 222222 10.0
    003 333333 10.0
    001 111111 10.0
    005 111111 10.0
    004 444444 10.0
    006 111111 10.0
    007 444444 10.0 select max(id) , yqzh,sum(jinE) from table1 group by yqzh
      

  11.   

    用聚合函数sum,
    select table.`id`,table.`yqzh`,sum(table.`jinE`) from table group by table.`yqzh`;