Home  >  Java 8

Java Comparator with SortedMap

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

Comparator with TreeMap

TreeMap are sorted according to the natural ordering of its keys, or by a comparator provided at map creation time, depending on which constructor is used. We can instantiate TreeMap class by passing Comparator using following constructor.
TreeMap(Comparator<? super K> comparator) 
It constructs a new, empty tree map, ordered according to the given comparator. When we don’t pass comparator, TreeMap sorts keys to its natural ordering. For natural ordering, an element class needs to implement Comparable interface and override compareTo method.
To get the comparator used by our TreeMap object, SortedMap provides comparator() method.
Now find the example using comparator with TreeMap class to control the order of its keys.
TreeMapDemo.java
package com.concretepage;
import java.util.Comparator;
import java.util.TreeMap;
public class TreeMapDemo {
  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("---TreeMap Order With Comparator---");
	
	Comparator<Student> ageComparator = Comparator.comparing(Student::getAge);
	TreeMap<Student, String> myTreeMap = new TreeMap<>(ageComparator);
	myTreeMap.put(s1, "Varanasi");
	myTreeMap.put(s2, "Mathura");
	myTreeMap.put(s3, "Kashi");	
	myTreeMap.forEach((k, v) -> System.out.println(k + " - " + v));	
	//System.out.println("Comparator: "+ myTreeMap.comparator());
	
	System.out.println("---TreeMap Natural Order (With Comparable)---");
	
	myTreeMap = new TreeMap<>();
	myTreeMap.put(s1, "Varanasi");
	myTreeMap.put(s2, "Mathura");
	myTreeMap.put(s3, "Kashi");	
	myTreeMap.forEach((k, v) -> System.out.println(k + " - " + v));	
  }
} 
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
---TreeMap Order With Comparator---
Shyam-18 - Varanasi
Mohan-20 - Mathura
Ram-22 - Kashi
---TreeMap Natural Order (With Comparable)---
Mohan-20 - Mathura
Ram-22 - Kashi
Shyam-18 - Varanasi 

Comparator with ConcurrentSkipListMap

ConcurrentSkipListMap are sorted according to the natural ordering of its keys, or by a comparator provided at map creation time, depending on which constructor is used. We can instantiate ConcurrentSkipListMap class by passing Comparator using following constructor.
ConcurrentSkipListMap(Comparator<? super K> comparator) 
It constructs a new, empty map, ordered according to the given comparator. When we don’t pass comparator, ConcurrentSkipListMap sorts keys to its natural ordering. For natural ordering, an element class needs to implement Comparable interface and override compareTo method.
To get the comparator used by our ConcurrentSkipListMap object, SortedMap provides comparator() method.
Now find the example using comparator with ConcurrentSkipListMap class to control the order of its keys.
ConcurrentSkipListMapDemo.java
package com.concretepage;
import java.util.Comparator;
import java.util.concurrent.ConcurrentSkipListMap;
public class ConcurrentSkipListMapDemo {
  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("---ConcurrentSkipListMap Order With Comparator---");
	
	Comparator<Student> ageComparator = Comparator.comparing(Student::getAge);
	ConcurrentSkipListMap<Student, String> myConcurrentSkipListMap = new ConcurrentSkipListMap<>(ageComparator);
	myConcurrentSkipListMap.put(s1, "Varanasi");
	myConcurrentSkipListMap.put(s2, "Mathura");
	myConcurrentSkipListMap.put(s3, "Kashi");	
	myConcurrentSkipListMap.forEach((k, v) -> System.out.println(k + " - " + v));	
	//System.out.println("Comparator: "+ myConcurrentSkipListMap.comparator());
	
	System.out.println("---ConcurrentSkipListMap Natural Order (With Comparable)---");
	
	myConcurrentSkipListMap = new ConcurrentSkipListMap<>();
	myConcurrentSkipListMap.put(s1, "Varanasi");
	myConcurrentSkipListMap.put(s2, "Mathura");
	myConcurrentSkipListMap.put(s3, "Kashi");	
	myConcurrentSkipListMap.forEach((k, v) -> System.out.println(k + " - " + v));	
  }
} 
Output
---ConcurrentSkipListMap Order With Comparator---
Shyam-18 - Varanasi
Mohan-20 - Mathura
Ram-22 - Kashi
---ConcurrentSkipListMap Natural Order (With Comparable)---
Mohan-20 - Mathura
Ram-22 - Kashi
Shyam-18 - Varanasi 

References

Interface Comparator
Interface SortedMap
Class TreeMap
Class ConcurrentSkipListMap
Java Comparator.comparing
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us