将两个List根据某个相同字段来进行合并,排序
2016-11-14 12:12
706 查看
业务类简介:
public class ChannelSituation implements Serializable {
private Long id;
private Date date;//日期
private Integer identityCount;//认证人数
private Integer registCount; //注册人数
private Integer investCount;//投资人数
}
注:三个字段的关系:注册-> 认证 ->投资
identityCountList 中的数据只有认证人数:{(1,“2016-10-09”,3,0,0),(2,“2016-10-11”,5,0,0),(3,“2016-11-15”,2,0,0)}
registAndInvestCountList 中的数据有注册和投资人数:{(1,“2016-10-03”,0,3,0),(1,“2016-10-09”,0,8,3),(2,“2016-10-11”,0,10,4),(3,“2016-11-15”,0,5 , 0)}
// 存储有日期和认证人数
List identityCountList ;
// 存储有日期和注册人数、投资人数
List registAndInvestCountList ;
现在要将两个List数据合并如下:
合并
合并之后registAndInvestCountList数据变成认证,注册,投资人数都有,日期无重复。
排序
首先重写Comparator方法:
然后排序:
最后,下一篇写一个通用的根据List指定字段排序的工具类ListSortUtil
public class ChannelSituation implements Serializable {
private Long id;
private Date date;//日期
private Integer identityCount;//认证人数
private Integer registCount; //注册人数
private Integer investCount;//投资人数
}
注:三个字段的关系:注册-> 认证 ->投资
identityCountList 中的数据只有认证人数:{(1,“2016-10-09”,3,0,0),(2,“2016-10-11”,5,0,0),(3,“2016-11-15”,2,0,0)}
registAndInvestCountList 中的数据有注册和投资人数:{(1,“2016-10-03”,0,3,0),(1,“2016-10-09”,0,8,3),(2,“2016-10-11”,0,10,4),(3,“2016-11-15”,0,5 , 0)}
// 存储有日期和认证人数
List identityCountList ;
// 存储有日期和注册人数、投资人数
List registAndInvestCountList ;
现在要将两个List数据合并如下:
合并
for (ChannelSituation identityCount : identityCountList) { boolean flag = false; for (ChannelSituation registAndInvestCount : registAndInvestCountList) { if (identityCount.getDate().getTime() == registAndInvestCount.getDate().getTime()) { registAndInvestCount.setIdentityCount(identityCount.getIdentityCount()); flag = true; break; } } if (!flag) { registAndInvestCountList.add(identityCount); } }
合并之后registAndInvestCountList数据变成认证,注册,投资人数都有,日期无重复。
排序
首先重写Comparator方法:
public class MyComparator implements Comparator { @Override public int compare(Object o1, Object o2) { // TODO Auto-generated method stub ChannelSituation c1 = (ChannelSituation) o1; ChannelSituation c2 = (ChannelSituation) o2; if (c1.getDate().getTime() < c2.getDate().getTime()) {// 根据时间排序,这里根据你的需要来重写 return 1; } else { return 0; } } }
然后排序:
public static List<ChannelSituation> sortList(List<ChannelSituation> channelSituationList) { MyComparator mc = new MyComparator(); int len = channelSituationList.size(); for (int i = 0; i < len; i++) { for (int j = 0; j < len; j++) { if (mc.compare(channelSituationList.get(i), channelSituationList.get(j)) == 1) { Collections.swap(channelSituationList, i, j); } } } return channelSituationList; }
最后,下一篇写一个通用的根据List指定字段排序的工具类ListSortUtil
相关文章推荐
- 根据两个list的相同字段比较,合并成一个list
- php两个数组合并在一起,根据两个数组相同的键进行排序(时间排序)
- php合并两个二维数组,二维数组按键名排序,删除二维数组元素,二维数组按相同字段合并)
- java中list里面存放map,根据map中的某一个字段进行排序
- java中list里面存放map,根据map中的某一个字段进行排序
- list 根据相同字段合并数据
- List<Entity>排序:根据javabean中的一个字段对beanList进行排序
- sql语句——不同的表根据相同的字段值将两个表指定的字段合并为一个表
- 2015061502 - List中根据某个字段进行排序(方法1)
- java中list里面存放map,根据map中的某一个字段进行排序 lambda
- mysql 排序两个字段/列表先根据时间升序排序,时间相同再根据商家ID升序排序
- 容易遗忘的Comparable:一个结果集List,里面有很多的对象,要求根据对象的指定(age)字段进行排序
- java中list里面存放map,根据map中的某一个字段进行排序
- java 两个相同元素的List,合并排序去除重复元素的快速算法
- list排序,如果A字段相同,则以B字段进行排序
- 用coredata碰上需要根据两个字段的和进行排序
- List集合中删除属性(一个或者多个属性)相同的对象返回List且根据对象属性进行排序输出
- 泛型列表根据某个字段进行排序的用法
- 三种对list排序的方法,并实现动态传递字段名对list进行排序
- 相同结构的两个表中相同字段的数据相加,不相同的字段合并