Comparable_Comparator
Comparable_Comparator
COMPARABLE==================================================
Java Comparable interface:
=>used to order the objects of user-defined class.
=>java.lang package
=>contains only one method named compareTo(Object).
=>It provide single sorting sequence only i.e. you can sort the elements on based
on single data member only. For
example it may be rollno, name, age or anything else.
NOTE 1:-
=>If collection elements are of Set or Map, we can use TreeSet or TreeMap.
But We cannot sort the elements of List. Collections class provides methods
Sort(List list) for sorting the elements of List type elements.
NOTE 2:-
Comparable interface is mainly used to sort the arrays (or lists) of custom
objects.
Lists (and arrays) of objects that implement Comparable interface can be sorted
automatically by Collections.sort () and Arrays.sort()
package com.intellect;
import java.util.ArrayList;
import java.util.Collections;
int rollno;
public int getRollno() {
return rollno;
}
String name;
int age;
@Override
public String toString() {
return "Student [rollno=" + rollno + ", name=" + name + ", age=" + age
+ "]";
}
@Override
public int compareTo(Student o) {
//Acending
return this.age-o.age;
//Descending
//return -this.age-o.age
}
public class ComparableSorting {
Collections.sort(al);
for(Student st:al)
{
System.out.println(st.rollno+" "+st.name+" "+st.age);
}
op:
===
111 Monu 30
101 Badri lal 29
105 Nagendra 27
EXAMPLE 2:
==========
package com.intellect;
import java.util.ArrayList;
import java.util.Collections;
int Salary;
@Override
public int compareTo(Employee o)
{
//Ascending
//return this.Salary-o.Salary;
//Descending
return -this.Salary-o.Salary;
}
public class ComparableSoring {
Collections.sort(e);
for(Employee e1: e)
{
System.out.println(e1.name+" "+e1.Salary);
}
=========================================
COMPARATOR==================================================
public int compare(Object obj1,Object obj2): compares the first object with second
object.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
String name;
String Dept;
int salary;
public Employee(int id, String name, String dept, int salary) {
super();
this.id = id;
this.name = name;
Dept = dept;
this.salary = salary;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", Dept=" + Dept + ",
salary=" + salary + "]";
}
@Override
public int compare(Employee e1, Employee e2) {
return e1.salary-e2.salary;
}
}
//Dept comparator class
class DeptComparator implements Comparator<Employee>
{
@Override
public int compare(Employee e1, Employee e2) {
// TODO Auto-generated method stub
return e1.Dept.compareTo(e2.Dept);
}
}
public class ComparatorSorting {
//sorting by salary
Collections.sort(obj, new salaryComparator());
//sorting by dept
Collections.sort(obj, new DeptComparator());
}
c
4=>2) Comparable affects the original class i.e. actual class is modified.
whereas
Comparator doesn't affect the original class i.e. actual class is not modified.