ホームページ  >  記事  >  Java  >  Java辞書

Java辞書

WBOY
WBOYオリジナル
2024-08-30 15:40:34779ブラウズ

Java Dictionary の util.Dictionary は、key-value ストレージ リポジトリを示し、マップのように動作する抽象クラスです。キーといくつかの値が指定されている場合、値を辞書オブジェクトに格納できます。値を保存した後、キーを使用して値を取得できます。このマップとの類似性が、辞書クラスが同様に機能するとよく言われる理由です。後続のセクションでは、ディクショナリ クラスのコンストラクター、宣言、およびその他の詳細について説明します。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

宣言

以下は辞書クラスの宣言です。

public abstract class Dictionary extends object

コンストラクター

以下は Dictionary クラスの唯一のコンストラクターです。

Dictionary() : 唯一のコンストラクター。

Java Dictionary クラスはどのように機能しますか?

上で説明したように、辞書クラスはマップと同様に動作する抽象クラスです。特定のキーと値を指定すると、値をディクショナリ オブジェクトに保存できます。値を保存した後、キーを使用して値を取得できます。このクラスでは、null 以外の任意のキーと値を使用できます。

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辞書

2 つの要素が辞書に追加され、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() メソッドを使用して 2 つの要素が辞書に追加され、それらのキーの値が後で取得されます。

削除(オブジェクトキー)

キーと対応する値は辞書から削除されます。

構文:

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辞書

辞書に 2 つの要素を追加した後、remove() メソッドを使用してそのうちの 1 つを削除できます。

キー()

辞書で使用可能なキーの列挙が返されます。

構文:

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辞書

2 つの要素が辞書に追加され、keys() メソッドを使用して取得できます。

isEmpty()

辞書がキー値をマッピングしていないかどうかをチェックします。関係がない場合はtrueを返します。それ以外の場合は false。

構文:

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 が返されます。

get(オブジェクトキー)

辞書は、指定されたキーに対応する値を返します。

構文:

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辞書

ディクショナリに 2 つの要素を追加した後、get() メソッドを使用してそのうちの 1 つを取得できます。

サイズ()

辞書で使用できるエントリの数が返されます。

構文:

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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:Javaリポジトリ次の記事:Javaリポジトリ