获取存储在 Java TreeMap 中的最低键
使用 firstKey() 方法来获取存储在 TreeMap 中的最低键。
我们先设置 TreeMap,并向其中添加一些元素 −
TreeMap<Integer,String> m = new TreeMap<Integer,String>(); m.put(1,"PHP"); m.put(2,"jQuery"); m.put(3,"JavaScript"); m.put(4,"Ruby"); m.put(5,"Java"); m.put(6,"AngularJS"); m.put(7,"ExpressJS");
现在获取最低键 −
m.firstKey()
以下是获取 TreeMap 中最低键的示例 −
示例
import java.util.*;
public class Demo {
public static void main(String args[]) {
TreeMap<Integer,String> m = new TreeMap<Integer,String>();
m.put(1,"PHP");
m.put(2,"jQuery");
m.put(3,"JavaScript");
m.put(4,"Ruby");
m.put(5,"Java");
m.put(6,"AngularJS");
m.put(7,"ExpressJS");
System.out.println("TreeMap Elements...
"+m);
System.out.println("Lowest key in TreeMap: " + m.firstKey());
}
}输出
TreeMap Elements...
{1=PHP, 2=jQuery, 3=JavaScript, 4=Ruby, 5=Java, 6=AngularJS, 7=ExpressJS}
Lowest key in TreeMap: 1
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP