0% found this document useful (0 votes)
3 views

Comparable_Comparator

The document explains the Java Comparable and Comparator interfaces used for sorting user-defined class objects. Comparable allows single sorting based on one data member, while Comparator supports multiple sorting sequences. It includes examples demonstrating the implementation of both interfaces for sorting custom objects like Student and Employee based on different attributes.

Uploaded by

Bl Gocher
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Comparable_Comparator

The document explains the Java Comparable and Comparator interfaces used for sorting user-defined class objects. Comparable allows single sorting based on one data member, while Comparator supports multiple sorting sequences. It includes examples demonstrating the implementation of both interfaces for sorting custom objects like Student and Employee based on different attributes.

Uploaded by

Bl Gocher
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

=========================================

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()

EXAMPLE 1: [sorts the list elements Student the basis of age]


==========

package com.intellect;

import java.util.ArrayList;
import java.util.Collections;

class Student implements Comparable<Student>


{

int rollno;
public int getRollno() {
return rollno;
}

public void setRollno(int rollno) {


this.rollno = rollno;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public int getAge() {


return age;
}

public void setAge(int age) {


this.age = age;
}

String name;
int age;

public Student(int rollno, String name, int age) {


super();
this.rollno = rollno;
this.name = name;
this.age = 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

[Note:- on name use compareTo() : return -


this.getName().compareTo(o.getName());]
}

}
public class ComparableSorting {

public static void main(String[] args) {

ArrayList<Student> al = new ArrayList<Student>();


al.add(new Student(101,"Badri lal",29));
al.add(new Student(105,"Nagendra",27));
al.add(new Student(111,"Monu",30));

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;

class Employee implements Comparable<Employee>


{
String name;
public String getName() {
return name;
}

public void setName(String name) {


this.name = name;
}

public int getSalary() {


return Salary;
}

public void setSalary(int salary) {


Salary = salary;
}

int Salary;

public Employee(String name, int salary) {


super();
this.name = name;
Salary = salary;
}

@Override
public int compareTo(Employee o)
{
//Ascending
//return this.Salary-o.Salary;
//Descending
return -this.Salary-o.Salary;

//on name : return -this.getName().compareTo(o.getName());

}
public class ComparableSoring {

public static void main(String[] args) {


ArrayList<Employee> e = new ArrayList<Employee>();
e.add(new Employee("Badri",101));
e.add(new Employee("Nagendra",102));
e.add(new Employee("Dinesh",104));

Collections.sort(e);

for(Employee e1: e)
{
System.out.println(e1.name+" "+e1.Salary);
}

=========================================
COMPARATOR==================================================

Java Comparator interface :-


>is used to order the objects of user-defined class.
>found in java.util package
>contains 2 methods compare(Object obj1,Object obj2) and equals(Object element).
>provides multiple sorting sequence i.e. you can sort the elements on the basis of
any data member, for example rollno, name, age or anything else.

public int compare(Object obj1,Object obj2): compares the first object with second
object.

EXAMPLE 1: COMPARATOR SORTING ACCORDING TO DEPT , SALARY


==========================================================
package com.intellect;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

//Employee class with setter/getter/toStirng


class Employee
{
int id;

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 + "]";
}

//salary comparator class


class salaryComparator implements Comparator<Employee>
{

@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 {

public static void main(String[] args) {

ArrayList<Employee> obj = new ArrayList<Employee>();


fff obj.add(new Employee(101,"Badri","Developer",50000));
obj.add(new Employee(102,"Monu","HR",30000));
obj.add(new Employee(103,"Nagendra","Manager",33000));

//sorting by salary
Collections.sort(obj, new salaryComparator());
//sorting by dept
Collections.sort(obj, new DeptComparator());

for(Employee e1: obj)


{
System.out.println(e1.id+" "+e1.name+" "+e1.Dept+" "+e1.salary);
}
}

}
c

==========================DIFF : COMPARABLE AND


COMPARATOR=========================================
1=> Comparable provides single sorting sequence. In other words, we can sort the
collection on the basis of single element such as id or name or price etc.
whereas
Comparator provides multiple sorting sequence. In other words, we can sort the
collection on the basis of multiple elements such as id, name and price etc.

2=>Comparable interface is in java.lang package


whereas
Comparator interface is present in java.util package.

3=>Comparable provides compareTo() method to sort elements.


whereas
Comparator provides compare() method to sort elements.

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.

You might also like