📜  Java中的 TreeMap firstEntry() 方法及示例

📅  最后修改于: 2022-05-13 01:55:46.955000             🧑  作者: Mango

Java中的 TreeMap firstEntry() 方法及示例

TreeMap firstEntry()是指用于检索与TreeMap 中存在的最低键值元素映射的键值对的方法,如果Map 中没有键值对,则直接返回null。方法名称“firstEntry()”解释了它自己将返回具有其值的键元素最少的条目。

Syntax: public Map.Entry firstEntry(), 

Here K and V refer key-value respectively.

Parameters: NA - As it doesnot accept any parameter.

Return Value: It returns an entry with the least key (lowest key-value pair) and null if the 
TreeMap is empty.

Exception: NA - As it doesnot throw any exception at entry return.

示例 1:

Java
// Java code to demonstrate 
// the working of TreeMap firstKey() 
import java.io.*; 
import java.util.*; 
public class treeMapFirstKey { 
public static void main(String[] args) 
    { 
    
        // Declaring the TreeMap of Key - Value Pairs
        // Integer - Key , String - Value
        TreeMap treemap = new TreeMap(); 
    
        // Adding-up the values in the TreeMap 
        // Use put() function to append data 
        treemap.put(2, "Java"); 
        treemap.put(4, "CPP"); 
        treemap.put(5, "PHP"); 
        treemap.put(1, "Python"); 
        treemap.put(3, "C"); 
    
        // Check for firstEntry()
        System.out.println("Lowest Entry is: " + treemap.firstEntry()); 
    } 
}


Java
// Java code to demonstrate the working
// of TreeMap firstKey() method
import java.io.*; 
import java.util.*; 
public class percentageFirstKey { 
public static void main(String[] args) 
    { 
    
        // Declaring the TreeMap of Key - Value Pairs
        // Double - Key , String - Value
        TreeMap treemapPercentage = new TreeMap(); 
    
        // Adding-up the values in the TreeMap 
        // Use put() function to append data 
        treemapPercentage.put(81.40, "Shashank"); 
        treemapPercentage.put(72.80, "Anand");
        treemapPercentage.put(65.50, "Gulshan");
        treemapPercentage.put(71.10, "Abhishek"); 
        treemapPercentage.put(70.20, "Ram"); 
    
        // Check for firstEntry()
        System.out.println("Lowest Entry is: " + treemapPercentage.firstEntry()); 
    } 
}


输出
Lowest Entry is: 1=Python

通过上面的示例,很明显 firstEntry() 检查比较 TreeMap 的每个键,并返回具有最少键值对。

示例 2:

Java

// Java code to demonstrate the working
// of TreeMap firstKey() method
import java.io.*; 
import java.util.*; 
public class percentageFirstKey { 
public static void main(String[] args) 
    { 
    
        // Declaring the TreeMap of Key - Value Pairs
        // Double - Key , String - Value
        TreeMap treemapPercentage = new TreeMap(); 
    
        // Adding-up the values in the TreeMap 
        // Use put() function to append data 
        treemapPercentage.put(81.40, "Shashank"); 
        treemapPercentage.put(72.80, "Anand");
        treemapPercentage.put(65.50, "Gulshan");
        treemapPercentage.put(71.10, "Abhishek"); 
        treemapPercentage.put(70.20, "Ram"); 
    
        // Check for firstEntry()
        System.out.println("Lowest Entry is: " + treemapPercentage.firstEntry()); 
    } 
}
输出
Lowest Entry is: 65.5=Gulshan

TreeMap firstEntry() 上的一些要点:

  • TreeMap firstEntry() 在Java.util 包中可用。
  • 它不会在返回入口时抛出异常。
  • TreeMap firstEntry() 方法是一个非静态方法,因为它可以被类的对象访问,除此之外,会出错。