JAVAutil。树地图。Java中的DescendangMap()和DescendangKeySet()

Java中descending()的变体。util。TreeMap,本文将讨论这两种方法。 1.下降键集() :它返回一个 逆序导航集视图 地图上的钥匙。

null
Syntax : 
public NavigableSet descendingKeySet()
Parameters:
NA
Return Value:
It returns a reverse order navigable set view of the keys in this map.
Exception:
NA

// Java code to demonstrate the working
// descendingKeySet()
import java.io.*;
import java.util.*;
public class descendingKeySet1 {
public static void main(String[] args)
{
// Declaring the tree map of Integer and String
TreeMap<Integer, String> treemap = new TreeMap<Integer, String>();
// assigning the values in the tree map
// using put()
treemap.put( 2 , "two" );
treemap.put( 0 , "zero" );
treemap.put( 3 , "three" );
treemap.put( 6 , "six" );
treemap.put( 9 , "nine" );
treemap.put( 7 , "seven" );
// putting values in navigable set
// use of descendingKeySet
NavigableSet set1 = treemap.descendingKeySet();
System.out.println( "Navigable set values are: " + set1);
}
}


输出:

Navigable set values are: [9, 7, 6, 3, 2, 0]

2.下降映射(): 它返回一个 映射的逆序视图 包含在地图上。

Syntax : 
public NavigableMap descendingMap()
Parameters:
NA
Return Value
It returns a reverse order view of the map.
Exception:
NA

// Java code to demonstrate the working
// of descendingMap()
import java.io.*;
import java.util.*;
public class descendingMap {
public static void main(String[] args)
{
// Declaring the tree map of Integer and String
TreeMap<Integer, String> treemap = new TreeMap<Integer, String>();
// assigning the values in the tree map
// using put()
treemap.put( 2 , "two" );
treemap.put( 0 , "zero" );
treemap.put( 3 , "three" );
treemap.put( 6 , "six" );
treemap.put( 9 , "nine" );
treemap.put( 7 , "seven" );
// putting values in navigable map
// use of descendingMap()
NavigableMap map1 = treemap.descendingMap();
System.out.println( "Navigable map values are: " + map1);
}
}


输出:

Navigable map values are: {9=nine, 7=seven, 6=six, 3=three, 2=two, 0=zero}

实际应用: 本文介绍了降序函数的许多可能应用。其中一些是 优先级调度,或设计排名系统 .后一个在下面的代码中演示。

// Java code to demonstrate the application
// of descendingMap() and descendingKetSet()
import java.io.*;
import java.util.*;
public class descendingAppli {
public static void main(String[] args)
{
// Declaring the tree map of Integer and String
// to store participants info of scores with name
TreeMap<Integer, String> participants = new TreeMap<Integer, String>();
// assigning score of participants
// using put()
participants.put( 30 , "Ashty" );
participants.put( 45 , "Shavi" );
participants.put( 16 , "Vaish" );
participants.put( 15 , "Kil" );
participants.put( 11 , "Manju" );
// putting ranks in NavigableMap
// use of descendingMap() to assign 1st to
// maximum values and so on
NavigableMap<Integer, String> Ranks = participants.descendingMap();
System.out.println( "The ranks according to scores are : " );
// Printing values rankwise
int count = 0 ;
for (NavigableMap.Entry<Integer, String> entry : Ranks.entrySet()) {
count++;
String str = Integer.toString(count);
System.out.println( "Rank " + str + ": " + entry.getValue());
}
}
}


输出:

The ranks according to scores are : 
Rank 1: Shavi
Rank 2: Ashty
Rank 3: Vaish
Rank 4: Kil
Rank 5: Manju

本文由 沙姆巴维·辛格 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享