Site icon JavaGoal

comparator() method in java

In this post, we will learn about the comparator() method of TreeSet class. If you haven’t read about Comparator in java then you must it the Java Comparator first. Here is the java comparator tutorial in which we have discussed the working of the Comparator interface with different types of examples.

Comparator comparator(): It returns the Comparator that is used to sort the elements of TreeSet. If, it will return null if default natural sorting order is used. It returns the object of comparator type. If TreeSet is not using any comparator for sorting, then it will return null. If TreeSet, is using any comparator for sorting, then it will return the comparator.

import java.util.Comparator;
import java.util.TreeSet;

class Book 
{
	int uniqueNumber;
	String name;
	String authorName;
	
	public int getUniqueNumber() {
		return uniqueNumber;
	}
	public void setUniqueNumber(int uniqueNumber) {
		this.uniqueNumber = uniqueNumber;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAuthorName() {
		return authorName;
	}
	public void setAuthorName(String authorName) {
		this.authorName = authorName;
	}
	public Book(int uniqueNumber, String name, String authorName) {
		this.uniqueNumber = uniqueNumber;
		this.name = name;
		this.authorName = authorName;
	}
	@Override
	public String toString()
	{
		 return "Name: "+this.name+" and Unique number: "+this.uniqueNumber+" and Author Name: "+ this.authorName;

	}
}


//Creating a comparator for Book's name
class BookNameComparator implements Comparator<Book>
{
	@Override
	public int compare(Book book1, Book book2) 
	{
		return book1.getName().compareTo(book2.getName());
	}
}

//Creating a comparator for Book's Unique number
class BookNumerComparator implements Comparator<Book>{

 @Override
 public int compare(Book book1, Book book2) 
 {
     if(book1.getUniqueNumber() > book2.getUniqueNumber())
     {
         return 1;
     } 
     else {
         return -1;
     }
 }
}
//Creating a comparator for Book's Author name
class BookAuthorNameComparator implements Comparator<Book>
{
	@Override
	public int compare(Book book1, Book book2) 
	{
		return book1.getAuthorName().compareTo(book2.getAuthorName());
	}
}

public class ExampleOfTreeSet 
{
	public static void main(String arg[])
	{
		// Creating a TreeSet and using the comparator.
		TreeSet<Book> booksSortedByName = new TreeSet<Book>(new BookNameComparator());
		booksSortedByName.add(new Book(101, "Computer Fundamental", "P.K. Shina"));
		booksSortedByName.add(new Book(104, "Digital Computer", "R.K."));
		booksSortedByName.add(new Book(103, "Java", "Preeti sain"));
		booksSortedByName.add(new Book(102, "LOC", "C.K.Verma"));
		booksSortedByName.add(new Book(105, "PC", "N.D."));
		
		for(Book book : booksSortedByName)
			System.out.println(book);
		
		// Let's find the Comparator name by use of Comparator method
		System.out.println("+++++Name of Comparator class:" + booksSortedByName.comparator().getClass());
		
		System.out.println("----------------------------------------------------------------------");
		
		// Creating a TreeSet and using the comparator.
		TreeSet<Book> booksSortedByUniqueNumber = new TreeSet<Book>(new BookNumerComparator());
		booksSortedByUniqueNumber.add(new Book(101, "Computer Fundamental", "P.K. Shina"));
		booksSortedByUniqueNumber.add(new Book(104, "Digital Computer", "R.K."));
		booksSortedByUniqueNumber.add(new Book(103, "Java", "Preeti sain"));
		booksSortedByUniqueNumber.add(new Book(102, "LOC", "C.K.Verma"));
		booksSortedByUniqueNumber.add(new Book(105, "PC", "N.D."));
			
		for(Book book : booksSortedByUniqueNumber)
			System.out.println(book);
			
		// Let's find the Comparator name by use of Comparator method
		System.out.println("+++++Name of Comparator class:" + booksSortedByUniqueNumber.comparator().getClass());
				
		System.out.println("----------------------------------------------------------------------");
		
		// Creating a TreeSet and using the comparator.
		TreeSet<Book> booksSortedByAuthorName = new TreeSet<Book>(new BookAuthorNameComparator());
		booksSortedByAuthorName.add(new Book(101, "Computer Fundamental", "P.K. Shina"));
		booksSortedByAuthorName.add(new Book(104, "Digital Computer", "R.K."));
		booksSortedByAuthorName.add(new Book(103, "Java", "Preeti sain"));
		booksSortedByAuthorName.add(new Book(102, "LOC", "C.K.Verma"));
		booksSortedByAuthorName.add(new Book(105, "PC", "N.D."));
					
		for(Book book : booksSortedByAuthorName)
			System.out.println(book);
				
		// Let's find the Comparator name by use of Comparator method
		System.out.println("+++++Name of Comparator class:" + booksSortedByAuthorName.comparator().getClass());		
	}
}

Output:Name: Computer Fundamental and Unique number: 101 and Author Name: P.K. Shina
Name: Digital Computer and Unique number: 104 and Author Name: R.K.
Name: Java and Unique number: 103 and Author Name: Preeti sain
Name: LOC and Unique number: 102 and Author Name: C.K.Verma
Name: PC and Unique number: 105 and Author Name: N.D.
+++++Name of Comparator class:class BookNameComparator

Name: Computer Fundamental and Unique number: 101 and Author Name: P.K. Shina
Name: LOC and Unique number: 102 and Author Name: C.K.Verma
Name: Java and Unique number: 103 and Author Name: Preeti sain
Name: Digital Computer and Unique number: 104 and Author Name: R.K.
Name: PC and Unique number: 105 and Author Name: N.D.
+++++Name of Comparator class:class BookNumerComparator

Name: LOC and Unique number: 102 and Author Name: C.K.Verma
Name: PC and Unique number: 105 and Author Name: N.D.
Name: Computer Fundamental and Unique number: 101 and Author Name: P.K. Shina
Name: Java and Unique number: 103 and Author Name: Preeti sain
Name: Digital Computer and Unique number: 104 and Author Name: R.K.
+++++Name of Comparator class:class BookAuthorNameComparator

In the above example, you can see we have three different Comparators. Another each comparator uses to maintain the order of elements. Here we are creating the TreeSet and passing the comparator. So, At the end of for loop, we are getting the class Comparator from TreeSet.

Exit mobile version