在 Java 中檢索 TreeMap 中的第一個條目
使用 TreeMap 中的 firstEntry() 方法檢索第一個條目。
建立一個 TreeMap 並新增一些元素
TreeMap<Integer, String> m = new TreeMap<Integer, String>(); m.put(1,"India"); m.put(2,"US"); m.put(3,"Australia"); m.put(4,"Netherlands"); m.put(5,"Canada");
現在,檢索第一個條目
m.firstEntry()
以下是檢索 TreeMap 中第一個條目的示例
示例
import java.util.*;
public class Demo {
public static void main(String args[]){
TreeMap<Integer, String> m = new TreeMap<Integer, String>();
m.put(1,"India");
m.put(2,"US");
m.put(3,"Australia");
m.put(4,"Netherlands");
m.put(5,"Canada");
for(Map.Entry e:m.entrySet()){
System.out.println(e.getKey()+" "+e.getValue());
}
System.out.println("First Entry in Map is "+m.firstEntry());
}
}輸出
1 India 2 US 3 Australia 4 Netherlands 5 Canada First Entry in Map is 1=India
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP