import java.util.Iterator;运行此代码给出:
导入 java.util.Map.Entry;
导入 java.util.Set;
导入 java.util.TreeMap;
import java.util.Map;
public class TreeMapFirstkey {
public static void main(String a[]){
// 创建 TreeMap
TreeMap<字符串、整数>tmap = 新树状图<字符串、整数>();
// 在 TreeMap
tmap.put(Key1”,8);
tmap.put(键2”,6);
tmap.put(键3”,11);
tmap.put(键4”,7);
// 获取所有输入
Set set = tmap.entrySet();
// 获取迭代器浏览列表
迭代器 itr = set.iterator();
// 显示列表中的所有项目
while(itr.hasNext()) {
Map.Entry mentry = (Map.Entry)itr.next();
System.out.print(键: ”+mentry.getKey() + - ”);
System.out.println(值:”+mentry.getValue());
}
条目<字符串,整数>ent = tmap.lastEntry();
System.out.println(最后一个键/值元素: ”);
System.out.println(ent.getKey()+ ==>+ent.getValue());
}
}
key: Key1 - 值: 8References:
key: key2 - 值: 6
key: key3 - 值: 11
key: Key4 - 值: 7
第一个键/值:
键4 ==>7
Please disable your ad blocker and refresh the window to use this website.