Java EnumMap

În acest tutorial, vom afla despre clasa Java EnumMap și operațiunile sale cu ajutorul unor exemple.

EnumMapClasa cadrului colecțiilor Java oferă o implementare hartă pentru elemente ale unui enum.

În EnumMap, elementele enum sunt utilizate ca chei . Implementează interfața Map.

Înainte de a afla EnumMap, asigurați-vă că știți despre Java Enums.

Crearea unui EnumMap

Pentru a crea o hartă enum, trebuie mai java.util.EnumMapîntâi să importăm pachetul. Odată ce am importat pachetul, iată cum putem crea hărți enum în Java.

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

În exemplul de mai sus, am creat o hartă enum numită dimensiuni.

Aici,

  • Dimensiune - chei ale enumului care se mapează la valori
  • Întreg - valorile hărții enum asociate cu tastele corespunzătoare

Metode de EnumMap

EnumMapClasa prevede metode care ne permit de a efectua diverse elemente de pe hărțile ENUM.

Inserați elemente în EnumMap

  • put() - inserează maparea cheii / valorilor specificate (intrare) pe harta enum
  • putAll() - inserează toate intrările unei hărți specificate pe această hartă

De exemplu,

 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); ) ) 

Ieșire

 EnumMap1: (SMALL = 28, MEDIUM = 32) EnumMap2: (SMALL = 28, MEDIUM = 32, LARGE = 36) 

În exemplul de mai sus, am folosit putAll()metoda pentru a insera toate elementele unei hărți enum dimensiuni1 într-o hartă enum de dimensiuni2.

De asemenea , este posibil să se introducă elemente din alte hărți , cum ar fi HashMap, TreeMapetc la o hartă folosind enum putAll(). Cu toate acestea, toate hărțile ar trebui să fie de același tip enum.

Accesați elementele EnumMap

1. Utilizarea entrySet (), keySet () și valori ()

  • entrySet() - returnează un set cu toate tastele / maparea valorilor (intrarea) unei hărți enum
  • keySet() - returnează un set de toate tastele unei hărți enum
  • values() - returnează un set cu toate valorile unei hărți enum

De exemplu,

 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()); ) ) 

Ieșire

 EnumMap: (SMALL = 28, MEDIUM = 32, LARGE = 36, EXTRALARGE = 40) Asocieri cheie / valoare: (SMALL = 28, MEDIUM = 32, LARGE = 36, EXTRALARGE = 40) Taste: (SMALL, MEDIUM, LARGE, EXTRALARGE) Valori: (28, 32, 36, 40) 

2. Folosind metoda get ()

get()Metoda returnează valoarea asociată cu cheia specificată. Revine nulldacă nu este găsită cheia specificată.

De exemplu,

 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); ) ) 

Ieșire

 EnumMap: (SMALL=28, MEDIUM=32, LARGE=36, EXTRALARGE=40) Value of MEDIUM: 32 

Remove EnumMap Elements

  • remove(key) - returns and removes the entry associated with the specified key from the map
  • remove(key, value) - removes the entry from the map only if the specified key mapped to the specified value and return a boolean value

For example,

 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); ) ) 

Output

 EnumMap: (SMALL=28, MEDIUM=32, LARGE=36, EXTRALARGE=40) Removed Value: 32 Is the entry (SMALL=28) removed? True Updated EnumMap: (LARGE=36, EXTRALARGE=40) 

Replace EnumMap Elements

  • replace(key, value) - replaces the value associated with the specified key by the new value
  • replace(key, old, new) - replaces the old value with the new value only if the old value is already associated with the specified key
  • replaceAll(function) - replaces each value of the map with the result of the specified function
 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); ) ) 

Output

 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.

SerializableInterfața permite clase să fie serializat. Aceasta înseamnă că obiectele claselor de implementare Serializablepot fi convertite în biți sau octeți.

Articole interesante...