Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.stackoverflow.questions.Q1440134;
- import org.jetbrains.annotations.NotNull;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- /**
- * @see [http://stackoverflow.com/questions/1440134/when-should-a-class-be-comparable-and-or-comparator]
- * @see [http://www.digizol.com/2008/07/java-sorting-comparator-vs-comparable.html]
- * @see [http://java-journal.blogspot.in/2011/01/when-to-use-comparable-and-when-to-use.html]
- * <p>
- * If we have only one single sorting criteria to sort our elements then we can use 'Comparable'
- * but if we have more than one sorting criterias then we have to go for 'Comparator' "also".
- * <p>
- * Said differently, it's more that we use 'Comparable' if there is only one single obvious thing to compare on.
- * And even then we can just write a 'Comparator'. If we have two different programs using the same class,
- * and one wants to sort by just age and the other by just name, at least one of them will need to use a
- * Comparator.
- * <p>
- * java.lang.Comparable: int compareTo(Object o1)
- * ----------------------------------------------
- * This method compares this object with o1 object. Returned int value has the following meanings.
- * 1. positive – this object is greater than o1
- * 2. zero – this object equals to o1
- * 3. negative – this object is less than o1
- * <p>
- * java.util.Comparator: int compare(Object o1, Objecto2)
- * ------------------------------------------------------
- * This method compares o1 and o2 objects. Returned int value has the following meanings.
- * 1. positive – o1 is greater than o2
- * 2. zero – o1 equals to o2
- * 3. negative – o1 is less than o2
- */
- public class ComparableVsComparator {
- public static void main(String[] args) {
- List<Employee> employeeList = createEmployeesAndAddThemToAList();
- System.out.println(employeeList.size() + " employees created!");
- System.out.println("EMPLOYEE LIST(UNSORTED):");
- employeeList.forEach(System.out::println);
- System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
- /**
- * Sorting in natural ordering
- */
- Collections.sort(employeeList);
- System.out.println("EMPLOYEE LIST(SORTED AS PER NATURAL ORDER):");
- employeeList.forEach(System.out::println);
- System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
- /**
- * Sorting Employee types by other fields(for example: sort by 'age'
- * See 'AgeComparator' which sorts Employees by their age by implementing
- * 'java.util.Comparator'
- */
- System.out.println("EMPLOYEE LIST(SORTED BY AGE):");
- Collections.sort(employeeList, new AgeComparator());
- employeeList.forEach(System.out::println);
- /**
- * Even the ordering by 'empId' (previously done using Comparable) can be implemented using 'Comparator';
- * by thefollowing class
- *<code>
- * class IdComparator implements Comparator<Employee>{
- * public int compare(Employee o1, Employee o2) {
- * return o1.getEmpId() - o2.getEmpId();
- * }
- * }
- * </code>
- *
- * and then
- *
- * Collections.sort(employeeList, new IdComparator());
- */
- }
- private static List<Employee> createEmployeesAndAddThemToAList() {
- return Arrays.asList(
- new Employee(5, "Frank", 28),
- new Employee(1, "Jorge", 19),
- new Employee(6, "Bill", 34),
- new Employee(3, "Michel", 10),
- new Employee(7, "Simpson", 8),
- new Employee(4, "Clerk", 16),
- new Employee(8, "Lee", 40),
- new Employee(2, "Mark", 30));
- }
- }
- class Employee implements Comparable<Employee> {
- protected int empId;
- protected String name;
- protected int age;
- /**
- * @param empId Employee Id
- * @param name Employee Name
- * @param age Employee Age
- */
- public Employee(int empId, String name, int age) {
- this.empId = empId;
- this.name = name;
- this.age = age;
- }
- @Override
- public String toString() {
- return "Employee{" +
- "empId=" + empId +
- ", name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
- /**
- * Sorting in natural ordering
- *
- * @param employee an Employee instance
- * @return compared value
- */
- @Override
- public int compareTo(@NotNull Employee employee) {
- return this.empId - employee.empId;
- }
- /* Accesors and mutators */
- }
- @SuppressWarnings("unused")
- class AgeComparator implements Comparator<Employee> {
- @Override
- public int compare(Employee employee1, Employee employee2) {
- return employee1.age < employee2.age ? -1 : employee1.age == employee2.age ? 0 : 1;
- }
- }
Add Comment
Please, Sign In to add comment