diff --git a/core-java-modules/core-java-collections-4/pom.xml b/core-java-modules/core-java-collections-4/pom.xml
index aaf63a2b2a..8060e4f606 100644
--- a/core-java-modules/core-java-collections-4/pom.xml
+++ b/core-java-modules/core-java-collections-4/pom.xml
@@ -25,5 +25,4 @@
2.2
-
\ No newline at end of file
diff --git a/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/collections/sorting/Employee.java b/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/collections/sorting/Employee.java
index d41ad54295..a9e7bc8a38 100644
--- a/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/collections/sorting/Employee.java
+++ b/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/collections/sorting/Employee.java
@@ -4,69 +4,48 @@ import java.util.Date;
public class Employee implements Comparable{
- private String name;
- private int age;
- private double salary;
- private Date joiningDate;
+ private String name;
+ private Date joiningDate;
- public Employee(String name, int age, double salary, Date joiningDate) {
- this.name = name;
- this.age = age;
- this.salary = salary;
- this.joiningDate = joiningDate;
- }
+ public Employee(String name, Date joiningDate) {
+ this.name = name;
+ this.joiningDate = joiningDate;
+ }
- public String getName() {
- return name;
- }
+ public String getName() {
+ return name;
+ }
- public void setName(String name) {
- this.name = name;
- }
+ public void setName(String name) {
+ this.name = name;
+ }
- public int getAge() {
- return age;
- }
+ public Date getJoiningDate() {
+ return joiningDate;
+ }
- public void setAge(int age) {
- this.age = age;
- }
+ public void setJoiningDate(Date joiningDate) {
+ this.joiningDate = joiningDate;
+ }
- public double getSalary() {
- return salary;
- }
+ @Override
+ public boolean equals(Object obj) {
+ return ((Employee) obj).getName()
+ .equals(getName());
+ }
- public void setSalary(double salary) {
- this.salary = salary;
- }
+ @Override
+ public String toString() {
+ return new StringBuffer().append("(")
+ .append(getName())
+ .append(",")
+ .append(getJoiningDate())
+ .append(")")
+ .toString();
+ }
- public Date getJoiningDate() {
- return joiningDate;
- }
-
- public void setJoiningDate(Date joiningDate) {
- this.joiningDate = joiningDate;
- }
-
- @Override
- public boolean equals(Object obj) {
- return ((Employee) obj).getName()
- .equals(getName());
- }
-
- @Override
- public String toString() {
- return new StringBuffer().append("(")
- .append(getName()).append(",")
- .append(getAge())
- .append(",")
- .append(getSalary()).append(",").append(getJoiningDate())
- .append(")")
- .toString();
- }
-
- @Override
- public int compareTo(Employee employee) {
- return getJoiningDate().compareTo(employee.getJoiningDate());
- }
+ @Override
+ public int compareTo(Employee employee) {
+ return getJoiningDate().compareTo(employee.getJoiningDate());
+ }
}
diff --git a/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/sorting/EmployeeSortingByDateUnitTest.java b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/sorting/EmployeeSortingByDateUnitTest.java
index b8acb789a6..250fb7b62b 100644
--- a/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/sorting/EmployeeSortingByDateUnitTest.java
+++ b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/sorting/EmployeeSortingByDateUnitTest.java
@@ -14,133 +14,132 @@ import org.junit.Test;
public class EmployeeSortingByDateUnitTest {
- private List employees = new ArrayList<>();
- private List employeesSortedByDateAsc = new ArrayList<>();
- private List employeesSortedByDateDesc = new ArrayList<>();
+ private List employees = new ArrayList<>();
+ private List employeesSortedByDateAsc = new ArrayList<>();
+ private List employeesSortedByDateDesc = new ArrayList<>();
- @Before
- public void initVariables() {
+ @Before
+ public void initVariables() {
- Collections.addAll(employees,
- new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)),
- new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)),
- new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)),
- new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)),
- new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1)),
- new Employee("John", 23, 5000, new Date())
- );
+ Collections.addAll(employees,
+ new Employee("Earl", DateUtils.addMonths(new Date(), -2)),
+ new Employee("Frank", DateUtils.addDays(new Date(), -20)),
+ new Employee("Steve", DateUtils.addDays(new Date(), -10)),
+ new Employee("Jessica", DateUtils.addMonths(new Date(), -6)),
+ new Employee("Pearl", DateUtils.addYears(new Date(), -1)),
+ new Employee("John", new Date())
+ );
- Collections.addAll(employeesSortedByDateDesc,
- new Employee("John", 23, 5000, new Date()),
- new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)),
- new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)),
- new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)),
- new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)),
- new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1))
- );
+ Collections.addAll(employeesSortedByDateDesc,
+ new Employee("John", new Date()),
+ new Employee("Steve", DateUtils.addDays(new Date(), -10)),
+ new Employee("Frank", DateUtils.addDays(new Date(), -20)),
+ new Employee("Earl", DateUtils.addMonths(new Date(), -2)),
+ new Employee("Jessica", DateUtils.addMonths(new Date(), -6)),
+ new Employee("Pearl", DateUtils.addYears(new Date(), -1))
+ );
- Collections.addAll(employeesSortedByDateAsc,
- new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1)),
- new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)),
- new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)),
- new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)),
- new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)),
- new Employee("John", 23, 5000, new Date())
- );
- }
+ Collections.addAll(employeesSortedByDateAsc,
+ new Employee("Pearl", DateUtils.addYears(new Date(), -1)),
+ new Employee("Jessica", DateUtils.addMonths(new Date(), -6)),
+ new Employee("Earl", DateUtils.addMonths(new Date(), -2)),
+ new Employee("Frank", DateUtils.addDays(new Date(), -20)),
+ new Employee("Steve", DateUtils.addDays(new Date(), -10)),
+ new Employee("John", new Date())
+ );
+ }
- @Test
- public void givenEmpList_SortEmpList_thenSortedListinNaturalOrder() {
- Collections.sort(employees);
+ @Test
+ public void givenEmpList_SortEmpList_thenSortedListinNaturalOrder() {
+ Collections.sort(employees);
+ System.out.println(employees);
- assertEquals(employees, employeesSortedByDateAsc);
- }
+ assertEquals(employees, employeesSortedByDateAsc);
+ }
- @Test
- public void givenEmpList_SortEmpList_thenCheckSortedList() {
+ @Test
+ public void givenEmpList_SortEmpList_thenCheckSortedList() {
- Collections.sort(employees, new Comparator() {
- public int compare(Employee o1, Employee o2) {
- return o1.getJoiningDate().compareTo(o2.getJoiningDate());
- }
- });
+ Collections.sort(employees, new Comparator() {
+ public int compare(Employee o1, Employee o2) {
+ return o1.getJoiningDate().compareTo(o2.getJoiningDate());
+ }
+ });
- assertEquals(employees, employeesSortedByDateAsc);
- }
+ assertEquals(employees, employeesSortedByDateAsc);
+ }
- @Test
- public void givenEmpList_SortEmpList_thenCheckSortedListV1() {
+ @Test
+ public void givenEmpList_SortEmpList_thenCheckSortedListV1() {
- Collections.sort(employees, new Comparator() {
- public int compare(Employee emp1, Employee emp2) {
- if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null)
- return 0;
- return emp1.getJoiningDate().compareTo(emp2.getJoiningDate());
- }
- });
+ Collections.sort(employees, new Comparator() {
+ public int compare(Employee emp1, Employee emp2) {
+ if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null)
+ return 0;
+ return emp1.getJoiningDate().compareTo(emp2.getJoiningDate());
+ }
+ });
- assertEquals(employees, employeesSortedByDateAsc);
- }
+ assertEquals(employees, employeesSortedByDateAsc);
+ }
- @Test
- public void givenEmpList_SortEmpList_thenSortedListinAscOrder() {
- Collections.sort(employees, Collections.reverseOrder());
+ @Test
+ public void givenEmpList_SortEmpList_thenSortedListinDescOrder() {
+ Collections.sort(employees, Collections.reverseOrder());
- assertEquals(employees, employeesSortedByDateDesc);
- }
+ assertEquals(employees, employeesSortedByDateDesc);
+ }
- @Test
- public void givenEmpList_SortEmpList_thenCheckSortedListAsc() {
+ @Test
+ public void givenEmpList_SortEmpList_thenCheckSortedListAsc() {
- Collections.sort(employees, new Comparator() {
- public int compare(Employee emp1, Employee emp2) {
- return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
- }
- });
+ Collections.sort(employees, new Comparator() {
+ public int compare(Employee emp1, Employee emp2) {
+ return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
+ }
+ });
- assertEquals(employees, employeesSortedByDateDesc);
- }
+ assertEquals(employees, employeesSortedByDateDesc);
+ }
- @Test
- public void givenEmpList_SortEmpList_thenCheckSortedListAscV1() {
+ @Test
+ public void givenEmpList_SortEmpList_thenCheckSortedListDescV1() {
- Collections.sort(employees, new Comparator() {
- public int compare(Employee emp1, Employee emp2) {
- if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null)
- return 0;
- return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
- }
- });
+ Collections.sort(employees, new Comparator() {
+ public int compare(Employee emp1, Employee emp2) {
+ return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
+ }
+ });
- assertEquals(employees, employeesSortedByDateDesc);
- }
+ assertEquals(employees, employeesSortedByDateDesc);
+ }
- @Test
- public void givenEmpList_SortEmpList_thenCheckSortedListDescLambda() {
+ @Test
+ public void givenEmpList_SortEmpList_thenCheckSortedListDescLambda() {
- Collections.sort(employees,
- (emp1, emp2) -> emp2.getJoiningDate().compareTo(emp1.getJoiningDate()));
+ Collections.sort(employees,
+ (emp1, emp2) -> emp2.getJoiningDate().compareTo(emp1.getJoiningDate()));
- assertEquals(employees, employeesSortedByDateDesc);
- }
+ assertEquals(employees, employeesSortedByDateDesc);
+ }
- @Test
- public void givenEmpList_SortEmpList_thenCheckSortedListDescLambdaV1() {
+ @Test
+ public void givenEmpList_SortEmpList_thenCheckSortedListDescLambdaV1() {
- Collections.sort(employees, (emp1, emp2) -> {
- if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null)
- return 0;
- return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
- });
+ Collections.sort(employees, (emp1, emp2) -> {
+ if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null)
+ return 0;
+ return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
+ });
- assertEquals(employees, employeesSortedByDateDesc);
- }
+ assertEquals(employees, employeesSortedByDateDesc);
+ }
- @Test
- public void givenEmpList_SortEmpList_thenCheckSortedListAscLambda() {
- Collections.sort(employees,
- Comparator.comparing(Employee::getJoiningDate));
- assertEquals(employees, employeesSortedByDateAsc);
- }
+ @Test
+ public void givenEmpList_SortEmpList_thenCheckSortedListAscLambda() {
+ Collections.sort(employees,
+ Comparator.comparing(Employee::getJoiningDate));
+ assertEquals(employees, employeesSortedByDateAsc);
+ }
}