Java EnumMap

Šajā apmācībā mēs ar piemēru palīdzību uzzināsim par Java EnumMap klasi un tās darbībām.

EnumMapJava kolekcijas ietvaros klase nodrošina kartes ieviešanu par elementu ar enum.

Vietā EnumMapenum elementi tiek izmantoti kā atslēgas . Tas ievieš kartes saskarni.

Pirms mēs uzzinām par EnumMap, pārliecinieties, ka zināt par Java Enums.

EnumMap izveide

Lai izveidotu uzskaites karti, mums vispirms ir jāimportē java.util.EnumMappakete. Kad pakotne ir importēta, šeit mēs varam izveidot Java kartes enum.

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

Iepriekš minētajā piemērā mēs esam izveidojuši enum karti ar nosaukumiem lielumi.

Šeit,

  • Izmērs - uzskaites taustiņi , kas norāda vērtības
  • Vesels skaitlis - uzskaites kartes vērtības , kas saistītas ar atbilstošajiem taustiņiem

EnumMap metodes

EnumMapKlase nodrošina metodes, kas ļauj mums veikt dažādus elementus par enum kartēm.

Ievietojiet elementus EnumMap

  • put() - ievieto norādīto atslēgu / vērtību kartēšanu (ierakstu) uzskaites kartē
  • putAll() - ievieto šajā kartē visus norādītās kartes ierakstus

Piemēram,

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

Rezultāts

 EnumMap1: (MAZS = 28, VIDĒJS = 32) EnumMap2: (MAZS = 28, VIDĒJS = 32, LIELS = 36) 

Iepriekš minētajā piemērā mēs izmantojām putAll()metodi, lai visus enum kartes izmēru1 elementus ievietotu enum kartes lielumos2.

Tāpat ir iespējams ievietot elementus no citām kartēm, piemēram HashMap, TreeMaputt uz ENUM kartē, izmantojot putAll(). Tomēr visām kartēm jābūt viena un tā paša veida.

Piekļūstiet EnumMap elementiem

1. Izmantojot entrySet (), keySet () un vērtības ()

  • entrySet() - atgriež visu atslēgu / vērtību kopu (ierakstu) kopu uzskaites kartē
  • keySet() - atgriež visu enum kartes taustiņu kopu
  • values() - atgriež visu enum kartes vērtību kopu

Piemēram,

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

Rezultāts

 EnumMap: (MAZS = 28, VIDĒJS = 32, LIELS = 36, EXTRALARGE = 40) Atslēgu / vērtību kartējums: (MAZS = 28, VIDĒJS = 32, LIELS = 36, EXTRALARGE = 40) Taustiņi: (MAZS, Vidējs, LIELS, EXTRALARGE) Vērtības: (28, 32, 36, 40) 

2. Izmantojot metodi get ()

get()Metode atgriež vērtību, kas saistīta ar norādīto atslēgu. Tas atgriežas, nullja norādītā atslēga nav atrasta.

Piemēram,

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

Rezultāts

 EnumMap: (MAZS = 28, VIDĒJS = 32, LIELS = 36, EXTRALARGE = 40) MEDIUM vērtība: 32 

Noņemiet EnumMap Elements

  • remove(key) - atgriež un noņem no kartes ar norādīto atslēgu saistīto ierakstu
  • remove(key, value) - noņem ierakstu no kartes tikai tad, ja norādītā atslēga ir piesaistīta norādītajai vērtībai un atgriež Būla vērtību

Piemēram,

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

Rezultāts

EnumMap: (MAZS = 28, VIDĒJS = 32, LIELS = 36, EXTRALARGE = 40) Noņemtā vērtība: 32 Vai ieraksts (MAZI = 28) ir noņemts? Patiesi atjaunināta EnumMap: (LARGE = 36, EXTRALARGE = 40)

Nomainiet EnumMap elementus

  • replace(key, value) - aizstāj vērtību, kas saistīta ar norādīto atslēgu, ar jauno vērtību
  • replace(key, old, new) - veco vērtību aizstāj ar jauno tikai tad, ja vecā vērtība jau ir saistīta ar norādīto atslēgu
  • replaceAll(function) - aizstāj katru kartes vērtību ar norādītās funkcijas rezultātu
 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); ) ) 

Rezultāts

 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.

SerializableSaskarne ļauj klasēm serializēja. Tas nozīmē, ka īstenojamo klašu objektus Serializablevar pārvērst bitos vai baitos.

Interesanti raksti...