📜  Java中的 SortedMap 比较器() 方法及示例

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

Java中的 SortedMap 比较器() 方法及示例

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

public Comparator comparator()

返回值:此方法返回用于对该映射中的键进行排序的比较器,如果此映射使用其键的自然排序,则返回 null。
下面的程序说明了compare()方法:
示例 1:对于自然排序。

Java
// Java program to demonstrate
// comparator() method for natural ordering
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
 
        try {
 
            // Creating object of SortedTreeMap
            SortedMap
                sotreemap = new TreeMap();
 
            // Populating tree map
            sotreemap.put(1, "one");
            sotreemap.put(2, "two");
            sotreemap.put(3, "three");
            sotreemap.put(4, "four");
            sotreemap.put(5, "five");
 
            // Printing the SortedTreeMap
            System.out.println("SortedTreeMap: " + sotreemap);
 
            // Getting used Comparator in the map
            // using comparator() method
            Comparator comp = sotreemap.comparator();
 
            // Printing the comparator value
            System.out.println("Comparator value: "
                               + comp);
        }
 
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Java
// Java program to demonstrate
// comparator() method
// for reverse ordering
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
Output:
Initial Mappings are: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}
The set is: [10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You]
      {           try {               // Creating object of TreeMap             SortedMap                 sotreemap = new TreeMap(                     Collections.reverseOrder());               // Populating tree map             sotreemap.put(1, "one");             sotreemap.put(2, "two");             sotreemap.put(3, "three");             sotreemap.put(4, "four");             sotreemap.put(5, "five");               // Printing the TreeMap             System.out.println("SortedTreeMap: " + sotreemap);               // Getting used Comparator in the map             // using comparator() method             Comparator comp = sotreemap.comparator();               // Printing the comparator value             System.out.println("Comparator value: " + comp);         }           catch (NullPointerException e) {             System.out.println("Exception thrown : " + e);         }     } }


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

示例 2:对于反向排序。

Java

// Java program to demonstrate
// comparator() method
// for reverse ordering
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
Output:
Initial Mappings are: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}
The set is: [10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You]
      {           try {               // Creating object of TreeMap             SortedMap                 sotreemap = new TreeMap(                     Collections.reverseOrder());               // Populating tree map             sotreemap.put(1, "one");             sotreemap.put(2, "two");             sotreemap.put(3, "three");             sotreemap.put(4, "four");             sotreemap.put(5, "five");               // Printing the TreeMap             System.out.println("SortedTreeMap: " + sotreemap);               // Getting used Comparator in the map             // using comparator() method             Comparator comp = sotreemap.comparator();               // Printing the comparator value             System.out.println("Comparator value: " + comp);         }           catch (NullPointerException e) {             System.out.println("Exception thrown : " + e);         }     } }
输出:
SortedTreeMap: {5=five, 4=four, 3=three, 2=two, 1=one}
Comparator value: java.util.Collections$ReverseComparator@232204a1