TreeMap firstEntry()是指用于检索与TreeMap中存在的最低键值元素映射的键值对的方法,如果Map中没有键值对,则仅返回null。方法名称“ firstEntry()”本身说明,它将返回键元素最少的条目及其值。
Syntax:public Map.Entry<K,V> 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<Integer, String> treemap = new TreeMap<Integer, String>();
// 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());
}
}
输出
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<Double, String> treemapPercentage = new TreeMap<Double, String>();
// 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()方法是非静态方法,因为类的对象可以访问该方法,否则将产生错误。
相关用法
- Java TreeMap.firstEntry()、firstKey()用法及代码示例
- Java TreeMap higherKey()用法及代码示例
- Java TreeMap lastEntry()用法及代码示例
- Java TreeMap lowerEntry()用法及代码示例
- Java TreeMap floorEntry()用法及代码示例
- Java TreeMap comparator()用法及代码示例
- Java TreeMap navigableKeySet()用法及代码示例
- Java TreeMap higherEntry()用法及代码示例
- Java NavigableMap firstEntry()用法及代码示例
- Java TreeMap lowerKey()用法及代码示例
- Java TreeMap ceilingKey()用法及代码示例
- Java TreeMap floorKey()用法及代码示例
- Java TreeMap get()用法及代码示例
- Java TreeMap put()用法及代码示例
- Java TreeMap lastKey()用法及代码示例
- Java TreeMap subMap()用法及代码示例
- Java TreeMap remove()用法及代码示例
- Java TreeMap putAll()用法及代码示例
- Java TreeMap values()用法及代码示例
- Java TreeMap containsKey()用法及代码示例
注:本文由纯净天空筛选整理自night_fury1大神的英文原创作品 TreeMap firstEntry() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。