java map大小_Java中HashMap的size()方法: HashMap.size() - Break易站
Java中的HashMap
HashMap類的java.util.HashMap.size()方法用于獲取哈希映射HashMap中映射的大小,該映射引用Map中鍵值對或映射的數(shù)量。
句法:
Hash_Map.size()
參數(shù):該方法不接受任何參數(shù)。
返回值:該方法返回映射的大小,這也表示映射中存在的鍵值對的數(shù)量。
下面的程序說明了java.util.HashMap.size()的工作原理
HashMap.size()程序1:將字符串值映射到整數(shù)鍵。
// Java code to illustrate the size() method
import java.util.*;
public class Hash_Map_Demo {
public static void main(String[] args)
{
// Creating an empty HashMap
HashMap hash_map = new HashMap();
// Mapping string values to int keys
hash_map.put(10, "Geeks");
hash_map.put(15, "4");
hash_map.put(20, "Geeks");
hash_map.put(25, "Welcomes");
hash_map.put(30, "You");
// Displaying the HashMap
System.out.println("Initial Mappings are: " + hash_map);
// Displaying the size of the map
System.out.println("The size of the map is " + hash_map.size());
}
}
輸出:
Initial Mappings are: {20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=4}
The size of the map is 5
HashMap.size()程序2:將整數(shù)值映射到字符串鍵。
// Java code to illustrate the size() method
import java.util.*;
public class Hash_Map_Demo {
public static void main(String[] args)
{
// Creating an empty HashMap
HashMap hash_map = new HashMap();
// Mapping int values to string keys
hash_map.put("Geeks", 10);
hash_map.put("4", 15);
hash_map.put("Geeks", 20);
hash_map.put("Welcomes", 25);
hash_map.put("You", 30);
// Displaying the HashMap
System.out.println("Initial Mappings are: " + hash_map);
// Displaying the size of the map
System.out.println("The size of the map is " + hash_map.size());
}
}
輸出:
Initial Mappings are: {4=15, Geeks=20, You=30, Welcomes=25}
The size of the map is 4
注意:對于具有不同數(shù)據(jù)類型的變體和組合的任何類型的映射,都可以執(zhí)行相同的操作。
總結(jié)
以上是生活随笔為你收集整理的java map大小_Java中HashMap的size()方法: HashMap.size() - Break易站的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vs未能找到程序集platform.wi
- 下一篇: 你的目的是什么是谁指使你_电视剧《谁说我