Java中向列表中添加多个NULL
我有两个列表,希望将某些元素从一个列表复制到另一个列表,即,有新旧员工列表。我需要合并两个列表,并删除包含在旧列表中但不包含在新列表中的元素 我可以通过使用TreeSet和覆盖Employees类的equals和hashcode函数来解决获取并集和交集的问题 现在,我想排除旧元素而不是新元素,并将它们添加到“deletedList”…我得到了“ConcurrentModificationException” 我尝试了这个而不是“迭代器”,但结果相同:for(Employees e:employeesListDB) 我还尝试了“CopyOnWriteArrayList”而不是“ArrayList”,但没有改变 但是现在的问题是,在初始化空列表“deletedList”时,它在add函数之前被多个null元素填充 代码如下:Java中向列表中添加多个NULL,java,list,concurrency,null,add,Java,List,Concurrency,Null,Add,我有两个列表,希望将某些元素从一个列表复制到另一个列表,即,有新旧员工列表。我需要合并两个列表,并删除包含在旧列表中但不包含在新列表中的元素 我可以通过使用TreeSet和覆盖Employees类的equals和hashcode函数来解决获取并集和交集的问题 现在,我想排除旧元素而不是新元素,并将它们添加到“deletedList”…我得到了“ConcurrentModificationException” 我尝试了这个而不是“迭代器”,但结果相同:for(Employees e:employe
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);
}
}
要添加的新列表:
[
{
"email":"ali.moustafa@x.com"
},
{
"email":"sara.ahmed@x.com"
},
{
"email":"emad.hamed@x.com"
}
]
我想要的已删除列表:
[{
“电子邮件”:“mariam。moustafa@x.com" }, {
“电子邮件”:“阿里。hassan@x.com" }, {
“电子邮件”:“hoosen.imam-ally@x.com" }, {
“电子邮件”:“艾伦。randall@x.com" }, {
“电子邮件”:“nishaan。maharaj@x.com" } ] Sara邮件将被更新 Employee类有两个字段{id,email}新列表(要添加到db的列表)是一个仅包含电子邮件的列表,id字段尚未识别,但旧列表具有完整的bean字段…要在这两个列表之间进行比较,我应该覆盖比较器以忽略id字段
只是我需要知道,为什么当我使用set.add操作时,它只添加唯一的电子邮件!列表的原始大小是36个元素,在将其添加到一个集合后,它只会变为16
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);
Set oldSet=new TreeSet(new EmployeeComparator());
oldSet.addAll(employeesListDB);
Set newSet=new TreeSet(new EmployeeComparator());
newSet.addAll(员工列表);
Set deleted=Set.difference(旧集、新闻集);
添加之前填充了多个空元素的空列表
功能
这是因为您使用的是包含以下常量的ArrayList
:
private static final int DEFAULT_CAPACITY = 10;
这意味着当您使用new
操作符创建ArrayList
时,实际上创建了T
的数组,其中包含10个null
s(它包含为private transient Object[/elementData
字段)
委员会:
程序中的每个变量都必须有一个值,然后才能确定其值
使用:
每个类变量、实例变量或数组组件在创建时都会使用默认值进行初始化(§15.9、§15.10.2):
[……]
对于所有引用类型(§4.3),默认值为空。
添加之前填充了多个空元素的空列表
功能
这是因为您使用的是包含以下常量的ArrayList
:
private static final int DEFAULT_CAPACITY = 10;
这意味着当您使用new
操作符创建ArrayList
时,实际上创建了T
的数组,其中包含10个null
s(它包含为private transient Object[/elementData
字段)
委员会:
程序中的每个变量都必须有一个值,然后才能确定其值
使用:
每个类变量、实例变量或数组组件在创建时都会使用默认值进行初始化(§15.9、§15.10.2):
[……]
对于所有引用类型(§4.3),默认值为空。
据我所知,您需要旧集合包含而新集合不包含的所有元素
为此,您可以使用以下方法:
据我所知,您需要旧集合包含而新集合不包含的所有元素
为此,您可以使用以下方法:
下面是一个核心Java解决方案,它使用了两个简单的步骤:
[1]-创建一组setOld
,其中包含第一组电子邮件
[2]-从setOld
中减去一组新的电子邮件setNew
Set oldSet<String> = new HashSet<String>(); // original set of email addresses
oldSet.add("mariam.moustafa@x.com");
oldSet.add("sara.ahmed@x.com");
oldSet.add("ali.hassan@x.com");
oldSet.add("hoosen.imam-ally@x.com");
oldSet.add("allan.randall@x.com");
oldSet.add("nishaan.maharaj@x.com");
Set newSet<String> = new HashSet<String>(); // new set of email addresses
newSet.add("ali.moustafa@x.com");
newSet.add("sara.ahmed@x.com");
newSet.add("emad.hamed@x.com");
for (String s : newSet) {
oldSet.remove(s); // this will only remove the element if found
}
// display new contents of oldSet
for (String s : oldSet) {
System.out.println(s);
}
下面是一个核心Java解决方案,它使用了两个简单的步骤:
[1]-创建一组setOld
,其中包含第一组电子邮件
[2]-从setOld
中减去一组新的电子邮件setNew
Set oldSet<String> = new HashSet<String>(); // original set of email addresses
oldSet.add("mariam.moustafa@x.com");
oldSet.add("sara.ahmed@x.com");
oldSet.add("ali.hassan@x.com");
oldSet.add("hoosen.imam-ally@x.com");
oldSet.add("allan.randall@x.com");
oldSet.add("nishaan.maharaj@x.com");
Set newSet<String> = new HashSet<String>(); // new set of email addresses
newSet.add("ali.moustafa@x.com");
newSet.add("sara.ahmed@x.com");
newSet.add("emad.hamed@x.com");
for (String s : newSet) {
oldSet.remove(s); // this will only remove the element if found
}
// display new contents of oldSet
for (String s : oldSet) {
System.out.println(s);
}
这样尝试(制作一个小测试用例):
输出:
sara.ahmed@x.com
ali.moustafa@x.com
emad.hamed@x.com
这样尝试(制作一个小测试用例):
输出:
sara.ahmed@x.com
ali.moustafa@x.com
emad.hamed@x.com
使用List removeAll方法。您需要重写Employees类中的equals方法。基于员工id的PFB示例代码段,您需要根据电子邮件id对其进行修改:
import java.util.*;
public class StringArray {
public static void main(String args[]) {
List<Employee> oldList = new ArrayList<Employee>();
oldList.add(new Employee(1));
oldList.add(new Employee(2));
oldList.add(new Employee(3));
oldList.add(new Employee(4));
List<Employee> newList = new ArrayList<Employee>();
newList.add(new Employee(3));
newList.add(new Employee(4));
newList.add(new Employee(5));
newList.add(new Employee(6));
oldList.removeAll(newList);
System.out.println("Printing delete list");
for (Employee employee : oldList)
System.out.println(employee);
System.out.println("Printing updated list");
for (Employee employee : newList)
System.out.println(employee);
}
}
public class Employee {
private int id;
public Employee(int id) {
super();
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Employee [id=" + this.id + "]";
}
@Override
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Employee))
return false;
Employee c = (Employee) o;
return this.id == c.id;
}
}
import java.util.*;
公共类字符串数组{
公共静态void main(字符串参数[]){
List oldList=new ArrayList();
新增(新员工(1));
新增(新员工(2));
新增(新员工(3人));
新增(新员工4人);
List newList=newarraylist();
新增(新员工(3));
新增(新增员工4人);
新增(新增员工5人);
新增(新增员工6人);
oldList.removeAll(newList);
System.out.println(“打印删除列表”);
用于(员工:旧列表)
系统输出打印项次(员工);
System.out.println(“打印更新列表”);
对于(员工:新列表)
系统输出打印项次(员工);
}
}
公营雇员{
私有int-id;
公共雇员(内部id){
超级();
this.id=id;
}
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
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("mariam.moustafa@x.com"));
oldList.add(createEmployee("sara.ahmed@x.com"));
oldList.add(createEmployee("ali.hassan@x.com"));
oldList.add(createEmployee("hoosen.imam-ally@x.com"));
oldList.add(createEmployee("allan.randall@x.com"));
oldList.add(createEmployee("nishaan.maharaj@x.com"));
newMails.add("ali.moustafa@x.com");
newMails.add("sara.ahmed@x.com");
newMails.add("emad.hamed@x.com");
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());
}
class Employee {
String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
sara.ahmed@x.com
ali.moustafa@x.com
emad.hamed@x.com
import java.util.*;
public class StringArray {
public static void main(String args[]) {
List<Employee> oldList = new ArrayList<Employee>();
oldList.add(new Employee(1));
oldList.add(new Employee(2));
oldList.add(new Employee(3));
oldList.add(new Employee(4));
List<Employee> newList = new ArrayList<Employee>();
newList.add(new Employee(3));
newList.add(new Employee(4));
newList.add(new Employee(5));
newList.add(new Employee(6));
oldList.removeAll(newList);
System.out.println("Printing delete list");
for (Employee employee : oldList)
System.out.println(employee);
System.out.println("Printing updated list");
for (Employee employee : newList)
System.out.println(employee);
}
}
public class Employee {
private int id;
public Employee(int id) {
super();
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Employee [id=" + this.id + "]";
}
@Override
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Employee))
return false;
Employee c = (Employee) o;
return this.id == c.id;
}
}