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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。