首页 > Java > java教程 > 正文

Java字典

WBOY
发布: 2024-08-30 15:40:34
原创
778 人浏览过

在 java Dictionary 中,util.Dictionary 是一个抽象类,表示键值存储库,其行为类似于地图。如果给出一个键和一些值,则值可以存储在字典对象中。保存值后,可以使用密钥检索该值。与地图的这种相似性就是为什么字典类通常被称为功能相似的原因。后续部分将介绍字典类的构造函数、声明和其他详细信息。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

声明

 下面是字典类的声明。

public abstract class Dictionary extends object
登录后复制

构造函数

以下是Dictionary类的唯一构造函数。

Dictionary() : 唯一构造函数。

Java Dictionary 类如何工作?

如上所述,字典类是一个抽象类,其行为与地图类似。通过提供特定的键和值,您可以将值保存在字典对象中。存储值后,可以使用密钥检索该值。您可以在此类中使用任何非空键和值。

Java 字典类方法

让我们看看Dictionary类的不同方法。

元素()

字典将返回其中可用值的枚举。

语法:

public abstract Enumeration elements()
登录后复制

示例:

import java.util.*;
public class DictionaryExample
{
public static void main(String[] args)
{
// Dictionary initialization
Dictionary dict = new Hashtable();
// Map the keys to the values given using put() method
dict.put("99", "Sarah");
dict.put("82", "Elsa");
<strong>/</strong>/ Return the eneumeration of dictionary using elements() method
for (Enumeration e = dict.elements(); e.hasMoreElements();)
{
System.out.println("Values available in the dictionary : " + e.nextElement());
}   }
}
登录后复制

输出:

Java字典

两个元素被添加到字典中,您可以使用 elements() 方法检索这些键的值。

put(K 键, V 值)

提到的键将映射到给定的值。

语法:

public abstract V put(K key, V value)
登录后复制

示例:

import java.util.*;
public class DictionaryExample
{
public static void main(String[] args)
{
// Dictionary initialization
Dictionary dict = new Hashtable();
// Map the keys to the values given using put() method
dict.put("101", "Anna");
dict.put("202", "Adam");
<strong>/</strong>/ Return the eneumeration of dictionary using elements() method
for (Enumeration e = dict.elements(); e.hasMoreElements();)
{
System.out.println("Values available in the dictionary : " + e.nextElement());
}
}
}
登录后复制

输出:

Java字典

使用 put() 方法将两个元素添加到字典中,稍后检索这些键的值。

删除(对象键)

键和对应的值将从字典中删除。

语法:

public abstract V remove(Object key)
登录后复制

示例:

import java.util.*;
public class DictionaryExample
{
public static void main(String[] args)
{
// Dictionary initialization
Dictionary dict = new Hashtable();
// Map the keys to the values given using put() method
dict.put("99", "Sarah");
dict.put("82", "Elsa");
// Return the eneumeration of dictionary using elements() method
for (Enumeration e = dict.elements(); e.hasMoreElements();)
{
System.out.println("Values available in the dictionary : " + e.nextElement());
}
// remove the element 99 using remove() method
System.out.println(" Remove the element : " + dict.remove("99"));
// Return the eneumeration of dictionary using elements() method
for (Enumeration e = dict.elements(); e.hasMoreElements();)
{
System.out.println("Values available in the dictionary after removal: " + e.nextElement());
}
}
}
登录后复制

输出:

Java字典

向字典添加两个元素后,可以使用remove()方法删除其中一个。

按键()

将返回字典中可用键的枚举。

语法:

public abstract Enumeration keys()
登录后复制

示例:

import java.util.*;
public class DictionaryExample
{
public static void main(String[] args)
{
// Dictionary initialization
Dictionary dict = new Hashtable();
// Map the keys to the values given using put() method
dict.put("101", "Anna");
dict.put("202", "Adam");
// Return the enumeration of dictionary using elements() method
for (Enumeration e = dict.keys(); e.hasMoreElements();)
{
System.out.println("Keys available in the dictionary : " + e.nextElement());
}
}
}
登录后复制

输出:

Java字典

两个元素被添加到字典中,并且可以使用keys()方法检索。

isEmpty()

检查字典是否没有映射键值。如果没有关系,则返回true。否则,错误。

语法:

public abstract booleanisEmpty()
登录后复制

示例:

import java.util.*;
public class DictionaryExample
{
public static void main(String[] args)
{
// Dictionary initialization
Dictionary dict = new Hashtable();
// Map the keys to the values given using put() method
dict.put("101", "Anna");
dict.put("202", "Adam");
// Checks no key-value pairs
System.out.println("Is there any no key-value pair : " + dict.isEmpty() + " \n " );
}
}
登录后复制

输出:

Java字典

当字典中存在键值对时,调用 isEmpty() 方法将返回 false。

获取(对象键)

字典将返回与指定键对应的值。

语法:

public abstract V get(Object key)
登录后复制

示例:

import java.util.*;
public class DictionaryExample
{
public static void main(String[] args)
{
// Dictionary initialization
Dictionary dict = new Hashtable();
// Map the keys to the values given using put() method
dict.put("99", "Sarah");
dict.put("82", "Elsa");
<strong> </strong>// Return the eneumeration of dictionary using elements() method
for (Enumeration e = dict.elements(); e.hasMoreElements();)
{
System.out.println("Values available in the dictionary : " + e.nextElement());
}
System.out.println(" Remove the element : " + dict.remove("99"));
for (Enumeration e = dict.elements(); e.hasMoreElements();)
{
System.out.println("Values available in the dictionary after removal: " + e.nextElement());
}
System.out.println("The value of the key 82 is : " + dict.get("82"));
}
}
登录后复制

输出:

Java字典

向字典添加两个元素后,您可以使用 get() 方法检索其中一个元素。

大小()

将返回条目数,可在词典中查看。

语法:

public abstract intsize()
登录后复制

示例:

import java.util.*;
public class DictionaryExample
{
public static void main(String[] args)
{
Dictionary dict = new Hashtable();
dict.put("99", "Sarah");
dict.put("82", "Elsa");
for (Enumeration e = dict.elements(); e.hasMoreElements();)
{
System.out.println("Values available in the dictionary : " + e.nextElement());
}
System.out.println("Dictionary size before removal of 99 is : " + dict.size());
// remove the element 99 using remove() method
System.out.println(" Remove the element : " + dict.remove("99"));
System.out.println("Dictionary size after removal of 99 is : " + dict.size());
}
}
登录后复制

输出:

Java字典

要确定字典的大小,您可以在删除元素之前和之后使用 size() 方法。

结论

本文通过示例详细解释了 Dictionary 类的声明、构造函数、工作方式和方法等几个方面。

以上是Java字典的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!