Java中的 TreeMap firstEntry() 和 firstKey() 方法及示例
Java.util.TreeMap 中的 first() 有两种变体,本文将讨论这两种变体。
方法一: firstEntry()
它返回与此映射中的最小键关联的键值映射,如果映射为空,则返回 null。
句法:
public Map.Entry firstEntry()
返回类型:具有最少键的条目,如果映射为空,则返回 null。
例子:
Java
// Java Program to Illustrate Working of firstKey() Method
// of TreeMap class
// Importing required classes
import java.io.*;
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an empty TreeMap by
// declaring object of integer, strings pairs
TreeMap treemap
= new TreeMap();
// Populating values in the TreeMap
// using put() method
treemap.put(2, "two");
treemap.put(7, "seven");
treemap.put(3, "three");
treemap.put(1, "one");
treemap.put(6, "six");
treemap.put(9, "nine");
// Printing the lowest entry in TreeMap by
// using firstEntry() method
System.out.println("Lowest entry is: "
+ treemap.firstEntry());
}
}
Java
// Java Program to Demonstrate firstKey() Method
// of TreeMap class
// Importing required classes
import java.io.*;
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an empty TreeMap by
// declaring object of integer, strings pairs
TreeMap treemap
= new TreeMap();
// Populating values in the TreeMap
// using put() method
treemap.put(2, "two");
treemap.put(1, "one");
treemap.put(3, "three");
treemap.put(6, "six");
treemap.put(5, "five");
treemap.put(9, "nine");
// Printing the lowest entry in TreeMap by
// using firstKey() method
System.out.println("Lowest key is: "
+ treemap.firstKey());
}
}
Java
// Java Program to Demonstrate Application Usage
// of firstKey() and firstEntry() Methods
// of TreeMap class
// Importing required classes
import java.io.*;
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an empty TreeMap
// of Integer and String times of participants
// In seconds
TreeMap time
= new TreeMap();
// Populating the time taken to complete task
// using put() method
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");
// Printing person with least time
// using of firstEntry() method
System.out.println("Winner with lowest time is : "
+ time.firstEntry());
}
}
输出:
Lowest entry is: 1=one
方法二:firstKey()
它返回地图中当前的第一个(最低)键。
句法:
public K firstKey()
返回类型:当前地图中的第一个(最低)键。
抛出异常: NoSuchElementException 如果此地图为空,则抛出。
例子:
Java
// Java Program to Demonstrate firstKey() Method
// of TreeMap class
// Importing required classes
import java.io.*;
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an empty TreeMap by
// declaring object of integer, strings pairs
TreeMap treemap
= new TreeMap();
// Populating values in the TreeMap
// using put() method
treemap.put(2, "two");
treemap.put(1, "one");
treemap.put(3, "three");
treemap.put(6, "six");
treemap.put(5, "five");
treemap.put(9, "nine");
// Printing the lowest entry in TreeMap by
// using firstKey() method
System.out.println("Lowest key is: "
+ treemap.firstKey());
}
}
输出:
Lowest key is: 1
实现:这些函数可用于获取给定列表中排名最高的人,或者可用于分配获胜者,其中完成任务的时间最短的人获胜。后一种将在下面讨论。
示例:实际应用
Java
// Java Program to Demonstrate Application Usage
// of firstKey() and firstEntry() Methods
// of TreeMap class
// Importing required classes
import java.io.*;
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an empty TreeMap
// of Integer and String times of participants
// In seconds
TreeMap time
= new TreeMap();
// Populating the time taken to complete task
// using put() method
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");
// Printing person with least time
// using of firstEntry() method
System.out.println("Winner with lowest time is : "
+ time.firstEntry());
}
}
输出:
Winner with lowest time is : 1.3=Shambhavi