📜  Java中的 TreeMap 比较器()方法和示例

📅  最后修改于: 2022-05-13 01:55:17.365000             🧑  作者: Mango

Java中的 TreeMap 比较器()方法和示例

Java.util.TreeMap类的comparator()方法用于返回用于对该映射中的键进行排序的比较器,如果该映射使用其键的自然排序,则返回null。

--> java.util Package
    --> TreeMap Class
        --> comparator() Method 

句法:

public Comparator comparator()

返回类型:此方法返回用于对该映射中的键进行排序的比较器,如果此映射使用其键的自然排序,则返回 null。

示例 1:对于自然排序

Java
// Java program to Illustrate comparator() Method
// for Natural Ordering (Descending Order)
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] argv) throws Exception
    {
 
        // Try block to check for exceptions
        try {
 
            // Creating an empty TreeMap by
            // creating object of NavigableMap
            NavigableMap treemap
                = new TreeMap();
 
            // Populating TreeMap
            // using put() method
            treemap.put(1, "one");
            treemap.put(2, "two");
            treemap.put(3, "three");
            treemap.put(4, "four");
            treemap.put(5, "five");
 
            // Printing the TreeMap
            System.out.println("TreeMap: " + treemap);
 
            // Getting used Comparator in the map
            // using comparator() method
            Comparator comp = treemap.comparator();
 
            // Printing the comparator value
            System.out.println("Comparator value: " + comp);
        }
 
        // Catch block to handle the exception
        catch (NullPointerException e) {
 
            // Display message if exception occurs
            System.out.println("Exception thrown : " + e);
        }
    }
}


Java
// Java program to demonstrate comparator() Method
// for Reverse Ordering
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] argv) throws Exception
    {
 
        // Try block to check for exceptions
        try {
 
            // Creating an empty TreeMap
            NavigableMap treemap
                = new TreeMap(
                    Collections.reverseOrder());
 
            // Populating TreeMap
            // using put() method
            treemap.put(1, "one");
            treemap.put(2, "two");
            treemap.put(3, "three");
            treemap.put(4, "four");
            treemap.put(5, "five");
 
            // Printing the TreeMap
            System.out.println("TreeMap: " + treemap);
 
            // Getting used Comparator in the map
            // using comparator() method
            Comparator comp = treemap.comparator();
 
            // Printing the comparator value
            System.out.println("Comparator value: " + comp);
        }
 
        // Catch block to handle the exceptions
        catch (NullPointerException e) {
 
            // Display message if exception occurs
            System.out.println("Exception thrown : " + e);
        }
    }
}


输出:
TreeMap: {1=one, 2=two, 3=three, 4=four, 5=five}
Comparator value: null

示例 2:对于反向排序

Java

// Java program to demonstrate comparator() Method
// for Reverse Ordering
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] argv) throws Exception
    {
 
        // Try block to check for exceptions
        try {
 
            // Creating an empty TreeMap
            NavigableMap treemap
                = new TreeMap(
                    Collections.reverseOrder());
 
            // Populating TreeMap
            // using put() method
            treemap.put(1, "one");
            treemap.put(2, "two");
            treemap.put(3, "three");
            treemap.put(4, "four");
            treemap.put(5, "five");
 
            // Printing the TreeMap
            System.out.println("TreeMap: " + treemap);
 
            // Getting used Comparator in the map
            // using comparator() method
            Comparator comp = treemap.comparator();
 
            // Printing the comparator value
            System.out.println("Comparator value: " + comp);
        }
 
        // Catch block to handle the exceptions
        catch (NullPointerException e) {
 
            // Display message if exception occurs
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
TreeMap: {5=five, 4=four, 3=three, 2=two, 1=one}
Comparator value: java.util.Collections$ReverseComparator@232204a1