如何将一个对象集合按照指定的标志(例如ID)进行合并去重的问题

**

如何将一个对象集合按照指定的标志(例如ID)进行合并去重的问题**@yl

最近遇到这样一个需求,后台接口从数据库中查询出一组对象的集合,然后需要将这组数据按照它们的id进行去重,并且需要把id相同的对象的某一个字段数据做加法。这里举个例子:

pojo类:

private class Employee{
	private int id;//编号
	private String name;//姓名
	private double inCome;//收入

	/*
	get()/set()方法
	*/
}

下面是实现方法:

public class Test{
	public static void main(String args[]){
	//首先新建一些数据
	Employee e1 = new Employee();
	e1.setId(1001);
	e1.setName("张三");
	e1.setInCome(3000.0);

	Employee e2 = new Employee();
	e2.setId(1002);
	e2.setName("李四");
	e2.setInCome(5000.0);

    Employee e3 = new Employee();
	e3.setId(1003);
	e3.setName("王五");
	e3.setInCome(8000.0);
	}

	Employee e4 = new Employee();
	e4.setId(1001);
	e4.setName("张三");
	e4.setInCome(4000.0);
	}

	Employee e5 = new Employee();
	e5.setId(1002);
	e5.setName("李四");
	e5.setInCome(2000.0);

	List<Employee> list = new ArrayList<Employy>();
	list.add(e1);
	list.add(e2);
	list.add(e3);
	list.add(e4);
	list.add(e5);

	//接下来对这个集合去重
	List<Employee> theList = getNewList(list);

	private static List<Employee> getNewList(List<Employee> list){
		HashMap<Integer,Employee> map = new HashMap<Integer,Employee>();
	    for(Employee emp : list){
	        int temp = emp.getId();
	        if(map.containsKey(temp)){
				Employee e = new Employee();
				e.setId(temp);
				e.setName(emp.getName());
				e.setInCome(emp.getInCome() + map.get(temp).getInCome())
			} else {
				map.put(temp,emp);
			}
	    }
	    //去除重复编号id的list
	    List<Employee> newList = new ArrayList<Employee>();
	    for(Integer temp : map.keySet()){
	    	newList.add(map.get(temp));
	    }
	    return newList;
	}
	}
}
要根据对象的某个属性值去重一个对象集合,可以使用Java 8的Stream API和`collect()`方法结合自定义的`Collector`来实现。以下是一个示例代码: 假设有一个`Person`类,其中包含`id`和`name`两个属性: ```java import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { List<Person> personList = new ArrayList<>(); personList.add(new Person(1, "Alice")); personList.add(new Person(2, "Bob")); personList.add(new Person(1, "Charlie")); personList.add(new Person(3, "Alice")); List<Person> distinctList = personList.stream() .collect(Collectors.collectingAndThen( Collectors.toCollection(() -> new ArrayList<>(personList.size())), list -> list.stream().collect(Collectors.toMap( Person::getId, p -> p, (existing, duplicate) -> existing )))) .values() .stream() .collect(Collectors.toList()); distinctList.forEach(System.out::println); } } class Person { private int id; private String name; public Person(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public String getName() { return name; } @Override public String toString() { return "Person{" + "id=" + id + ", name='" + name + '\'' + '}'; } } ``` 在上述示例中,我们使用了`toMap()`方法来创建一个Map,其中键是对象的某个属性值(这里是`id`),值是对象本身。在`toMap()`方法中,我们提供了一个合并函数,以保留原始对象而不是重复的对象。然后,我们通过获取Map的值集合,并将其转换为List,得到了根据`id`属性去重对象集合。 输出结果如下: ``` Person{id=1, name='Alice'} Person{id=2, name='Bob'} Person{id=3, name='Alice'} ``` 注意,这里使用了`collectingAndThen()`方法来在收集结束后执行一些操作,即将结果转换为List。这样可以避免直接对`toMap()`的结果进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值