• September 3, 2014
  • Core Java
  • Comments Off on Comparator interface in java

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: 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 comparable interface we can sort student objects based on one or more than one properties.

Methods of Comparator interface:

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

Syntax: public int compare(Object obj1,Object obj2).

Sorting example using Comparable interface:

Student.java

/**
 * This class represents a Student.
 * @author javawithease
 */
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 javawithease
 */
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 javawithease
 */
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;
	}  
}

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.

Related Topics:

Collection framework in java.
Collection interfaces in java.
Collection interface in java with example.
Set interface in java with example.
SortedSet interface in java with example.
List interface in java with example.
Map interface in java with example.
Daemon thread in java in java with example.
SortedMap interface in java with example.
Queue interface in java with example.
Deque interface in java with example.
Enumeration interface in java with example.
Collection classes in java with example.
HashSet in java with example.
LinkedHashSet in java with example.
TreeSet in java with example.
ArrayList in java with example.
LinkedList in java with example.
HashMap in java with example.
LinkedHashMap in java with example.
TreeMap in java with example.
PriorityQueue in java with example.
ArrayDeque in java with example.
Abstract classes in collection framework in java with example.
Sorting in java with example.
Comparable interface in java with example.
Comparator interface in java with example.
Properties class in java with example.
Hashtable in java with example.
ListIterator interface in java with example.

 


Comments are closed.