java小算法介绍-合并List中满足某些字段相同的记录

《Java基础技术》 专栏收录该内容
22 篇文章 1 订阅

java小算法介绍-合并List中满足某些字段相同的记录

    有这么一个需求:List中存放的是一个对象,如Student,里面有n个字段,现在想合并列表中满足一些字段相等的情况下合并其中的金额等其他字段。如:

    Student的字段:

 

class Student{
		private String store;
		private int money;
		private Date date;
		private Integer status=0;
		
		public String getStore() {
			return store;
		}
		public void setStore(String store) {
			this.store = store;
		}
		public int getMoney() {
			return money;
		}
		public void setMoney(int money) {
			this.money = money;
		}
		public Date getDate() {
			return date;
		}
		public void setDate(Date date) {
			this.date = date;
		}
		public Student() {
			super();
		}
		/*public Student(String store, int money, Date date,Integer status) {
			super();
			this.store = store;
			this.money = money;
			this.date = date;
			this.status=status;
		}*/
		
		@Override
		public String toString() {
			return "\nStudent [store=" + store + ", money=" + money + ", date="
					+ DateUtil.formatDate(date, "yyyy-MM-dd") + ", status=" + status + "]\n";
		}
		
		public Student(String store, int money, Date date) {
			super();
			this.store = store;
			this.money = money;
			this.date = date;
		}
		public Integer getStatus() {
			return status;
		}
		public void setStatus(Integer status) {
			this.status = status;
		}
	}


    现在想根据: store与date相同的记录合并其中的金额到第一次出现的那条记录中(可用于后续插入数据库等处理)。在这里我额外加上了非业务上需求的status字段,即状态位,用于标识该记录是否已经被合并过了。

 

    我的思路是这样: 遍历每条记录,取出当前记录的status与store、date值,当status不等于1,即还没被合并的前提下,遍历后面的所有记录,在还没被合并的前提下,判断是否有store跟state等于当前记录下来的store和date值,如果有,则合并到当前记录,并记录被合并的记录的status=1,标识已经被合并了。

    talk is cheap,now show you my code:

 

        @Test
	public void testList() throws Exception{
		String format="yyyy-MM-dd";
		Student student1=new Student("bb",12,DateUtil.formatString("2017-06-01", format));
		Student student2=new Student("aa",13 ,DateUtil.formatString("2017-06-01", format));
		Student student3=new Student("bb",12,DateUtil.formatString("2017-06-01", format));
		Student student4=new Student("dd",14,DateUtil.formatString("2017-06-02", format));
		Student student5=new Student("cc",15,DateUtil.formatString("2017-06-03", format));
		Student student6=new Student("bb",16,DateUtil.formatString("2017-06-03", format));
		Student student7=new Student("bb",17,DateUtil.formatString("2017-06-03", format));
		Student student8=new Student("dd",18,DateUtil.formatString("2017-06-04", format));
		Student student9=new Student("cc",19,DateUtil.formatString("2017-06-04", format));
		Student student10=new Student("dd",20,DateUtil.formatString("2017-06-04", format));
		Student student11=new Student("aa",22,DateUtil.formatString("2017-06-01", format));
		
		List<Student> list=Lists.newArrayList();
		list.add(student1);
		list.add(student2);
		list.add(student3);
		list.add(student4);
		list.add(student5);
		list.add(student6);
		list.add(student7);
		list.add(student8);
		list.add(student9);
		list.add(student10);
		list.add(student11);

		System.out.println("合并前list: "+list);
		
		for(int i=0;i<list.size();i++){
			Student student=list.get(i);
			if (!student.getStatus().equals(1)) {
				String store=student.getStore();
				Date date=student.getDate();
				
				for (int j = i+1; j < list.size(); j++) {
					if (!list.get(j).getStatus().equals(1)) {
						Student mergeStudent=list.get(j);
						if (mergeStudent.getStore().equals(store) && DateUtils.isSameDay(date, mergeStudent.getDate())) {
							student.setMoney(student.getMoney()+mergeStudent.getMoney());
							
							mergeStudent.setStatus(1);
						}
					}
				}
			}
		}
		
		System.out.println();
		System.out.println("合并后list: "+list);
	}

   
    结果:

 

 

合并前list: [
Student [store=bb, money=12, date=2017-06-01, status=0]
, 
Student [store=aa, money=13, date=2017-06-01, status=0]
, 
Student [store=bb, money=12, date=2017-06-01, status=0]
, 
Student [store=dd, money=14, date=2017-06-02, status=0]
, 
Student [store=cc, money=15, date=2017-06-03, status=0]
, 
Student [store=bb, money=16, date=2017-06-03, status=0]
, 
Student [store=bb, money=17, date=2017-06-03, status=0]
, 
Student [store=dd, money=18, date=2017-06-04, status=0]
, 
Student [store=cc, money=19, date=2017-06-04, status=0]
, 
Student [store=dd, money=20, date=2017-06-04, status=0]
, 
Student [store=aa, money=22, date=2017-06-01, status=0]
]

合并后list: [
Student [store=bb, money=24, date=2017-06-01, status=0]
, 
Student [store=aa, money=35, date=2017-06-01, status=0]
, 
Student [store=bb, money=12, date=2017-06-01, status=1]
, 
Student [store=dd, money=14, date=2017-06-02, status=0]
, 
Student [store=cc, money=15, date=2017-06-03, status=0]
, 
Student [store=bb, money=33, date=2017-06-03, status=0]
, 
Student [store=bb, money=17, date=2017-06-03, status=1]
, 
Student [store=dd, money=38, date=2017-06-04, status=0]
, 
Student [store=cc, money=19, date=2017-06-04, status=0]
, 
Student [store=dd, money=20, date=2017-06-04, status=1]
, 
Student [store=aa, money=22, date=2017-06-01, status=1]
]


    好了,就介绍到这里吧!下面继续介绍我的ssm框架整合实战-博客系统开发(个人QQ:1948831260

 

  • 1
    点赞
  • 2
    评论
  • 2
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值