用Java检索TreeMap的大小
使用size()方法获取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");
计算上述TreeMap中的元素数,或使用size()如下所示的方法获取大小-
m.size()
要获取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...\n"+m);
System.out.println("\nSize of TreeMap: " + m.size());
}
}输出结果
TreeMap Elements...
{1=PHP, 2=jQuery, 3=JavaScript, 4=Ruby, 5=Java, 6=AngularJS, 7=ExpressJS}
Size of TreeMap: 7热门推荐
10 简短中秋祝福语朋友结婚
11 年底拜年祝福语大全简短
12 表达家人的祝福语简短
13 庆祝国家的祝福语简短
14 年底老板祝福语简短的
15 毕业英语祝福语大全简短
16 送给情侣贺卡祝福语简短
17 孩子满月随礼简短祝福语
18 新年给大哥祝福语简短