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


Java TreeMap descendingMap()用法及代码示例


descendingMap()方法用于返回此映射中包含的映射的逆序视图。映射的倒序或降序是根据键的降序排列的。降序Map由该Map支持,因此对Map的更改会反映在降序Map中,反之亦然。

声明语法:

public NavigableMap<K,V> descendingMap()
  • K:它是此映射维护的键的类型。
  • V:它是映射值的类型。

参数:无法使用

返回值:此Map的逆序或降序视图。

范例1:



Java

// Java program to demonstrate descendingMap() method 
  
import java.util.*; 
  
public class Example1 { 
    public static void main(String[] args) 
    { 
  
        // Declaring the tree map of Integer and String 
        TreeMap<Integer, String> treemap 
            = new TreeMap<Integer, String>(); 
  
        // Add the mappings to the tree map using put() 
        treemap.put(2, "Two"); 
        treemap.put(16, "Sixteen"); 
        treemap.put(8, "Eight"); 
        treemap.put(6, "Six"); 
        treemap.put(10, "Ten"); 
  
        // store the descending order of mappings in dmap 
        NavigableMap dmap = treemap.descendingMap(); 
  
        System.out.println("Reverse navigable map values:"
                           + dmap); 
    } 
}
输出
Reverse navigable map values:{16=Sixteen, 10=Ten, 8=Eight, 6=Six, 2=Two}


范例2:

Java

// Java Program to demonstrate descendingMap() method 
  
import java.util.*; 
  
public class Example2 { 
    public static void main(String[] args) 
    { 
  
        // Declaring the tree map of Integer and String 
        TreeMap<Integer, String> treemap 
            = new TreeMap<Integer, String>(); 
  
        // Add the mappings to the tree map using put() 
        treemap.put(11, "Abhishek Rout"); 
        treemap.put(9, "Akash Salvi"); 
        treemap.put(2, "Hemant Koul"); 
        treemap.put(8, "Vaibhav Kamble"); 
        treemap.put(6, "Sagar Joshi"); 
        treemap.put(10, "Onkar Dherange"); 
        treemap.put(7, "Rajwardhan Shinde"); 
        treemap.put(1, "Rahul Gavhane"); 
        treemap.put(4, "Abhishek Gadge"); 
        treemap.put(3, "Pratik Kulkarni"); 
        treemap.put(5, "Raviraj Bugge"); 
  
        // store the descending order of mappings in dmap 
        NavigableMap dmap = treemap.descendingMap(); 
  
        // print the mappings 
        System.out.println( 
            "List of students in reverse order:" + dmap); 
    } 
}
输出
List of students in reverse order:{11=Abhishek Rout, 10=Onkar Dherange, 9=Akash Salvi, 8=Vaibhav Kamble, 7=Rajwardhan Shinde, 6=Sagar Joshi, 5=Raviraj Bugge, 4=Abhishek Gadge, 3=Pratik Kulkarni, 2=Hemant Koul, 1=Rahul Gavhane}

注意读者!现在不要停止学习。以student-friendly的价格掌握Java和Java集合基础知识课程中所有重要的Java和集合概念,并做好行业准备。




相关用法


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