首頁 > Java > java教程 > 主體

Java 枚舉映射

王林
發布: 2024-08-30 15:47:49
原創
710 人瀏覽過

Java Collection Framework 的一個成員 EnumMap,它擴展了 AbstractMap,是介面 Map 的具體實現,主要針對枚舉類型。除此之外,EnumMap 還提供其他幾個功能。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

它包括:

  • 未同步。
  • 更快;相較之下,HashMap 更慢。
  • 它保持密鑰的自然順序。
  • 高性能。
  • 不允許使用空鍵。也就是說,當存在 null 鍵時將拋出 NullPointerException。
  • 可以存在空值。
  • 將使用枚舉元素作為鍵。

有關 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 建立一個空的枚舉映射。
  • EnumMap(EnumMapK,? 擴充V 🎜>如果存在任何初始映射,將建立一個枚舉映射,其keyType 與所提到的枚舉映射相同。
  • EnumMap
  • (MapK,? 延伸K,? 延伸 V> m):
  • 將從上述映射建立一個枚舉映射。

Java EnumMap 中的方法

現在,讓我們來看看Java EnumMap的一些常用方法。
  • clear():
  • 呼叫此方法時,地圖中可用的每個映射都會被刪除。
  • clone(): 
  • 將為提到的枚舉映射傳回一個淺拷貝。
  • containsKey(Objectk): 
  • 如果地圖具有上述鍵 k 的映射,則傳回 True。
  • containsValue(Objectv): 
  • 如果對應將 1 個或多個鍵對應到提到的值 v,則傳回 True。
  • entrySet
  • ():將為地圖上可用的映射傳回集合視圖。
  • equals(Objectob): 
  • 將比較 Map 和提到的物件 ob 是否相等。
  • get(Objectk):
  • 如果映射具有上述鍵 k 的映射,則將傳回值。如果鍵沒有值,則傳回 null。
  • hashCode():
  • 將為地圖傳回雜湊程式碼。
  • keySet():
  • 將為地圖上出現的鍵傳回一個集合視圖。
  • put(Kkey, V value): 
  • 值v 將與地圖上的K鍵。
  • putAll(地圖 延伸 K,? 延伸>,? 延伸>
  • >  m): 給定地圖中的每個映射都會複製到此地圖。 remove(
  • Object
  • k):將刪除提到的鍵 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 枚舉映射輸出:

上述程式說明:

在上面的程式中,建立了兩個枚舉映射。第一個映射是用 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 枚舉映射

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 枚舉映射

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 枚舉映射的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!