W tym samouczku dowiemy się o klasie Java EnumMap i jej działaniu na przykładach.
EnumMap
Klasa zbiorów ramach Java zapewnia realizację mapy dla elementów wyliczenia.
W programie EnumMap
elementy wyliczeniowe są używane jako klucze . Implementuje interfejs Map.
Zanim się dowiemy EnumMap
, upewnij się, że znasz język Java Enums.
Tworzenie EnumMap
Aby utworzyć mapę wyliczeń, musimy najpierw zaimportować java.util.EnumMap
pakiet. Po zaimportowaniu pakietu, oto jak możemy tworzyć mapy wyliczeniowe w Javie.
enum Size ( SMALL, MEDIUM, LARGE, EXTRALARGE ) EnumMap sizes = new EnumMap(Size.class);
W powyższym przykładzie utworzyliśmy mapę wyliczeń o nazwie rozmiary.
Tutaj,
- Rozmiar - klucze wyliczenia, które są mapowane na wartości
- Integer - wartości mapy wyliczeniowej skojarzonej z odpowiednimi kluczami
Metody EnumMap
EnumMap
Klasa udostępnia metody, które pozwalają nam na wykonywanie różnych elementów na mapach enum.
Wstaw elementy do EnumMap
put()
- wstawia określone mapowanie klucz / wartość (wpis) do mapy wyliczeniowejputAll()
- wstawia wszystkie wpisy określonej mapy do tej mapy
Na przykład,
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); ) )
Wynik
EnumMap1: (SMALL = 28, MEDIUM = 32) EnumMap2: (SMALL = 28, MEDIUM = 32, LARGE = 36)
W powyższym przykładzie użyliśmy putAll()
metody, aby wstawić wszystkie elementy mapy wyliczeniowej size1 do mapy wyliczeniowej size2.
Jest również możliwe, aby wstawić elementy z innych map, takich jak HashMap
, TreeMap
itp na mapie przy użyciu enum putAll()
. Jednak wszystkie mapy powinny być tego samego typu wyliczenia.
Uzyskaj dostęp do elementów EnumMap
1. Za pomocą entrySet (), keySet () i wartości ()
entrySet()
- zwraca zestaw wszystkich mapowań kluczy / wartości (wpis) mapy wyliczeniowejkeySet()
- zwraca zestaw wszystkich kluczy mapy wyliczeniowejvalues()
- zwraca zestaw wszystkich wartości mapy wyliczeniowej
Na przykład,
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()); ) )
Wynik
EnumMap: (SMALL = 28, MEDIUM = 32, LARGE = 36, EXTRALARGE = 40) Mapowania klucza / wartości: (SMALL = 28, MEDIUM = 32, LARGE = 36, EXTRALARGE = 40) Klucze: (MAŁE, ŚREDNIE, DUŻE, EXTRALARGE) Wartości: (28, 32, 36, 40)
2. Za pomocą metody get ()
get()
Metoda zwraca wartość skojarzoną z określonym kluczem. Zwraca, null
jeśli określony klucz nie zostanie znaleziony.
Na przykład,
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); ) )
Wynik
EnumMap: (SMALL = 28, MEDIUM = 32, LARGE = 36, EXTRALARGE = 40) Wartość MEDIUM: 32
Usuń elementy EnumMap
remove(key)
- zwraca i usuwa wpis powiązany z określonym kluczem z mapyremove(key, value)
- usuwa wpis z mapy tylko wtedy, gdy określony klucz jest mapowany na określoną wartość i zwraca wartość logiczną
Na przykład,
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); ) )
Wynik
EnumMap: (SMALL = 28, MEDIUM = 32, LARGE = 36, EXTRALARGE = 40) Usunięta wartość: 32 Czy wpis (SMALL = 28) został usunięty? Prawdziwie zaktualizowana mapa wyliczeniowa: (LARGE = 36, EXTRALARGE = 40)
Zastąp elementy EnumMap
replace(key, value)
- zastępuje wartość skojarzoną z określonym kluczem nową wartościąreplace(key, old, new)
- zastępuje starą wartość nową wartością tylko wtedy, gdy stara wartość jest już skojarzona z określonym kluczemreplaceAll(function)
- zastępuje każdą wartość mapy wynikiem określonej funkcji
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); ) )
Wynik
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.
Serializable
Interfejs pozwala zajęcia się w odcinkach. Oznacza to, że obiekty klas implementujących Serializable
mogą być konwertowane na bity lub bajty.