Home > Java > [Java] Compare two list

[Java] Compare two list

Problem:

I have two classes are Employee and Department


class Department{
private Long deptId;
private String deptName;

public Long getDeptId() {
return deptId;
}
public void setDeptId(Long deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
}

class Employee{
private Long empId;
private String empName;
private Department dept;

public Employee() {
super();
}

public Employee(Long empId, String empName, Department dept) {
super();
this.empId = empId;
this.empName = empName;
this.dept = dept;
}
public Long getEmpId() {
return empId;
}
public void setEmpId(Long empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}

}

And, I write  a class for compare two list with data from Employee class: ListComparisonTest


package tdd.sprint10;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import org.junit.*;

public class ListComparisonTest {

private Department dept;

public ListComparisonTest() {
}

@Before
public void setUp() throws Exception {
dept=new Department();
}

@After
public void tearDown() throws Exception {
}

@Test
public void testCompareList() {
Employee emp1 = new Employee();
emp1.setEmpId(1L);
emp1.setEmpName("Van Huong");
emp1.setDept(dept);

Employee emp2 = new Employee();
emp2.setEmpId(2L);
emp2.setEmpName("Xuan Vinh");
emp2.setDept(dept);

List<Employee> firstList = new ArrayList<Employee>();
firstList.add(emp1);
firstList.add(emp2);

List<Employee> secondList = new ArrayList<Employee>();
secondList.add(new Employee(1L,"Van Huong",dept));
secondList.add(new Employee(2L,"Xuan Vinh",dept));

assertEquals(firstList.size(), secondList.size());
assertTrue(firstList.containsAll(secondList));
}

}

It’s fail when I  run the test class with JUnit Test!

The solution: create hashCode() and equals() functions on Employee class

On Eclipse IDE, press Alt + Shift + S, chose  generate and hashCode() and equals() functions.


@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((dept == null) ? 0 : dept.hashCode());
result = prime * result + ((empId == null) ? 0 : empId.hashCode());
result = prime * result + ((empName == null) ? 0 : empName.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (dept == null) {
if (other.dept != null)
return false;
} else if (!dept.equals(other.dept))
return false;
if (empId == null) {
if (other.empId != null)
return false;
} else if (!empId.equals(other.empId))
return false;
if (empName == null) {
if (other.empName != null)
return false;
} else if (!empName.equals(other.empName))
return false;
return true;
}

Advertisements
Categories: Java
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: