TreeMap Comparator comparator() Method Example Program


Returns the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.

Program

package com.candidjava;

import java.util.TreeMap;
import java.util.Comparator;
import java.util.NavigableMap;

/**
 * @author: Hari Haran
 * @discription: The comparator() method is used to return the comparator used
 *               to order the keys in this map, or null if this map uses the
 *               natural ordering of its keys.
 */

public class TreeMapComparator {
	public static void main(String[] args) {
		NavigableMap<Integer, String> tm = new TreeMap<Integer, String>();

		tm.put(1, "Anand");
		tm.put(2, "Hari Haran");
		tm.put(8, "Kamal");
		tm.put(4, "Karthikeyan");
		tm.put(5, "Mohan");
		tm.put(6, "Vinoth");
		tm.put(7, "Hari Haran");

		Comparator comp = tm.comparator();

		System.out.println("Following natural ordering");
		System.out.println("Comparator value: " + comp);
	}
}

Output

Following natural ordering
Comparator value: null

Explanation

public Comparator<? super K> comparator()
Returns the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.
Specified by:
comparator in interface SortedMap<K,V>
Returns:
the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys


Related Post

Comments


©candidjava.com