我有2个列表,想要将某些元素从一个元素复制到另一个元素,即有新老员工列表,我需要合并2个列表并删除包含在旧列表中但不包含在新列表中的元素。
我可以通过使用TreeSet解决覆盖并集和交集的部分,并覆盖Employees类的equals和hashcode函数...。
现在,我要排除旧的元素,而不是新的元素,然后将它们添加到“ deletedList” ...中。我得到了“ ConcurrentModificationException”
我尝试了此方法,而不是“ iterator”,但结果相同:for(Employees e:employeeListDB)
我也尝试了“ CopyOnWriteArrayList”而不是“ ArrayList”,但是没有改变!
但是现在的问题是,在空列表“ deletedList”的初始化时,在添加功能之前,它用多个空元素填充了!
这是代码:
List<Employees> employeesListDB = this.findAll();
Set<Employees> empSet = new TreeSet<Employees>(new EmployeeComparator());
empSet.addAll(employeesList);
List<Employees> deletedList = new ArrayList<Employees>();
Employees e = new Employees();
ListIterator<Employees> itr = employeesListDB.listIterator();
for(itr.hasNext()) {
e = (Employees)itr.next();
if(!empSet.contains(e)) {
deletedList.add(e);
}
}
一个反例:
旧列表“ employeesListDB”从数据库中列出了雇员列表:
[
{
"email":"[email protected]"
},
{
"email":"[email protected]"
},
{
"email":"[email protected]"
},
{
"email":"[email protected]"
},
{
"email":"[email protected]"
},
{
"email":"[email protected]"
}
]
要添加的新列表:
[
{
"email":"[email protected]"
},
{
"email":"[email protected]"
},
{
"email":"[email protected]"
}
]
我想要的已删除列表:
[
{
“ email”:“ [email protected]”},{
“ email”:“ [email protected]”},{
“ email”:“ [email protected]”} ,{
“ email”:“ [email protected]”},{
“ email”:“ [email protected]”}]
萨拉邮件将被更新...
Employee类有两个字段{id,email},新列表(要添加到db的列表)是仅电子邮件的列表,id字段尚未识别,但旧列表具有完整的bean字段...进行比较在这两个列表之间,我应该重写比较器以忽略id字段;在忽略字段的列表中查找重复项
只是我需要知道,为什么当我使用set.add操作时,它仅添加唯一的电子邮件!将列表添加到集合中后,列表的原始大小为36个元素!
Set<Employees> oldSet = new TreeSet<Employees>(new EmployeeComparator());
oldSet.addAll(employeesListDB);
Set<Employees> newSet = new TreeSet<Employees>(new EmployeeComparator());
newSet.addAll(employeesList);
Set<Employees> deleted = Sets.difference(oldSet, newSet);
以这种方式尝试(制作一个小型TestCase):
private static Employee createEmployee(String string) {
Employee employee = new Employee();
employee.setEmail(string);
return employee;
}
public static void main(String[] args) {
List<String> newMails = new ArrayList<>();
List<Employee> oldList = new ArrayList<>();
oldList.add(createEmployee("[email protected]"));
oldList.add(createEmployee("[email protected]"));
oldList.add(createEmployee("[email protected]"));
oldList.add(createEmployee("[email protected]"));
oldList.add(createEmployee("[email protected]"));
oldList.add(createEmployee("[email protected]"));
newMails.add("[email protected]");
newMails.add("[email protected]");
newMails.add("[email protected]");
List<Employee> delete = new ArrayList<>();
Set<String> removedMails = new HashSet<>();
for (Employee emp : oldList) {
if (!newMails.contains(emp.getEmail())) {
delete.add(emp);
}
removedMails.add(emp.getEmail());
}
newMails.removeAll(removedMails);
// remove emploeyees in delete
oldList.removeAll(delete);
// Create employee for left MAils
for (String newMail : newMails) {
oldList.add(createEmployee(newMail));
}
//Old and new Employees
for (Employee emp : oldList) {
System.out.println(emp.getEmail());
}
}
简单的Employee类:
class Employee {
String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
输出:
[email protected]
[email protected]
[email protected]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句