java 集合拼接_java中拼接两个对象集合

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

1.首先定义一个字符串

String str = "[{\"ITEMID\":2,\"ITEMNAME\":\"人大常委办公厅\",\"BILLTYPEID\":1,\"BILLTYPENAME\":\"一般缴款书\"},\n" +

" {\"ITEMID\":2,\"ITEMNAME\":\"人大常委办公厅\",\"BILLTYPEID\":3,\"BILLTYPENAME\":\"退付书(微机)\"},\n" +

" {\"ITEMID\":2,\"ITEMNAME\":\"人大常委办公厅\",\"BILLTYPEID\":41,\"BILLTYPENAME\":\"高速公路收费专用票据(500元)\"},\n" +

" {\"ITEMID\":2,\"ITEMNAME\":\"人大常委办公厅\",\"BILLTYPEID\":720,\"BILLTYPENAME\":\"单位手写缴款书(本局)\"},\n" +

" {\"ITEMID\":2,\"ITEMNAME\":\"人大常委办公厅\",\"BILLTYPEID\":721,\"BILLTYPENAME\":\"单位微机缴款书(本局)\"},\n" +

" {\"ITEMID\":2,\"ITEMNAME\":\"人大常委办公厅\",\"BILLTYPEID\":743,\"BILLTYPENAME\":\"收入专用收据(机打)\"}\n" +

"]";

2.然后将str先转换成json格式,然后再转换成list1: 代码如下

ObjectMapper mapper = new ObjectMapper();

List> conList = null;

conList = mapper.readValue(str, List.class);

3.ObjectMapper的使用需要导入jar包或者在pom文件中添加依赖:

org.codehaus.jackson

jackson-mapper-asl

1.7.1

4.通过mybatis查询表中的数据,用list接收

List list = new ArrayList();

list = eInoviceThresholdMapper.selectAll();

5.然后通过循环嵌套,对比出BILLTYPEID,如果相同则将表中查出的部分属性通过list的put方法添加到list conList中上完整代码:

@Override

public List> selectAll() {

String str = "[{\"ITEMID\":2,\"ITEMNAME\":\"人大常委办公厅\",\"BILLTYPEID\":1,\"BILLTYPENAME\":\"一般缴款书\"},\n" +

" {\"ITEMID\":2,\"ITEMNAME\":\"人大常委办公厅\",\"BILLTYPEID\":3,\"BILLTYPENAME\":\"退付书(微机)\"},\n" +

" {\"ITEMID\":2,\"ITEMNAME\":\"人大常委办公厅\",\"BILLTYPEID\":41,\"BILLTYPENAME\":\"高速公路收费专用票据(500元)\"},\n" +

" {\"ITEMID\":2,\"ITEMNAME\":\"人大常委办公厅\",\"BILLTYPEID\":720,\"BILLTYPENAME\":\"单位手写缴款书(本局)\"},\n" +

" {\"ITEMID\":2,\"ITEMNAME\":\"人大常委办公厅\",\"BILLTYPEID\":721,\"BILLTYPENAME\":\"单位微机缴款书(本局)\"},\n" +

" {\"ITEMID\":2,\"ITEMNAME\":\"人大常委办公厅\",\"BILLTYPEID\":743,\"BILLTYPENAME\":\"收入专用收据(机打)\"}\n" +

"]";

List list = new ArrayList();

list = eInoviceThresholdMapper.selectAll();

ObjectMapper mapper = new ObjectMapper();

List> conList = null;

try {

conList = mapper.readValue(str, List.class);

for (int i=0;i

for (int j=0;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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值