Java程序 在Hashtable中使用枚举法读取元素

Java程序 在Hashtable中使用枚举法读取元素

java中的枚举是预定义的接口之一,其对象用于从集合框架变量(如Stack、Vector、HashTable等)中获取数据,只在前向而非后向。

HashTable是一个类 散列表类实现了一个Map,它将键值映射到值。它将键/值对存储在哈希表中。在这个数据结构中,我们指定一个作为键的对象,以及我们想与该键相关联的值。然后对键进行散列,得到的散列码被用作表内存储值的索引。HashMap不提供任何枚举功能,而HashTable则不提供失败的枚举功能。散列表的层次结构如下。

在Hashtable中使用枚举法读取元素的Java程序

语法:

public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable, Serializable

参数:

  • K:钥匙已插入。
  • V:分配给各键的数值。

为了创建HashTable,从java.util.Hashtable中导入hash Table,其中,K、V是数据类型,如整数、字符串、浮点数等。

语法: 创建哈希表

Hashtable<K, V> ht = new Hashtable<K, V>();

实现: 大学生信息

示例

// Java Program to read elements
// using enumeration in hashtable
 
// Importing enumeration class
import java.util.Enumeration;
// Importing hash table
import java.util.Hashtable;
 
// Class
public class GFG {
 
    // Main driver method
    public static void main(String a[])
    {
 
        // Creating hash table
        Hashtable<String, String> hm
            = new Hashtable<String, String>();
 
        // Add key-value pair to Hashtable
        // Custom inputs
        hm.put("Name", "Bahubali");
        hm.put("College", "Amarnath");
        hm.put("Department", "Vedics");
 
        // enum
        Enumeration<String> keys = hm.keys();
 
        // Condition check whether element(K,V) is present
        // using hasMoreElements()
        while (keys.hasMoreElements()) {
            String key = keys.nextElement();
 
            // Print corresponding key-value pair
            System.out.println("Value of " + key
                               + " is: " + hm.get(key));
        }
 
        System.out.println();
 
        // Creating a new Hashtable
        Hashtable<String, String> hm1
            = new Hashtable<String, String>();
 
        // Adding key-value pair to Hashtable
        // Custom inputs
        hm1.put("Name", "Ravaan");
        hm1.put("College", "SriLanka");
        hm1.put("Department", "CS");
 
        // Enum
        Enumeration<String> keys1 = hm1.keys();
 
        // Condition check whether element(K,V) is present
        // using hasMoreElements()
        while (keys1.hasMoreElements()) {
            String key = keys1.nextElement();
 
            // Print corresponding key-value pair
            System.out.println("Value of " + key
                               + " is: " + hm1.get(key));
        }
 
        System.out.println();
 
        // Creating a new Hashtable
        Hashtable<String, String> hm2
            = new Hashtable<String, String>();
 
        // Adding key-value pair to Hashtable
        // Custom inputs
        hm2.put("Name", "Kattappa");
        hm2.put("College", "Beardo");
        hm2.put("Department", "War");
 
        /// enum
        Enumeration<String> keys2 = hm2.keys();
 
        // Condition check whether element(K,V) is present
        // using hasMoreElements()
        while (keys2.hasMoreElements()) {
            String key = keys2.nextElement();
 
            // Print corresponding key-value pair
            System.out.println("Value of " + key
                               + " is: " + hm2.get(key));
        }
    }
}

输出

Value of Name is: Bahubali
Value of College is: Amarnath
Value of Department is: Vedics

Value of Name is: Ravaan
Value of College is: SriLanka
Value of Department is: CS

Value of Name is: Kattappa
Value of College is: Beardo
Value of Department is: War

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程