如果您的班级Employee
定义如下:
public class Employee {
private int id;
public Employee(int id){
this.id = id;
}
public int getId() {
return id;
}
@Override
public String toString() {
return "Id : " + this.id;
}
@Override
public boolean equals(Object obj) {
return (obj instanceof Employee) && this.id == ((Employee)obj).getId();
}
在
方法中,您可以检索不匹配的内容,如下所示:
public static void main( String[] args )
{
List<Employee> l1 = new ArrayList<Employee>();
l1.add(new Employee(1));
l1.add(new Employee(2));
l1.add(new Employee(3));
l1.add(new Employee(4));
l1.add(new Employee(5));
List<Employee> l2 = new ArrayList<Employee>();
l2.add(new Employee(4));
l2.add(new Employee(5));
l1.removeAll(l2);
System.out.println(l1);
}
这将打印:[Id : 1, Id : 2, Id : 3]
请注意,对于此项工作,您必须覆盖equals
方法。
问题是这段代码: -
if(!list2.contains(l1.getID())){
您的list2
是ArrayList<Employee>
,您正在检查对l1.getId()
的遏制。因此,您的if
条件将始终为真。
您应该覆盖equals
课程中的hashCode
和Employee
方法,并使用: -
if(!list2.contains(l1))
用于检查list2是否包含员工l1
。
为什么在将id
添加到列表之前将其设置为Employee
3次。它不会添加所有三个id,而只会添加一个e2.setID("1");
e2.setID("2");
e2.setID("4");
list.add(e2); // Will only add one Employee with id = 4
,其中最后一个值设置为id。你需要纠正它: -
{{1}}
向您的员工添加equals
方法,根据他们的ID对其进行比较,制作list2
的副本并致电removeAll
:
List<Employee> list1 = ...;
List<Employee> list2 = ...;
List<Employee> unmatchedList = new ArrayList<Employee>(list2);
unmatchedList.removeAll(list1);
有关完整示例,请参阅@ Dimitri的回答。
我认为你应该在你的场景中使用Set
。可能这个例子对你有帮助。
Employee
班
package com.yourcomp;
/**
* <br>
* <div style="width:600px;text-align:justify;">
*
* TODO: Class comment.
*
* </div>
* <br>
*
*/
public class Employee {
private int id;
/**
* Constructor for Employee. <tt></tt>
*/
public Employee() {
this(-1);
}
/**
* Constructor for Employee. <tt></tt>
*/
public Employee(int id) {
this.id = id;
}
/**
* Gets the id.
*
* @return <tt> the id.</tt>
*/
public int getId() {
return id;
}
/**
* Sets the id.
*
* @param id <tt> the id to set.</tt>
*/
public void setId(int id) {
this.id = id;
}
@Override
public boolean equals(Object obj) {
return this.id == ((Employee)obj).id;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Employee [id=" + id + "]";
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
// TODO Auto-generated method stub
return new Integer(id).hashCode();
}
}
TestEmployee
类:
package com.yourcomp;
import java.util.HashSet;
import java.util.Set;
/**
* <br>
* <div style="width:600px;text-align:justify;">
*
* TODO: Class comment.
*
* </div>
* <br>
*
*/
public class TestEmployee {
public static void main(String[] args) {
// creating the first set with employees having ID's 1 to 5
Set<Employee> set1 = new HashSet<Employee>();
for(int i=1;i<5;i++)
set1.add(new Employee(i));
System.out.println(set1); // printing it
// creating the first set with employees having ID's 3 to 8.
// note that now you have two employees with same ID, 3 & 4
Set<Employee> set2 = new HashSet<Employee>();
for(int i=3;i<8;i++)
set2.add(new Employee(i));
System.out.println(set2);// printing the second set
// creates a final set to contain all elements from above two sets without duplicates
Set<Employee> finalSet = new HashSet<Employee>();
for(Employee employee:set1)
finalSet.add(employee); // adds first set content
for(Employee employee:set2)
finalSet.add(employee); // adds second set content. If any duplicates found, it will be overwritten
System.out.println(finalSet); // prints the final set
}
}
和ouput
[Employee [id=1], Employee [id=2], Employee [id=3], Employee [id=4]]
[Employee [id=3], Employee [id=4], Employee [id=5], Employee [id=6], Employee [id=7]]
[Employee [id=1], Employee [id=2], Employee [id=3], Employee [id=4], Employee [id=5], Employee [id=6], Employee [id=7]]