ホームページ  >  記事  >  Java  >  Java EnumMap

Java EnumMap

王林
王林オリジナル
2024-08-30 15:47:49713ブラウズ

AbstractMap を拡張する EnumMap として知られる Java Collection Framework メンバーは、主に列挙型用のインターフェイス マップの特定の実装です。それに加えて、EnumMap には他のいくつかの機能もあります。

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

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

内容:

  • 同期されていません。
  • より速く、これに比べて、HashMap は遅いです。
  • キーの自然な順序で保持されます。
  • 高性能。
  • Null キーは許可されません。つまり、null キーが存在する場合、NullPointerException がスローされます。
  • Null 値が存在する可能性があります。
  • キーとして、enum 要素が使用されます。

Java EnumMap の詳細については、次のセクションで説明します。

構文

Java EnumMap は、以下の構文を使用して宣言できます。

public class EnumMap<Key extends Enum<Key>,Value> extends AbstractMap<Key,Value> implements Serializable, Cloneable

Java EnumMap のコンストラクター

Java EnumMap で一般的に使用されるコンストラクターは次のとおりです。

  • EnumMap(クラス<K>keyType): 空の列挙型マップが、前述の keyType で作成されます。
  • EnumMap(EnumMapK,? extends V> m): 初期マッピングが存在する場合、列挙型マップは、前述の列挙型マップと同じ keyType で作成されます。
  • EnumMap(Map<K,? extends V> m): 言及されたマップから列挙型マップが作成されます。

Java EnumMap のメソッド

ここで、Java EnumMap の一般的に使用されるメソッドをいくつか見てみましょう。

  • clear(): このメソッドを呼び出すと、マップ内で使用可能なすべてのマッピングが削除されます。
  • clone(): 言及された列挙型マップの浅いコピーが返されます。
  • containsKey(Objectk):マップに指定されたキー k のマッピングがある場合、True が返されます。
  • containsValue(Objectv):マップが 1 つ以上のキーを指定された値 v にマッピングする場合、True が返されます。
  • entrySet(): マップ上で使用可能なマッピングのセット ビューが返されます。
  • equals(Objectob): マップと言及されたオブジェクト ob が等しいかどうか比較されます。
  • get(Objectk):マップに指定されたキー k のマッピングがある場合、値が返されます。キーの値がない場合は、null が返されます。
  • hashCode(): マップのハッシュ コードが返されます。
  • keySet(): マップ上に存在するキーのセット ビューが返されます。
  • put(Kkey, V value): 値 v は、地図上のキー K。
  • putAll(MapK,? extends V> m): 指定されたマップのすべてのマッピングがこのマップにコピーされます。
  • remove(Objectk): 指定されたキー k のマッピングが削除されます。
  • size(): マップ内で使用可能なキーと値のマッピングの数が返されます。
  • values(): マップ上で使用可能な値のコレクション ビューが返されます。

Java EnumMap の実装例

Java Enum Map についてさらに理解するために、上記のメソッドをいくつかのプログラムに実装してみましょう。

例 #1

列挙型マップを作成し、要素を別の列挙型マップにコピーするサンプル プログラム。

コード:

import java.util.EnumMap;
class JavaEnumMapExample {
enum fruits {
APPLE, ORANGE, GRAPES, KIWI
}
public static void main(String[] args) {
// EnumMap creation of the fruits enum
EnumMap<fruits, Integer> fr = new EnumMap<>(fruits.class);
// add key-value to the mapping using the method put()
fr.put(fruits.APPLE, 2);
fr.put(fruits.ORANGE, 5);
System.out.println("The key-value pairs in EnumMap 1 is :  " + fr);
//create another enum map fru
EnumMap<fruits, Integer> fru = new EnumMap<>(fruits.class);
// copy all the elements from first enum map to this using the methodputAll()
fru.putAll(fr);
fru.put(fruits.GRAPES, 3);
System.out.println("The key-value pairs in EnumMap 2 is : " + fru);
}
}

出力:

Java EnumMap

上記プログラムの説明: 上記プログラムでは、2 つの enum マップが作成されます。最初のマップは 2 つの要素で作成され、2 番目のマップは最初のマップの要素をコピーして作成されます。それに加えて、2番目のマップには追加要素も追加されます。これらは put() メソッドと putAll() メソッドを使用して行われます。

例 #2

列挙型マップを作成し、キーと値を個別に取得するサンプル プログラム。

コード:

import java.util.EnumMap;
class JavaEnumMapExample {
enum fruits {
APPLE, ORANGE, GRAPES, KIWI
}
public static void main(String[] args) {
// EnumMap creation of the fruits enum
EnumMap<fruits, Integer> fr = new EnumMap<>(fruits.class);
// add key-value to the mapping using the method put()
fr.put(fruits.APPLE, 2);
fr.put(fruits.ORANGE, 5);
System.out.println("The key-value pairs in EnumMap 1 is :  " + fr);
// print all the keys in the enum map using the method keySet()
System.out.println("The keys in enum map 1 are : " + fr.keySet());
// print all the values in the enum map using the method values()
System.out.println("The values in enum map 1 are : " + fr.values());
//create another enum map fru
EnumMap<fruits, Integer> fru = new EnumMap<>(fruits.class);
// copy all the elements from first enum map to this using the methodputAll()
fru.putAll(fr);
fru.put(fruits.GRAPES, 3);
System.out.println("The key-value pairs in EnumMap 2 is : " + fru);
// print all the keys in the enum map using the method keySet()
System.out.println("The keys in enum map 2 are : " + fru.keySet());
// print all the values in the enum map using the method values()
System.out.println("The values in enum map 2 are : " + fru.values());
}
}

出力:

Java EnumMap

Explanation to the above program: Similar to the first program, two enum maps are available here. This program displays the maps’ keys and values independently using the methods keyset() and values(), respectively.

Example #3

Sample program to remove an element from the enum map

Code:

import java.util.EnumMap;
class JavaEnumMapExample {
enum fruits {
APPLE, ORANGE, GRAPES, KIWI
}
public static void main(String[] args) {
// EnumMap creation of the fruits enum
EnumMap<fruits, Integer> fr = new EnumMap<>(fruits.class);
// add key-value to the mapping using the method put()
fr.put(fruits.APPLE, 2);
fr.put(fruits.ORANGE, 5);
System.out.println("The key-value pairs in EnumMap :  " + fr);
// remove an element using the method remove()
int val = fr.remove(fruits.APPLE);
System.out.println("Removed Value: " + val);
System.out.println("The key-value pairs in EnumMap after removing apple :  " + fr);
}
}

Output:

Java EnumMap

Explanation to the above program: In this program, an element is removed from the map using the remove() method and the resultant enum map is printed in the next step.

Conclusion

A detailed explanation of all the aspects such as declaration, methods, constructors of Java EnumMap is discussed in this document in detail.

以上がJava EnumMapの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

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