-first we do try to understand Detailed usage of Comparable.
// Employee.java package com.comparable; public class Employee implements Comparable, Serializable { private int eNo; private String eName; private float eSal; public Employee() { super(); } public Employee(int eNo, String eName, float eSal) { super(); this.eNo = eNo; this.eName = eName; this.eSal = eSal; } @Override public int hashCode() { System.out.println("-hashCode()- : " + this.hashCode()); final int prime = 31; int result = 1; result = prime * result + ((eName == null) ? 0 : eName.hashCode()); result = prime * result + eNo; result = prime * result + Float.floatToIntBits(eSal); return result; } @Override public boolean equals(Object obj) { System.out.println("-equals(obj)-: " + obj.toString()); if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Employee other = (Employee) obj; if (eName == null) { if (other.eName != null) return false; } else if (!eName.equals(other.eName)) return false; if (eNo != other.eNo) return false; if (Float.floatToIntBits(eSal) != Float.floatToIntBits(other.eSal)) return false; return true; } public int geteNo() { return eNo; } public String geteName() { return eName; } public float geteSal() { return eSal; } @Override public int compareTo(Employee emp) { int comp = 0; if ((comp = this.geteName().compareTo(emp.geteName())) != 0) { } else { if ((comp = Float.floatToIntBits(this.geteSal()) - Float.floatToIntBits(emp.geteSal())) != 0) { } else { comp = this.geteNo() - emp.geteNo(); } } return comp; } } //EmpComparable.java package com.comparable; /* * To be able to use the Comparable interface, the class of the objects * in the collection has to implement that interface. * You might have for example a List of objects from a class * that you don't control yourself - for example, * a class that comes from some third-party library. * You can't make that class implement Comparable easily * (it would mean you'd have to change the source code of * the third-party library, which is normally not a good idea). * To be able to sort the List anyway, you can implement your own Comparator * and pass that to for example * Collections.sort(list, comparator);. * Your Comparator object can be separate from the objects that are in the List. */ import java.util.ArrayList; import java.util.Collections; public class EmpComparable { /** * @param args */ public static void main(String[] args) { ArrayList empList = EmpComparable.getEmployees(); EmpComparable.EmpListIterate(empList); } public static ArrayList getEmployees() { ArrayList empList = new ArrayList(); empList.add(new Employee(1, "Sreerama", 50000.0f)); empList.add(new Employee(2, "Seetha", 45000.0f)); empList.add(new Employee(8, "Lakshmana", 40000.0f)); empList.add(new Employee(4, "Bharatha", 30000.0f)); empList.add(new Employee(5, "Sethrugna", 20000.0f)); empList.add(new Employee(7, "Hanuma", 10000.0f)); empList.add(new Employee(6, "Hanuma", 15000.0f)); empList.add(new Employee(3, "Lakshmana", 40000.0f)); /* * perform the sorting on given object; NOTE - no return type. * we cont perform reverse sorting by using comparable */ Collections.sort(empList); return empList; } public static void EmpListIterate(ArrayList empList) { System.out.println("EmpNo" + "\t\t" + "EmpName" + "\t\t\t" + "EmpSal"); System.out.println("-----" + "\t\t" + "-------" + "\t\t\t" + "------"); // no need of Iterator here, we just go for display employees. for (Employee emp : empList) { System.out.println(emp.geteNo() + "\t\t" + emp.geteName() + "\t\t" + emp.geteSal()); } } }
-------
---------------------------------
-Second, we do try to understand Detailed usage of Comparator.
//Employee.java package com.comparator; public class Employee implements Serializable { private int eNo; private String eName; private float eSal; public Employee() { super(); } public Employee(int eNo, String eName, float eSal) { super(); this.eNo = eNo; this.eName = eName; this.eSal = eSal; } @Override public int hashCode() { System.out.println("-hashCode()- : " + this.hashCode()); final int prime = 31; int result = 1; result = prime * result + ((eName == null) ? 0 : eName.hashCode()); result = prime * result + eNo; result = prime * result + Float.floatToIntBits(eSal); return result; } @Override public boolean equals(Object obj) { System.out.println("-equals(obj)-: " + obj.toString()); if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Employee other = (Employee) obj; if (eName == null) { if (other.eName != null) return false; } else if (!eName.equals(other.eName)) return false; if (eNo != other.eNo) return false; if (Float.floatToIntBits(eSal) != Float.floatToIntBits(other.eSal)) return false; return true; } public int geteNo() { return eNo; } public String geteName() { return eName; } public float geteSal() { return eSal; } } //EmpComparator.java package com.comparator; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import com.comparable.EmpComparable; import com.comparable.Employee; class EmpNameComparator implements Comparator { @Override public int compare(Employee emp1, Employee emp2) { return emp1.geteName().compareTo(emp2.geteName()); } } class EmpNoComparator implements Comparator { @Override public int compare(Employee emp1, Employee emp2) { return emp1.geteNo() < emp2.geteNo() ? -1 : (emp1.geteNo() < emp2 .geteNo()) ? 0 : 1; } } class EmpSalComparator implements Comparator { @Override public int compare(Employee emp1, Employee emp2) { return Float.floatToIntBits(emp1.geteSal()) < Float.floatToIntBits(emp2 .geteSal()) ? -1 : (Float.floatToIntBits(emp1.geteSal()) < Float .floatToIntBits(emp2.geteSal())) ? 0 : 1; } } public class EmpComparator { public EmpComparator() { // no code } public static Comparator CompareWith(String comp) { System.out.println("-comp- " + comp); Comparator compObj = null; if (comp.equals("EmpName")) { compObj = new EmpNameComparator(); } else if (comp.equals("EmpSal")) { compObj = new EmpSalComparator(); } else { compObj = new EmpNoComparator(); } return compObj; } public static ArrayList showEmployees() { ArrayList empList = new ArrayList(); empList.add(new Employee(1, "Sreerama", 50000.0f)); empList.add(new Employee(2, "Seetha", 45000.0f)); empList.add(new Employee(8, "Lakshmana", 40000.0f)); empList.add(new Employee(4, "Bharatha", 30000.0f)); empList.add(new Employee(5, "Sethrugna", 20000.0f)); empList.add(new Employee(7, "Hanuma", 10000.0f)); empList.add(new Employee(6, "Hanuma", 15000.0f)); empList.add(new Employee(3, "Lakshmana", 40000.0f)); // perform the sorting on given object; NOTE - no return type. Collections.sort(empList, EmpComparator.CompareWith("EmpName")); EmpComparable.EmpListIterate(empList); Collections.sort(empList, EmpComparator.CompareWith("EmpSal")); EmpComparable.EmpListIterate(empList); Collections.sort(empList, EmpComparator.CompareWith("default")); EmpComparable.EmpListIterate(empList); //Reverse sort is also possible. Collections.sort(empList, Collections.reverseOrder(EmpComparator.CompareWith("default"))); EmpComparable.EmpListIterate(empList); return empList; } public static void EmpListIterate(ArrayList empList) { System.out.println("EmpNo" + "\t\t" + "EmpName" + "\t\t\t" + "EmpSal"); System.out.println("-----" + "\t\t" + "-------" + "\t\t\t" + "------"); // no need of Iterator here, we just go for display employees. for (Employee emp : empList) { System.out.println(emp.geteNo() + "\t\t" + emp.geteName() + "\t\t" + emp.geteSal()); } } public static void main(String[] args) { ArrayList empList = EmpComparator.showEmployees(); } }
output:
------
// In reverse order by EmpNo as default sort.
No comments:
Post a Comment