I think you should use Set for your scenario. Perhaps these examples will help you.
Employee Class
package com.yourcomp; public class Employee { private int id; public Employee() { this(-1); } public Employee(int id) { this.id = id; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public boolean equals(Object obj) { return this.id == ((Employee)obj).id; } @Override public String toString() { return "Employee [id=" + id + "]"; } @Override public int hashCode() {
Class TestEmployee :
package com.yourcomp; import java.util.HashSet; import java.util.Set; public class TestEmployee { public static void main(String[] args) {
and conclusion
[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]]
source share