Java.util.TreeMap中有first()的兩個變體,本文都將進行討論。 1. firstEntry():它返回與此映射中最小鍵關聯的鍵-值映射;如果映射為空,則返回null。
用法: public Map.Entry firstEntry() 參數: NA 返回值: It returns an entry with the least key and null if the map is empty. Exception: NA
// Java code to demonstrate the working
// of firstKey()
import java.io.*;
import java.util.*;
public class firstKey {
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(7, "seven");
treemap.put(3, "three");
treemap.put(1, "one");
treemap.put(6, "six");
treemap.put(9, "nine");
// use of firstEntry()
System.out.println("Lowest entry is:" + treemap.firstEntry());
}
}
輸出:
Lowest entry is:1=one
2. firstKey():它返回Map中當前的第一個(最低)鍵。
用法: public K firstKey() 參數: NA 返回值: It returns the first (lowest) key currently in this map. Exception: NA NoSuchElementException: It is thrown if this map is empty.
// Java code to demonstrate the working
// of firstKey()
import java.io.*;
import java.util.*;
public class firstKey {
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(1, "one");
treemap.put(3, "three");
treemap.put(6, "six");
treemap.put(5, "five");
treemap.put(9, "nine");
// Use of firstKey()
System.out.println("Lowest key is:" + treemap.firstKey());
}
}
輸出:
Lowest key is:1
實際應用:這些函數可用於獲取給定列表中排名最高的人,或者可用於在遊戲中以最短的時間完成任務的人獲勝的情況下分配贏家。下麵討論一個。
// Java code to demonstrate the application
// of firstKey() and firstEntry()
import java.io.*;
import java.util.*;
public class FirstAppli {
public static void main(String[] args)
{
// Declaring the tree map of Integer and String
// times of participants (in seconds)
TreeMap<Float, String> time = new TreeMap<Float, String>();
// assigning the time taken to complete task
// using put()
time.put(2.32f, "Astha");
time.put(7.43f, "Manjeet");
time.put(1.3f, "Shambhavi");
time.put(5.63f, "Nikhil");
time.put(6.26f, "Vaishnavi");
// use of firstEntry()
// printing person with least time
System.out.println("Winner with lowest time is:" + time.firstEntry());
}
}
輸出:
Winner with lowest time is:1.3=Shambhavi
相關用法
- Java SortedMap firstKey()用法及代碼示例
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Java lang.Long.reverse()用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java.util.Collections.rotate()用法及代碼示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代碼示例
- Java Java.util.function.BiPredicate用法及代碼示例
- Java Java.util.Collections.disjoint()用法及代碼示例
- Java Java.util.function.IntPredicate用法及代碼示例
- Java Java.util.function.DoublePredicate用法及代碼示例
- Java Java.util.function.LongPredicate用法及代碼示例
注:本文由純淨天空篩選整理自 Java.util.TreeMap.firstEntry() and firstKey() in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。