Java EnumMap

Bu eğitimde, örnekler yardımıyla Java EnumMap sınıfı ve operasyonları hakkında bilgi edineceğiz.

EnumMapJava koleksiyonları çerçeve sınıf bir enum elemanları için bir harita uygulamasını sağlar.

İçinde EnumMap, enum öğeleri anahtar olarak kullanılır . Harita arayüzünü uygular.

Öğrenmeden önce EnumMap, Java Enums hakkında bilgi sahibi olduğunuzdan emin olun.

Bir EnumMap Oluşturma

Bir enum eşlemesi oluşturmak için önce java.util.EnumMappaketi içe aktarmalıyız . Paketi içe aktardıktan sonra, işte Java'da enum haritalarını nasıl oluşturabiliriz.

 enum Size ( SMALL, MEDIUM, LARGE, EXTRALARGE ) EnumMap sizes = new EnumMap(Size.class); 

Yukarıdaki örnekte, boyutları adlı bir enum haritası oluşturduk.

Buraya,

  • Boyut - değerlerle eşleşen numaralandırmanın anahtarları
  • Tamsayı - karşılık gelen anahtarlarla ilişkili enum eşlemesinin değerleri

EnumMap Yöntemleri

EnumMapSınıf bize enum haritalarda çeşitli unsurları gerçekleştirmek için izin yöntemler sağlar.

EnumMap'e Eleman Ekle

  • put() - belirtilen anahtar / değer eşlemesini (giriş) enum eşlemesine ekler
  • putAll() - belirli bir haritanın tüm girişlerini bu haritaya ekler

