在Java中检索TreeMap中的最后一个条目
使用lastEntry()TreeMap中的方法检索最后一个条目。
创建一个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.lastEntry()
以下是在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("Last Entry in Map is "+m.lastEntry());
}
}输出结果
1 India 2 US 3 Australia 4 Netherlands 5 Canada Last Entry in Map is 5=Canada
热门推荐
10 广西考试祝福语结婚简短
11 猪年祝福语简短小孩
12 元旦祝福语送长辈简短
13 恭喜二宝祝福语简短
14 祝福语暖心话简短
15 国庆中秋祝福语简短兄弟
16 朋友订婚的祝福语简短
17 送弟弟中秋祝福语简短
18 爱生日祝福语简短独特