I den här handledningen lär vi oss om Java EnumMap-klassen och dess funktioner med hjälp av exempel.
Den EnumMap
klass av samlingar ramverket Java ger en karta genomförande av delar av en enum.
I EnumMap
, enum-element används som nycklar . Det implementerar Map-gränssnittet.
Innan vi lär oss om EnumMap
, se till att känna till Java Enums.
Skapa en EnumMap
För att skapa en enum-karta måste vi först importera java.util.EnumMap
paketet. När vi väl har importerat paketet, här kan vi skapa enum-kartor i Java.
enum Size ( SMALL, MEDIUM, LARGE, EXTRALARGE ) EnumMap sizes = new EnumMap(Size.class);
I exemplet ovan har vi skapat en enum-karta med namnet storlekar.
Här,
- Storlek - tangenterna för det enum som mappar till värden
- Heltal - värden på enumskartan associerad med motsvarande tangenter
Metoder för EnumMap
I EnumMap
klassen innehåller metoder som tillåter oss att utföra olika element på ENUM kartor.
Infoga element till EnumMap
put()
- infogar den angivna nyckel- / värdekartläggningen (post) på enum-kartanputAll()
- infogar alla poster på en specifik karta på denna karta
Till exempel,
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); ) )
Produktion
EnumMap1: (SMALL = 28, MEDIUM = 32) EnumMap2: (SMALL = 28, MEDIUM = 32, LARGE = 36)
I exemplet ovan har vi använt putAll()
metoden för att infoga alla element i en enum-kartstorlekar1 till en enum-karta över storlekar2.
Det är också möjligt att sätta in element från andra kartor såsom HashMap
, TreeMap
etc. till en enum karta med hjälp av putAll()
. Alla kartor bör dock vara av samma enumtyp.
Åtkomst till EnumMap-element
1. Använda entrySet (), keySet () och värden ()
entrySet()
- returnerar en uppsättning av alla nycklar / värderingsmappning (inmatning) på en enum-kartakeySet()
- returnerar en uppsättning av alla tangenter på en enum-kartavalues()
- returnerar en uppsättning av alla värden på en enum-karta
Till exempel,
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()); ) )
Produktion
EnumMap: (SMALL = 28, MEDIUM = 32, LARGE = 36, EXTRALARGE = 40) Nyckel- / värdekartläggningar: (SMALL = 28, MEDIUM = 32, LARGE = 36, EXTRALARGE = 40) Knappar: (SMALL, MEDIUM, LARGE, EXTRALARGE) Värden: (28, 32, 36, 40)
2. Använd get () -metoden
Den get()
metod returnerar värdet associerat med den angivna nyckeln. Den returneras null
om den angivna nyckeln inte hittas.
Till exempel,
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); ) )
Produktion
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 mapremove(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 valuereplace(key, old, new)
- replaces the old value with the new value only if the old value is already associated with the specified keyreplaceAll(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.
Den Serializable
gränssnittet kan klasser som ska serialiseras. Detta innebär att objekt från klasserna som implementeras Serializable
kan konverteras till bitar eller byte.