Java 字典 size()方法及实例

Java 字典 size()方法及实例

Java中Dictionary类的size()方法是用来知道Dictionary的大小或Dictionary中存在的独立键的数量。

语法

DICTIONARY.size()

参数:

该方法不接受任何参数。

返回值:

该方法返回字典中存在的键的数量。

下面的程序说明了字典的 size() 方法:

程序 1 :

// Java Code to illustrate size()
import java.util.*;
  
public class Dictionary_Demo {
    public static void main(String args[])
    {
  
        // Creating a dictionary of Hashtable
        Dictionary<Integer, String> dict
            = new Hashtable<Integer, String>();
  
        // Inserting elements into the Dictionary
        dict.put(10, "Geeks");
        dict.put(15, "4");
        dict.put(10, "Geeks");
        dict.put(25, "Welcomes");
        dict.put(30, "You");
  
        // Displaying the Dictionary
        System.out.println("Dictionary: " + dict);
  
        // Displaying the size of the dictionary
        System.out.println("The size of the dictionary is "
                           + dict.size());
    }
}

输出:

Dictionary: {10=Geeks, 30=You, 15=4, 25=Welcomes}
The size of the dictionary is 4

程序2

// Java Code to illustrate size()
import java.util.*;
  
public class Dictionary_Demo {
    public static void main(String args[])
    {
  
        // Creating a dictionary of Hashtable
        Dictionary<String, Integer> dict
            = new Hashtable<String, Integer>();
  
        // Inserting elements into the table
        dict.put("Geek", 10);
        dict.put("4", 15);
        dict.put("Geeks", 20);
        dict.put("Welcomes", 25);
        dict.put("You", 30);
  
        // Displaying the Dictionary
        System.out.println("Dictionary: " + dict);
  
        // Displaying the size of the dictionary
        System.out.println("The size of the dictionary is "
                           + dict.size());
    }
}

输出:

Dictionary: {You=30, Welcomes=25, 4=15, Geeks=20, Geek=10}
The size of the dictionary is 5

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程