Comparator interface in java

Comparator interface:

Comparator interface is defined in java.util package. It has two methods named compare(Object obj1,Object obj2) and equals(Object element).

Note:

In the case of the Comparator interface, we can sort the elements based on multiple properties. Suppose we have Student class elements with name, class, and rollNo as properties then by using the Comparator interface we can sort student objects based on one or more than one property.

Methods of Comparator interface:

compare(Object obj1, Object obj2): It is used to compare the two objects. It returns +ve integer if the first object is greater than the second object, 0 if the first object equals to the second object, and –ve integer if the first object is less than the second object.

Syntax:

public int compare(Object obj1,Object obj2)

Sorting example using Comparator interface:

Student.java

/**
 * This class represents a Student.
 * @author w3spoint
 */
public class Student {
	//data members
	private String name;
	private String rollNo;
	private int age;
 
	//no-argument constructor
	public Student(){
 
	}
 
	//argument constructor
	public Student(String name, String rollNo, int age){
		this.name = name;
		this.rollNo = rollNo;
		this.age = age;
	}
 
	//getter setters
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getRollNo() {
		return rollNo;
	}
	public void setRollNo(String rollNo) {
		this.rollNo = rollNo;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}	
}

StudentNameComparator.java

import java.util.Comparator;
 
/**
 * This class is used to sort student objects by name.
 * @author w3spoint
 */
public class StudentNameComparator implements Comparator{
 
	public int compare(Object obj1, Object obj2) {
		Student student1=(Student)obj1;  
		Student student2=(Student)obj2;  
 
		return (student1.getName()).
                                   compareTo(student2.getName());  
	}  
}

StudentAgeComparator.java

import java.util.Comparator;
 
/**
 * This class is used to sort student objects by age.
 * @author w3spoint
 */
public class StudentAgeComparator implements Comparator{
 
	public int compare(Object obj1, Object obj2) {
		Student student1=(Student)obj1;  
		Student student2=(Student)obj2;  
 
		if(student1.getAge()==student2.getAge())  
			return 0;  
		else if(student1.getAge()>student2.getAge())  
			return 1;  
		else  
			return -1;
	}  
}

Test.java

package com.w3spoint.business;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
 
/**
 * This class is used to show the sorting functionality using comparator.
 * @author w3spoint
 */
public class Test {
	public static void main(String args[]){
		ArrayList studentList = new ArrayList();
 
		studentList.add(new Student("Sandy", "MCA/07/06", 29));
		studentList.add(new Student("Roxy", "MCA/07/32", 28));
		studentList.add(new Student("Sunil", "MCA/07/15", 26));
		studentList.add(new Student("Munish", "MCA/07/04", 27));
 
		System.out.println("Sorting by student name:");  
		Collections.sort(studentList,new StudentNameComparator());  
 
		Iterator iterator1=studentList.iterator();  
		while(iterator1.hasNext()){  
			Student student=(Student)iterator1.next();  
			System.out.println("Name: " + student.getName()+", " +
					"RollNo: "+student.getRollNo()+", Age: "+student.getAge());  
		} 
 
		System.out.println("Sorting by student age:");  
		Collections.sort(studentList,new StudentAgeComparator());  
 
		Iterator iterator2=studentList.iterator();  
		while(iterator2.hasNext()){  
			Student student=(Student)iterator2.next();  
			System.out.println("Name: " + student.getName()+", " +
					"RollNo: "+student.getRollNo()+", Age: "+student.getAge());  
		} 	
 
	}
}

Output

Sorting by student name:
Name: Munish, RollNo: MCA/07/04, Age: 27
Name: Roxy, RollNo: MCA/07/32, Age: 28
Name: Sandy, RollNo: MCA/07/06, Age: 29
Name: Sunil, RollNo: MCA/07/15, Age: 26
Sorting by student age:
Name: Sunil, RollNo: MCA/07/15, Age: 26
Name: Munish, RollNo: MCA/07/04, Age: 27
Name: Roxy, RollNo: MCA/07/32, Age: 28
Name: Sandy, RollNo: MCA/07/06, Age: 29

Download this example.

Next Topic: Properties class in java with example.
Previous Topic: Comparable interface in java with example.

Content Protection by DMCA.com
Please Share