Menu
Topics Index
...
`


Collections Framework >
Siva Nookala - 20 Feb 2017
Both Java TreeSet - TreeSet Examples in Java and TreeMap In Java - java.util.TreeMap store elements in sorted order. However, it is the Comparator that defines precisely what sorted order means.

The Comparator interface defines two methods:
MethodDescription
int compare(Object obj1, Object obj2)Returns zero if the objects obj1 and obj2 are equal.
Returns a positive value if obj1 is greater than obj2 Otherwise a negative value is returned.
boolean equals(Object obj)Returns true if obj and the invoking object are both Comparator objects and use the same ordering. Otherwise, it returns false.
Collections.sort(List<T> list) is a method that sorts the specified list into ascending order, according to the natural ordering of its elements. It clearly explains in Java Collection Algorithms topic.
Comparators
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class ComparatorTest
{
    public static void main(String arg[])
    {
        List<Student> list = new ArrayList<Student>(); // List of Student Type
        list.add(new Student("Nishan", 538, 'A')); // Adding Elements to list
        list.add(new Student("Ram", 513, 'A'));
        list.add(new Student("Sanath", 583, 'B'));
        list.add(new Student("Amar", 504, 'A'));
        list.add(new Student("Shiva", 506, 'A'));
        list.add(new Student("Bobby", 589, 'B'));
        Collections.sort(list, new Comparator<Student>() { // Comparator in Collections sort
        
                    public int compare(Student a1, Student a2) { // Compare method
                        return a1.name.compareTo(a2.name); // LINE A
                    }
                });
        for (int i = 0; i < list.size(); i++) {
            Student a = list.get(i); // Assigning list element to Student object
            System.out.println(a.name + " " + a.rollno + " " + a.section);
        }
    
    }
}

class Student {

    String name;
    int rollno;
    char section;

    public Student(String name, int rollno, char section) {
        this.name = name;
        this.rollno = rollno;
        this.section = section;
    }
}
OUTPUT

Amar 504 A
Bobby 589 B
Nishan 538 A
Ram 513 A
Sanath 583 B
Shiva 506 A

DESCRIPTION

Here, a Student class with member variables name, rollno and section is defined. A constructor is also defined for the Student class. A Comparator is created which compares the student names. When Collection.sort is called on list with this Comparator, the list is sorted in ascending order of names. The sorted list is printed at the end.
Please note that the definition of the class implementing Comparator is anonymous i.e. class has no name.

THINGS TO TRY
At LINE A,
  • In order to get the list elements in descending order by name interchange the a1 and a2 objects.
  • In order to get the list elements in ascending order by rollno replace code in return statement as a1.rollno-a2.rollno for descending order, interchange a1 and a2 objects.
  • In order to get the list elements in ascending order by section replace code in return statement as a1.section-a2.section. For descending order, interchange a1 and a2 objects.

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.