- Java.util 包类
- Java.util - 首页
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util 包其他内容
- Java.util - 接口
- Java.util - 异常
- Java.util - 枚举
- Java.util 有用资源
- Java.util - 有用资源
- Java.util - 讨论
Java TreeMap size() 方法
描述
Java TreeMap size() 方法用于返回此映射中的键值映射数量。
声明
以下是 java.util.TreeMap.size() 方法的声明。
public int size()
参数
无
返回值
方法调用返回此映射中的键值映射的数量。
异常
无
获取 Integer,Integer 对的 TreeMap 的大小示例
以下示例演示了如何使用 Java TreeMap size() 方法获取映射中存在的键值映射总数。我们创建了一个 Integer,Integer 对的 TreeMap 对象。然后添加了一些条目,并使用 size() 打印键值映射的数量。
package com.tutorialspoint;
import java.util.TreeMap;
public class TreeMapDemo {
public static void main(String[] args) {
// creating tree map
TreeMap<Integer, Integer> treemap = new TreeMap<>();
// populating tree map
treemap.put(2, 2);
treemap.put(1, 1);
treemap.put(3, 3);
treemap.put(6, 6);
treemap.put(5, 5);
// getting size of the map
System.out.println("Size of the map: "+treemap.size());
}
}
输出
让我们编译并运行上述程序,这将产生以下结果。
Size of the map: 5
获取 Integer,String 对的 TreeMap 的大小示例
以下示例演示了如何使用 Java TreeMap size() 方法获取映射中存在的键值映射总数。我们创建了一个 Integer,String 对的 TreeMap 对象。然后添加了一些条目,并使用 size() 打印键值映射的数量。
package com.tutorialspoint;
import java.util.TreeMap;
public class TreeMapDemo {
public static void main(String[] args) {
// creating tree map
TreeMap<Integer, String> treemap = new TreeMap<>();
// populating tree map
treemap.put(2, "two");
treemap.put(1, "one");
treemap.put(3, "three");
treemap.put(6, "six");
treemap.put(5, "five");
// getting size of the map
System.out.println("Size of the map: "+treemap.size());
}
}
输出
让我们编译并运行上述程序,这将产生以下结果。
Size of the map: 5
获取 Integer,Object 对的 TreeMap 的大小示例
以下示例演示了如何使用 Java TreeMap size() 方法获取映射中存在的键值映射总数。我们创建了一个 Integer,Student 对的 TreeMap 对象。然后添加了一些条目,并使用 size() 打印键值映射的数量。
package com.tutorialspoint;
import java.util.TreeMap;
public class TreeMapDemo {
public static void main(String[] args) {
// creating tree map
TreeMap<Integer, Student> treemap = new TreeMap<>();
// populating tree map
treemap.put(2, new Student(2, "Robert"));
treemap.put(1, new Student(1, "Julie"));
treemap.put(3, new Student(3, "Adam"));
treemap.put(6, new Student(6, "Julia"));
treemap.put(5, new Student(5, "Tom"));
// getting size of the map
System.out.println("Size of the map: "+treemap.size());
}
}
class Student {
int rollNo;
String name;
Student(int rollNo, String name){
this.rollNo = rollNo;
this.name = name;
}
@Override
public String toString() {
return "[ " + this.rollNo + ", " + this.name + " ]";
}
@Override
public boolean equals(Object obj) {
if(obj == null) return false;
Student s = (Student)obj;
return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
}
}
输出
让我们编译并运行上述程序,这将产生以下结果。
Size of the map: 5
java_util_treemap.htm
广告