Java.util.TreeMap中有descending()的兩個變體,本文都將進行討論。 1. descendingKeySet():它返回Map中包含的鍵的逆序可導航Set視圖。
用法: public NavigableSet descendingKeySet() 參數: NA 返回值: 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. descendingMap():它返回映射中包含的映射的逆序視圖。
用法: public NavigableMap descendingMap() 參數: 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
相關用法
- Java Java.util.concurrent.RecursiveTask用法及代碼示例
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
- Java Java.util.concurrent.Phaser用法及代碼示例
- Java Java.util.Collections.disjoint()用法及代碼示例
- Java Java.util.Collections.rotate()用法及代碼示例
- Java Java lang.Long.reverse()用法及代碼示例
- Java Java.util.function.LongPredicate用法及代碼示例
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Java.util.function.IntPredicate用法及代碼示例
- Java Java.util.concurrent.RecursiveAction用法及代碼示例
注:本文由純淨天空篩選整理自 Java.util.TreeMap.descendingMap() and descendingKeyset() in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。