dictionary类 java_Java中的Dictionary 类与示例 - Break易站

本文详细介绍了Java.util.Dictionary类,它是一个抽象类,用于表示键值对。Dictionary类提供了put()、elements()、get()、isEmpty()、keys()、remove()和size()等方法来操作键值对。通过示例代码展示了如何使用这些方法进行添加、获取、删除和遍历字典中的元素。最后,输出了字典的大小和操作结果。
摘要由CSDN通过智能技术生成

Java 集合框架

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

public abstract class Dictionary extends Object

构造函数:

Dictionary()唯一的构造函数。

util.Dictionary类的方法:

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

elements():java.util.Dictionary.elements()返回字典语法中的值表示

语法:

public abstract Enumeration elements()

Parameters :

--------

Return :

value enumeration in dictionary

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

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

语法:

public abstract boolean isEmpty()

Parameters :

------

Return :

true, if there is no key-value relation in the dictionary; else false

keys():java.util.Dictionary.keys()返回字典语法中的键表示

语法:

public abstract Enumeration keys()

Parameters :

--------

Return :

key enumeration in dictionary

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值