Örneğin,

 import java.util.EnumMap; class Main ( enum Size ( SMALL, MEDIUM, LARGE, EXTRALARGE ) public static void main(String() args) ( // Creating an EnumMap of the Size enum EnumMap sizes1 = new EnumMap(Size.class); // Using the put() Method sizes1.put(Size.SMALL, 28); sizes1.put(Size.MEDIUM, 32); System.out.println("EnumMap1: " + sizes1); EnumMap sizes2 = new EnumMap(Size.class); // Using the putAll() Method sizes2.putAll(sizes1); sizes2.put(Size.LARGE, 36); System.out.println("EnumMap2: " + sizes2); ) ) 

Çıktı

 EnumMap1: (KÜÇÜK = 28, ORTA = 32) EnumMap2: (KÜÇÜK = 28, ORTA = 32, BÜYÜK = 36) 

Yukarıdaki örnekte, putAll()bir enum eşleme boyutlarının1 tüm öğelerini boyutların bir enum haritasına eklemek için yöntemi kullandık .

Gibi diğer haritalardan unsurları eklemek de mümkündür HashMap, TreeMapvb kullanarak bir enum haritaya putAll(). Ancak, tüm eşlemeler aynı enum türünde olmalıdır.

EnumMap Öğelerine Erişim

1. entrySet (), keySet () ve değerleri () kullanma

  • entrySet() - bir enum eşlemesinin tüm anahtar / değer eşlemelerinin (giriş) bir kümesini döndürür
  • keySet() - bir enum haritasının tüm anahtarlarının bir kümesini döndürür
  • values() - bir enum haritasının tüm değerlerinin bir kümesini döndürür

Örneğin,

 import java.util.EnumMap; class Main ( enum Size ( SMALL, MEDIUM, LARGE, EXTRALARGE ) public static void main(String() args) ( // Creating an EnumMap of the Size enum EnumMap sizes = new EnumMap(Size.class); sizes.put(Size.SMALL, 28); sizes.put(Size.MEDIUM, 32); sizes.put(Size.LARGE, 36); sizes.put(Size.EXTRALARGE, 40); System.out.println("EnumMap: " + sizes); // Using the entrySet() Method System.out.println("Key/Value mappings: " + sizes.entrySet()); // Using the keySet() Method System.out.println("Keys: " + sizes.keySet()); // Using the values() Method System.out.println("Values: " + sizes.values()); ) ) 

Çıktı

 EnumMap: (SMALL = 28, MEDIUM = 32, LARGE = 36, EXTRALARGE = 40) Anahtar / Değer eşlemeleri: (SMALL = 28, MEDIUM = 32, LARGE = 36, EXTRALARGE = 40) Anahtarlar: (SMALL, MEDIUM, LARGE, EXTRALARGE) Değerler: (28, 32, 36, 40) 

2. get () Yöntemini kullanma

get()Yöntem, belirli bir anahtar ile ilişkili değer geri gönderir. nullBelirtilen anahtar bulunmazsa döner .

Örneğin,

 import java.util.EnumMap; class Main ( enum Size ( SMALL, MEDIUM, LARGE, EXTRALARGE ) public static void main(String() args) ( // Creating an EnumMap of the Size enum EnumMap sizes = new EnumMap(Size.class); sizes.put(Size.SMALL, 28); sizes.put(Size.MEDIUM, 32); sizes.put(Size.LARGE, 36); sizes.put(Size.EXTRALARGE, 40); System.out.println("EnumMap: " + sizes); // Using the get() Method int value = sizes.get(Size.MEDIUM); System.out.println("Value of MEDIUM: " + value); ) ) 

Çıktı

 EnumMap: (KÜÇÜK = 28, ORTA = 32, BÜYÜK = 36, EXTRALARGE = 40) MEDIUM Değeri: 32 

EnumMap Öğelerini Kaldır

  • remove(key) - belirtilen anahtarla ilişkili girişi haritadan döndürür ve kaldırır
  • remove(key, value) - girişi haritadan yalnızca belirtilen anahtar belirtilen değere eşlenirse kaldırır ve bir boole değeri döndürür

Örneğin,

 import java.util.EnumMap; class Main ( enum Size ( SMALL, MEDIUM, LARGE, EXTRALARGE ) public static void main(String() args) ( // Creating an EnumMap of the Size enum EnumMap sizes = new EnumMap(Size.class); sizes.put(Size.SMALL, 28); sizes.put(Size.MEDIUM, 32); sizes.put(Size.LARGE, 36); sizes.put(Size.EXTRALARGE, 40); System.out.println("EnumMap: " + sizes); // Using the remove() Method int value = sizes.remove(Size.MEDIUM); System.out.println("Removed Value: " + value); boolean result = sizes.remove(Size.SMALL, 28); System.out.println("Is the entry (SMALL=28) removed? " + result); System.out.println("Updated EnumMap: " + sizes); ) ) 

Çıktı

EnumMap: (SMALL = 28, MEDIUM = 32, LARGE = 36, EXTRALARGE = 40) Kaldırılan Değer: 32 Girdi (SMALL = 28) kaldırıldı mı? Doğru Güncellenmiş EnumMap: (LARGE = 36, EXTRALARGE = 40)

EnumMap Öğelerini Değiştir

  • replace(key, value) - belirtilen anahtarla ilişkili değeri yeni değerle değiştirir
  • replace(key, old, new) - yalnızca eski değer belirtilen anahtarla zaten ilişkilendirilmişse eski değeri yeni değerle değiştirir
  • replaceAll(function) - haritanın her bir değerini belirtilen işlevin sonucuyla değiştirir
 import java.util.EnumMap; class Main ( enum Size ( SMALL, MEDIUM, LARGE, EXTRALARGE ) public static void main(String() args) ( // Creating an EnumMap of the Size enum EnumMap sizes = new EnumMap(Size.class); sizes.put(Size.SMALL, 28); sizes.put(Size.MEDIUM, 32); sizes.put(Size.LARGE, 36); sizes.put(Size.EXTRALARGE, 40); System.out.println("EnumMap: " + sizes); // Using the replace() Method sizes.replace(Size.MEDIUM, 30); sizes.replace(Size.LARGE, 36, 34); System.out.println("EnumMap using replace(): " + sizes); // Using the replaceAll() Method sizes.replaceAll((key, oldValue) -> oldValue + 3); System.out.println("EnumMap using replaceAll(): " + sizes); ) ) 

Çıktı

 EnumMap: (SMALL=28, MEDIUM=32, LARGE=36, EXTRALARGE=40) EnumMap using replace(): (SMALL=28, MEDIUM=30, LARGE=34, EXTRALARGE=40) EnumMap using replaceAll(): (SMALL=31, MEDIUM=33, LARGE=37, EXTRALARGE=43) 

In the above program, notice the statement

 sizes.replaceAll((key, oldValue) -> oldValue + 3); 

Here, the method accesses all the entries of the map. It then replaces all the values with the new values provided by the lambda expressions.

Other Methods

Method Description
clone() Creates a copy of the EnumMap
containsKey() Searches the EnumMap for the specified key and returns a boolean result
containsValue() Searches the EnumMap for the specified value and returns a boolean result
size() Returns the size of the EnumMap
clear() Removes all the entries from the EnumMap

EnumSet Vs. EnumMap

Both the EnumSet and EnumMap class provides data structures to store enum values. However, there exist some major differences between them.

  • Enum set is represented internally as a sequence of bits, whereas the enum map is represented internally as arrays.
  • Enum set is created using its predefined methods like allOf(), noneOf(), of(), etc. However, an enum map is created using its constructor.

Clonable and Serializable Interfaces

The EnumMap class also implements Cloneable and Serializable interfaces.

Cloneable Interface

It allows the EnumMap class to make a copy of instances of the class.

Serializable Interface

Whenever Java objects need to be transmitted over a network, objects need to be converted into bits or bytes. This is because Java objects cannot be transmitted over the network.

SerializableArabirim sınıfları tefrika sağlar. Bu, uygulanan sınıfların nesnelerinin Serializablebitlere veya bayta dönüştürülebileceği anlamına gelir .

Ilginç makaleler...