📜  Java Java类

📅  最后修改于: 2022-05-13 01:55:18.074000             🧑  作者: Mango

Java Java类

util.Dictionary是一个抽象类,表示键值关系,其工作方式类似于地图。给定一个键,您可以存储值,并在需要时使用其键检索值。因此,它是一个键值对列表。

宣言

public abstract class Dictionary extends Object

构造函数:
Dictionary()唯一的构造函数。

util.Dictionary 类的方法:

1. put(K key, V value) : Java.util.Dictionary.put(K key, V value)将键值对添加到字典中。

句法 :

public abstract V put(K key, V value)
Parameters : 
-> key
-> value
Return : 
key-value pair mapped in the dictionary

2. elements() : Java.util.Dictionary.elements()返回字典中的值表示。

句法 :

public abstract Enumeration elements()
Parameters : 
--------
Return : 
value enumeration in dictionary

3. get(Object key) : Java.util.Dictionary.get(Object key)返回与字典中的参数键映射的值。

句法 :

public abstract V get(Object key)
Parameters : 
key - key whose mapped value we want
Return : 
value mapped with the argumented key

4. isEmpty() : Java.util.Dictionary.isEmpty()检查字典是否为空。

句法 :

public abstract boolean isEmpty()
Parameters : 
------
Return : 
true, if there is no key-value relation in the dictionary; else false

5. keys() : Java.util.Dictionary.keys()返回字典中的键表示。

句法 :

public abstract Enumeration keys()
Parameters : 
--------
Return : 
key enumeration in dictionary

6. remove(Object key) : Java.util.Dictionary.remove(Object key)删除与参数键映射的键值对。

句法 :

public abstract V remove(Object key)
Parameters : 
key : key to be removed
Return : 
value mapped with the key

7. size() : Java.util.Dictionary.size()返回编号。字典中的键值对。

句法 :

public abstract int size()
Parameters : 
-------
Return : 
returns the no. of key-value pairs in the Dictionary
Java
// Java Program explaining util.Dictionary class Methods
// put(), elements(), get(), isEmpty(), keys()
// remove(), size()
 
import java.util.*;
public class New_Class
{
    public static void main(String[] args)
    {
 
        // Initializing a Dictionary
        Dictionary geek = new Hashtable();
 
        // put() method
        geek.put("123", "Code");
        geek.put("456", "Program");
 
        // elements() method :
        for (Enumeration i = geek.elements(); i.hasMoreElements();)
        {
            System.out.println("Value in Dictionary : " + i.nextElement());
        }
 
        // get() method :
        System.out.println("\nValue at key = 6 : " + geek.get("6"));
        System.out.println("Value at key = 456 : " + geek.get("123"));
 
        // isEmpty() method :
        System.out.println("\nThere is no key-value pair : " + geek.isEmpty() + "\n");
 
        // keys() method :
        for (Enumeration k = geek.keys(); k.hasMoreElements();)
        {
            System.out.println("Keys in Dictionary : " + k.nextElement());
        }
 
        // remove() method :
        System.out.println("\nRemove : " + geek.remove("123"));
        System.out.println("Check the value of removed key : " + geek.get("123"));
 
        System.out.println("\nSize of Dictionary : " + geek.size());
 
    }
}


输出:

Value in Dictionary : Code
Value in Dictionary : Program

Value at key = 6 : null
Value at key = 456 : Code

There is no key-value pair : false

Keys in Dictionary : 123
Keys in Dictionary : 456

Remove : Code
Check the value of removed key : null

Size of Dictionary : 1