当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java SortedMap comparator()用法及代码示例


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

用法:

public Comparator comparator()

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


以下示例程序旨在说明comparator()方法:

范例1:对于自然排序。

// 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<Integer, String> 
                sotreemap = new TreeMap<Integer, String>(); 
  
            // Populating tree map 
            sotreemap.put(1, "one"); 
            sotreemap.put(2, "two"); 
            sotreemap.put(3, "three"); 
            sotreemap.put(4, "four"); 
            sotreemap.put(5, "five"); 
  
            // Pritnig the SortedTreeMap 
            System.out.println("SortedTreeMap:" + sotreemap); 
  
            // Getting used Comparator in the map 
            // using comparator() method 
            Comparator comp = sotreemap.comparator(); 
  
            // Pritnig 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 program to demonstrate 
// comparator() method 
// for reverse ordering 
  
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
        throws Exception<div class = 'outputDiv'> 
<b>Output:</b> 
<pre> 
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] 
</pre> 
</div> 
  
    { 
  
        try { 
  
            // Creating object of TreeMap 
            SortedMap<Integer, String> 
                sotreemap = new TreeMap<Integer, String>( 
                    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"); 
  
            // Pritnig the TreeMap 
            System.out.println("SortedTreeMap:" + sotreemap); 
  
            // Getting used Comparator in the map 
            // using comparator() method 
            Comparator comp = sotreemap.comparator(); 
  
            // Pritnig 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


相关用法


注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 SortedMap comparator() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。