• java中拼接两个对象集合


    目标:  根据两个list中每条记录的某个属性是否相同来拼接。

    1.首先定义一个字符串

    String str = "[{"ITEMID":2,"ITEMNAME":"人大常委办公厅","BILLTYPEID":1,"BILLTYPENAME":"一般缴款书"},
    " +
    " {"ITEMID":2,"ITEMNAME":"人大常委办公厅","BILLTYPEID":3,"BILLTYPENAME":"退付书(微机)"}, " +
    " {"ITEMID":2,"ITEMNAME":"人大常委办公厅","BILLTYPEID":41,"BILLTYPENAME":"高速公路收费专用票据(500元)"}, " +
    " {"ITEMID":2,"ITEMNAME":"人大常委办公厅","BILLTYPEID":720,"BILLTYPENAME":"单位手写缴款书(本局)"}, " +
    " {"ITEMID":2,"ITEMNAME":"人大常委办公厅","BILLTYPEID":721,"BILLTYPENAME":"单位微机缴款书(本局)"}, " +
    " {"ITEMID":2,"ITEMNAME":"人大常委办公厅","BILLTYPEID":743,"BILLTYPENAME":"收入专用收据(机打)"} " +
    "]";
    2.然后将str先转换成json格式,然后再转换成list1: 代码如下
    ObjectMapper mapper = new ObjectMapper();
    List<Map<String,Object>> conList = null;
    conList = mapper.readValue(str, List.class);

    3.ObjectMapper的使用需要导入jar包或者在pom文件中添加依赖:
    <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.7.1</version>
    </dependency>
    4.通过mybatis查询表中的数据,用list接收
    List<EInvoiceThresholdDTO> list = new ArrayList<EInvoiceThresholdDTO>();
    list = eInoviceThresholdMapper.selectAll();

    5.然后通过循环嵌套,对比出BILLTYPEID,如果相同则将表中查出的部分属性通过list的put方法添加到list conList中
    上完整代码:
    @Override
    public List<Map<String,Object>> selectAll() {
    String str = "[{"ITEMID":2,"ITEMNAME":"人大常委办公厅","BILLTYPEID":1,"BILLTYPENAME":"一般缴款书"}, " +
    " {"ITEMID":2,"ITEMNAME":"人大常委办公厅","BILLTYPEID":3,"BILLTYPENAME":"退付书(微机)"}, " +
    " {"ITEMID":2,"ITEMNAME":"人大常委办公厅","BILLTYPEID":41,"BILLTYPENAME":"高速公路收费专用票据(500元)"}, " +
    " {"ITEMID":2,"ITEMNAME":"人大常委办公厅","BILLTYPEID":720,"BILLTYPENAME":"单位手写缴款书(本局)"}, " +
    " {"ITEMID":2,"ITEMNAME":"人大常委办公厅","BILLTYPEID":721,"BILLTYPENAME":"单位微机缴款书(本局)"}, " +
    " {"ITEMID":2,"ITEMNAME":"人大常委办公厅","BILLTYPEID":743,"BILLTYPENAME":"收入专用收据(机打)"} " +
    "]";
    List<EInvoiceThresholdDTO> list = new ArrayList<EInvoiceThresholdDTO>();
    list = eInoviceThresholdMapper.selectAll();
    ObjectMapper mapper = new ObjectMapper();
    List<Map<String,Object>> conList = null;
    try {
    conList = mapper.readValue(str, List.class);
    for (int i=0;i<conList.size();i++){
    for (int j=0;j<list.size();j++){
    if (conList.get(i).get("BILLTYPEID").toString().equals(list.get(j).getBillTypeID().toString())){
    conList.get(i).put("AGENCY",list.get(j).getAGENCY());
    conList.get(i).put("YEAR",list.get(j).getYEAR());
    conList.get(i).put("WRITEUSER",list.get(j).getWRITEUSER());
    conList.get(i).put("WRITEDATE",list.get(j).getWRITEDATE());
    conList.get(i).put("ISENABLE",list.get(j).getISENABLE());
    conList.get(i).put("ISAUTO",list.get(j).getISAUTO());
    conList.get(i).put("threshold",list.get(j).getThreshold());
    }
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    return conList;
    }





  • 相关阅读:
    POJ 3093 Margaritas on the River Walk(背包)
    BZOJ 2287 【POJ Challenge】消失之物(DP+容斥)
    WC2017 Day1
    WC2017 Day0
    WC2017 Conclusion
    WC2017 Day6
    UOJ #58 糖果公园
    WC2017 Day5
    codevs 1946 阿狸的打字机
    HDU 2457 DNA_repair
  • 原文地址:https://www.cnblogs.com/lovefaner/p/9605608.html
Copyright © 2020-2023  润新知