Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Monday, 19 June 2017

Java Comparator

 

Comparator Interface

Java Comparator

Comparator is a interface in java which is available in java.util package.

Comparator is used to sort or order the objects of user-defined class.

As we know comparable interface contains only one method compareTo(Object) but comparator interface contains 2 methods which is compare(Object obj1, Object obj2) and equals(Object element).

Comparator interface provides multiple sorting sequence i.e you can sort the elements on the basis of any data member such as name, age, rollno, etc.

A comparison function, which imposes a total ordering on some collection of objects. Comparator can be passed to a sort method(such as Collections.sort or Arrays.sort) to allow precise control over the sort order. Comparator can also be used to control the order of certain data structure(such as sorted sets and sorted maps) or to provide an ordering for collections of objects that don't have a natural ordering.


Methods of Comparator Interface

  1. public int compare(Object obj1, Object obj2) : This method compares the first object with second object.
  2. boolean equals(Object obj) : This method indicates whether some other objects is "equal to " this comparator.

Collections Class


Collections is a class in java, It is not a collection(interface), it is a collections(class) which provides us static method so that we can easily sort the elements of collections. If the elements of collection is a type of Set or Map then we will sort these elements easily by using treeSet and treeMap class but we cannot sort the elements of List if we want to sort the elements of List type , We have to use method of Collections's class so that we can easily sort the elements of List type.


Collections class extends only super class i.e Object class.


The methods of this class all throw a NullPointerException, if the collections or class objects provide them are null.

Method of Collections Class

public void sort(List list, Comparator c) : This method is used to sort the elements of List by the given Comparator.


Java Comparator Example

This is a comparator example, where we will sort the elements of List type on the basis of age and name. Here we will create 4 .java files.

File 1 : Student.java
class Student
{
int rollno;
String name;
int age;
Student(int rollno, String name, int age)
{
this.rollno = rollno;
this.name = name;
this.age = age;
}
}



File 2 : AgeComparator.java

In this java file, we will define the comparison logic based on age. If age of first object is greater than the second it will return positive value and if age of first object is less than the second object it will return negative value, if both will be equal, return 0.

import java.util.*;
class AgeComparator implements Comparator<Student>
{
public int compare(Student s1, Student s2)
{
if(s1.age == s2.age)
return 0;
else if(s1.age>s2.age)
return 1;
else
return -1;
}
}

File 3 : NameComparator.java

In this java file, we will define comparison logic based on name and we will use compareTo() method.

import java.util.*;
class NameComparator implements Comparator<Student>
{
public int compare(Student s1, Student s2)
{
return s1.name.compareTo(s2.name);
}
}

File 4 : Test.java

In this java file, we will sort the List on the basis of age and name.

import java.util.*;
class Test
{
public static void main(String args[])
{
ArrayList<Student> al = new ArrayList<Student>();
al.add(new Student(100, "salman", 19);
al.add(new Student(105, "ranvir", 9);
al.add(new Student(102, "varun", 29);
al.add(new Student(107, "arjun", 18);

System.out.println("sorting by name");
Collections.sort(al, new NameComparator());
for(Student st : al)
{
System.out.println(st.rollno+" " +st.name+" " +st.age);
}

System.out.println("sorting by age");
Collections.sort(al, new AgeComparator());
for(Student st : al)
{
System.out.println(st.rollno+" " +st.name+" " +st.age);
}
}
}

output : sorting by name
              107 arjun 18
              105 ranvir 9
              100 salman 19
              102 varun 29

              sorting by age
              105 ranvir 9
              107 arjun 18
              100 salman 19
              102 varun 29




Share:

1 comment:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate