• Tags         
  • 2018-08-14  10:26:02        
  • 157 °C    

    Java.util.Dictionary是一个抽象类,表示键值关系,并且与map类似。给定一个键,您可以存储值,并在需要时可以使用其键检索值。因此,它是键值对的列表。

    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()返回no。字典语法中的键值对
      语法:

      public abstract int size()
      Parameters : 
      -------
      Return : 
      returns the no. of key-value pairs in the Dictionary
    // 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
     
    转载请保留页面地址:https://www.breakyizhan.com/java/5377.html