util。词典 是一个抽象类,表示 关键值 关系和工作原理类似于地图。给定一个键,您可以存储值,并在需要时使用其键检索该值。因此,它是一个键值对列表。
公告
public abstract class Dictionary extends Object
施工人员: 字典() 唯一的构造器。
util的方法。字典类:
1.put(K键,V值):java。util。词典输入(K键,V值) 将键值对添加到字典中。
语法:
public abstract V put(K key, V value)Parameters : -> key-> valueReturn : key-value pair mapped in the dictionary
2.elements():java。util。词典元素() 返回字典中的值表示形式。
语法:
public abstract Enumeration elements()Parameters : --------Return : value enumeration in dictionary
3.获取(对象键):java。util。词典获取(对象键) 返回字典中与参数化键映射的值。
语法:
public abstract V get(Object key)Parameters : key - key whose mapped value we wantReturn : value mapped with the argumented key
4.isEmpty():java。util。词典isEmpty() 检查字典是否为空。
语法:
public abstract boolean isEmpty()Parameters : ------Return : true, if there is no key-value relation in the dictionary; else false
5.keys():java。util。词典钥匙() 返回字典中的键表示形式。
语法:
public abstract Enumeration keys()Parameters : --------Return : key enumeration in dictionary
6.移除(对象键):java。util。词典移除(对象键) 移除与参数化键映射的键值对。
语法:
public abstract V remove(Object key)Parameters : key : key to be removedReturn : value mapped with the key
7.size():java。util。词典大小() 返回字典中键值对的数目。
语法:
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("Value at key = 6 : " + geek.get(" 6 ")); System.out.println("Value at key = 456 : " + geek.get(" 123 ")); // isEmpty() method : System.out.println("There is no key-value pair : " + geek.isEmpty() + ""); // keys() method : for (Enumeration k = geek.keys(); k.hasMoreElements();) { System.out.println("Keys in Dictionary : " + k.nextElement()); } // remove() method : System.out.println("Remove : " + geek.remove(" 123 ")); System.out.println("Check the value of removed key : " + geek.get(" 123 ")); System.out.println("Size of Dictionary : " + geek.size()); } } |
输出:
Value in Dictionary : CodeValue in Dictionary : ProgramValue at key = 6 : nullValue at key = 456 : CodeThere is no key-value pair : falseKeys in Dictionary : 123Keys in Dictionary : 456Remove : CodeCheck the value of removed key : nullSize of Dictionary : 1
本文由 莫希特·古普塔(Mohit Gupta_OMG) .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。