Home  >  Java 8

Java Comparator with SortedSet

By Arvind Rai, March 23, 2019
Java Comparator can be used to control the order of SortedSet data structures. The implementing classes of SortedSet are TreeSet and ConcurrentSkipListSet. We can pass Comparator instance to the constructor of TreeSet and ConcurrentSkipListSet classes to control its order. SortedSet provides comparator() method that returns the comparator used to order the elements in this set. If SortedSet uses natural ordering of its elements, then comparator() method returns null. Here on this page we will provide examples to use comparator with TreeSet and ConcurrentSkipListSet classes.

Comparator with TreeSet

TreeSet orders the elements according to their natural ordering, or by a comparator provided at set creation time, depending on which constructor is used. We can instantiate TreeSet class by passing Comparator using following constructor.
TreeSet(Comparator<? super E> comparator) 
It constructs a new, empty tree set, sorted according to the specified comparator. When we don’t pass comparator, TreeSet sorts the elements according to its natural ordering. For natural ordering, a class needs to implement Comparable interface and override compareTo method.
To get the comparator used by our TreeSet object, SortedSet provides comparator() method.
Now find the example using comparator with TreeSet class to control the order of its elements.
TreeSetDemo.java
package com.concretepage;
import java.util.Arrays;
import java.util.Comparator;
import java.util.TreeSet;
public class TreeSetDemo {
  public static void main(String[] args) {
	Student s1 = new Student("Shyam", 18);
	Student s2 = new Student("Mohan", 20);
	Student s3 = new Student("Ram", 22);
	
	System.out.println("---TreeSet Order With Comparator---");
	
	Comparator<Student> ageComparator = Comparator.comparing(Student::getAge);
	TreeSet<Student> myTreeSet = new TreeSet<>(ageComparator);
	myTreeSet.addAll(Arrays.asList(s1, s2, s3));
	myTreeSet.forEach(s -> System.out.println(s));	
	//System.out.println("Comparator: "+ myTreeSet.comparator());
	
	System.out.println("---TreeSet Natural Order (With Comparable)---");
	
	myTreeSet = new TreeSet<>();
	myTreeSet.addAll(Arrays.asList(s1, s2, s3));
	myTreeSet.forEach(s -> System.out.println(s));
  }
} 
Student.java
package com.concretepage;
public class Student implements Comparable<Student> {
  private String name;
  private int age;
  public Student(String name, int age) {
	this.name = name;
	this.age = age;
  }
  public String getName() {
	return name;
  }
  public int getAge() {
	return age;
  }
  @Override
  public int compareTo(Student s) {
	return name.compareTo(s.getName());
  }
  @Override  
  public String toString(){
	return name + "-" + age; 
  }
} 
Output
---TreeSet Order With Comparator---
Shyam-18
Mohan-20
Ram-22
---TreeSet Natural Order (With Comparable)---
Mohan-20
Ram-22
Shyam-18 

Comparator with ConcurrentSkipListSet

ConcurrentSkipListSet orders the elements according to their natural ordering, or by a comparator provided at set creation time, depending on which constructor is used. We can instantiate ConcurrentSkipListSet class by passing Comparator using following constructor.
ConcurrentSkipListSet(Comparator<? super E> comparator) 
It constructs a new, empty set that orders its elements according to the specified comparator. When we don’t pass comparator, ConcurrentSkipListSet sorts the elements according to its natural ordering. For natural ordering, a class needs to implement Comparable interface and override compareTo method.
To get the comparator used by our ConcurrentSkipListSet object, SortedSet provides comparator() method.
Now find the example using comparator with ConcurrentSkipListSet class to control the order of its elements.
ConcurrentSkipListSetDemo.java
package com.concretepage;
import java.util.Arrays;
import java.util.Comparator;
import java.util.concurrent.ConcurrentSkipListSet;
public class ConcurrentSkipListSetDemo {
  public static void main(String[] args) {
	Student s1 = new Student("Shyam", 18);
	Student s2 = new Student("Mohan", 20);
	Student s3 = new Student("Ram", 22);
	
	System.out.println("---ConcurrentSkipListSet Order With Comparator---");
	
	Comparator<Student> ageComparator = Comparator.comparing(Student::getAge);
	ConcurrentSkipListSet<Student> myConcurrentSkipList = new ConcurrentSkipListSet<>(ageComparator);
	myConcurrentSkipList.addAll(Arrays.asList(s1, s2, s3));
	myConcurrentSkipList.forEach(s -> System.out.println(s));	
	//System.out.println("Comparator: "+ myConcurrentSkipList.comparator());
	
	System.out.println("---ConcurrentSkipListSet Natural Order (With Comparable)---");
	
	myConcurrentSkipList = new ConcurrentSkipListSet<>();
	myConcurrentSkipList.addAll(Arrays.asList(s1, s2, s3));
	myConcurrentSkipList.forEach(s -> System.out.println(s));
  }
}
Output
---ConcurrentSkipListSet Order With Comparator---
Shyam-18
Mohan-20
Ram-22
---ConcurrentSkipListSet Natural Order (With Comparable)---
Mohan-20
Ram-22
Shyam-18 

References

Interface Comparator
Interface SortedSet
Class TreeSet
Class ConcurrentSkipListSet
Java Comparator.comparing
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us