Java WeakHashMap

Šajā apmācībā mēs ar piemēru palīdzību uzzināsim par Java WeakHashMap un tā darbībām. Mēs arī uzzināsim par atšķirībām starp WeakHashMap un HashMap

WeakHashMapJava kolekcijas ietvaros klase nodrošina iezīme hash tabulas datu struktūra …

Tas ievieš kartes saskarni.

Piezīme : Vājā hashmap taustiņi ir WeakReference tipa.

Vāja atsauces veida objekts var būt atkritumi, kas savākti Java, ja atsauce vairs netiek izmantota programmā.

Mācīsimies vispirms izveidot vāju jaukšanas karti. Tad mēs uzzināsim, kā tas atšķiras no hashmap.

Izveidojiet WeakHashMap

Lai izveidotu vāju hashmap, mums vispirms ir jāimportē java.util.WeakHashMappakete. Kad pakotne ir importēta, šeit ir norādīts, kā Java var izveidot vājus hashmaps.

 //WeakHashMap creation with capacity 8 and load factor 0.6 WeakHashMap numbers = new WeakHashMap(8, 0.6); 

Iepriekš minētajā kodā mēs esam izveidojuši vāju hashmap nosaukumu numurus.

Šeit,

  • Atslēga - unikāls identifikators, ko izmanto, lai katru elementu (vērtību) saistītu kartē
  • Vērtība - elementi, kas saistīti ar atslēgām kartē

Ievērojiet daļu new WeakHashMap(8, 0.6). Pirmais parametrs ir kapacitāte, bet otrais parametrs - loadFactor .

  • ietilpība - šīs kartes ietilpība ir 8. Tas nozīmē, ka tajā var saglabāt 8 ierakstus.
  • loadFactor - šīs kartes slodzes koeficients ir 0,6. Tas nozīmē, ka ikreiz, kad mūsu jaukšanas tabula ir aizpildīta par 60%, ieraksti tiek pārvietoti uz jaunu jaukšanas tabulu, kas ir divreiz lielāka par sākotnējās jaukšanas tabulas lielumu.

Noklusējuma jauda un slodzes koeficients

Ir iespējams izveidot vāju hashmap, nenosakot tā jaudu un slodzes koeficientu. Piemēram,

 // WeakHashMap with default capacity and load factor WeakHashMap numbers1 = new WeakHashMap(); 

Pēc noklusējuma,

  • kartes ietilpība būs 16 vietas
  • slodzes koeficients būs 0,75

Atšķirības starp HashMap un WeakHashMap

Ļaujiet mums redzēt vājas hashmap ieviešanu Java.

 import java.util.WeakHashMap; class Main ( public static void main(String() args) ( // Creating WeakHashMap of numbers WeakHashMap numbers = new WeakHashMap(); String two = new String("Two"); Integer twoValue = 2; String four = new String("Four"); Integer fourValue = 4; // Inserting elements numbers.put(two, twoValue); numbers.put(four, fourValue); System.out.println("WeakHashMap: " + numbers); // Make the reference null two = null; // Perform garbage collection System.gc(); System.out.println("WeakHashMap after garbage collection: " + numbers); ) ) 

Rezultāts

 WeakHashMap: (četri = 4, divi = 2) WeakHashMap pēc atkritumu savākšanas: (četri) 

Kā redzam, kad vājā hashmap otrais taustiņš ir iestatīts nullun tiek veikta atkritumu savākšana, atslēga tiek noņemta.

Tas ir tāpēc, ka atšķirībā no hashmaps, vāju hashmaps taustiņiem ir vājš atsauces tips. Tas nozīmē, ka atkritumu savācējs noņem kartes ievadi, ja šī ieraksta atslēga vairs netiek izmantota. Tas ir noderīgi, lai ietaupītu resursus.

Tagad redzēsim to pašu ieviešanu hashmap.

 import java.util.HashMap; class Main ( public static void main(String() args) ( // Creating HashMap of even numbers HashMap numbers = new HashMap(); String two = new String("Two"); Integer twoValue = 2; String four = new String("Four"); Integer fourValue = 4; // Inserting elements numbers.put(two, twoValue); numbers.put(four, fourValue); System.out.println("HashMap: " + numbers); // Make the reference null two = null; // Perform garbage collection System.gc(); System.out.println("HashMap after garbage collection: " + numbers); ) ) 

Rezultāts

 HashMap: (četri = 4, divi = 2) HashMap pēc atkritumu savākšanas: (četri = 4, divi = 2) 

Šeit, kad otrais hashmap taustiņš ir iestatīts uz nullatkritumu savākšanu, atslēga netiek noņemta.

Tas ir tāpēc, ka atšķirībā no vājajiem hashmaps taustiņiem ir spēcīgs atsauces tips. Tas nozīmē, ka atkritumu savācējs nenoņem kartes ievadi, kaut arī šī ieraksta atslēga vairs netiek izmantota.

Piezīme . Visas hashmaps un vājo hashmaps funkcijas ir līdzīgas, izņemot to, ka vāja hashmap taustiņiem ir vāja atsauce, turpretim hashmap taustiņiem ir spēcīga atsauce.

WeakHashMap izveide no citām kartēm

Lūk, kā mēs varam izveidot vāju hashmap no citām kartēm.

 import java.util.HashMap; import java.util.WeakHashMap; class Main ( public static void main(String() args) ( // Creating a hashmap of even numbers HashMap evenNumbers = new HashMap(); String two = new String("Two"); Integer twoValue = 2; evenNumbers.put(two, twoValue); System.out.println("HashMap: " + evenNumbers); // Creating a weak hash map from other hashmap WeakHashMap numbers = new WeakHashMap(evenNumbers); System.out.println("WeakHashMap: " + numbers); ) ) 

Rezultāts

 HashMap: (Divi = 2) WeakHashMap: (Divi = 2) 

WeakHashMap metodes

WeakHashMapKlase nodrošina metodes, kas ļauj mums, lai veiktu dažādas darbības kartē.

Ievietojiet elementus WeakHashMap

  • put() - ievieto kartē norādīto atslēgu / vērtību kartēšanu
  • putAll() - ievieto šajā kartē visus ierakstus no norādītās kartes
  • putIfAbsent() - ievieto kartē norādīto atslēgu / vērtību kartēšanu, ja norādītās atslēgas kartē nav

Piemēram,

 import java.util.WeakHashMap; class Main ( public static void main(String() args) ( // Creating WeakHashMap of even numbers WeakHashMap evenNumbers = new WeakHashMap(); String two = new String("Two"); Integer twoValue = 2; // Using put() evenNumbers.put(two, twoValue); String four = new String("Four"); Integer fourValue = 4; // Using putIfAbsent() evenNumbers.putIfAbsent(four, fourValue); System.out.println("WeakHashMap of even numbers: " + evenNumbers); //Creating WeakHashMap of numbers WeakHashMap numbers = new WeakHashMap(); String one = new String("One"); Integer oneValue = 1; numbers.put(one, oneValue); // Using putAll() numbers.putAll(evenNumbers); System.out.println("WeakHashMap of numbers: " + numbers); ) ) 

Rezultāts

 Pāra skaitļu WeakHashMap: (četri = 4, divi = 2) WeakHashMapu skaitļi: (divi = 2, četri = 4, viens = 1) 

Piekļūstiet WeakHashMap elementiem

1. Using entrySet(), keySet() and values()

  • entrySet() - returns a set of all the key/value mapping of the map
  • keySet() - returns a set of all the keys of the map
  • values() - returns a set of all the values of the map

For example,

 import java.util.WeakHashMap; class Main ( public static void main(String() args) ( // Creating WeakHashMap of even numbers WeakHashMap numbers = new WeakHashMap(); String one = new String("One"); Integer oneValue = 1; numbers.put(one, oneValue); String two = new String("Two"); Integer twoValue = 2; numbers.put(two, twoValue); System.out.println("WeakHashMap: " + numbers); // Using entrySet() System.out.println("Key/Value mappings: " + numbers.entrySet()); // Using keySet() System.out.println("Keys: " + numbers.keySet()); // Using values() System.out.println("Values: " + numbers.values()); ) ) 

Output

 WeakHashMap: (Two=2, One=1) Key/Value mappings: (Two=2, One=1) Keys: (Two, One) Values: (1, 2) 

2. Using get() and getOrDefault()

  • get() - Returns the value associated with the specified key. Returns null if the key is not found.
  • getOrDefault() - Returns the value associated with the specified key. Returns the specified default value if the key is not found.

For example,

 import java.util.WeakHashMap; class Main ( public static void main(String() args) ( // Creating WeakHashMap of even numbers WeakHashMap numbers = new WeakHashMap(); String one = new String("One"); Integer oneValue = 1; numbers.put(one, oneValue); String two = new String("Two"); Integer twoValue = 2; numbers.put(two, twoValue); System.out.println("WeakHashMap: " + numbers); // Using get() int value1 = numbers.get("Two"); System.out.println("Using get(): " + value1); // Using getOrDefault() int value2 = numbers.getOrDefault("Four", 4); System.out.println("Using getOrDefault(): " + value2); ) ) 

Output

 WeakHashMap: (Two=2, One=1) Using get(): 2 Using getOrDefault(): 4 

Remove WeakHashMap 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.WeakHashMap; class Main ( public static void main(String() args) ( // Creating WeakHashMap of even numbers WeakHashMap numbers = new WeakHashMap(); String one = new String("One"); Integer oneValue = 1; numbers.put(one, oneValue); String two = new String("Two"); Integer twoValue = 2; numbers.put(two, twoValue); System.out.println("WeakHashMap: " + numbers); // Using remove() with single parameter int value = numbers.remove("Two"); System.out.println("Removed value: " + value); // Using remove() with 2 parameters boolean result = numbers.remove("One", 3); System.out.println("Is the entry (One=3) removed? " + result); System.out.println("Updated WeakHashMap: " + numbers); ) ) 

Output

WeakHashMap: (Divi = 2, Viens = 1) Noņemtā vērtība: 2 Vai ieraksts (Viens = 3) ir noņemts? Nepatiesa atjaunināta WeakHashMap: (viens = 1)

Citas WeakHashMap metodes

Metode Apraksts
clear() Noņem visus ierakstus no kartes
containsKey() Pārbauda, ​​vai kartē ir norādīta atslēga, un atgriež Būla vērtību
containsValue() Pārbauda, ​​vai kartē ir norādītā vērtība, un atgriež Būla vērtību
size() Atgriež kartes lielumu
isEmpty() Pārbauda, ​​vai karte ir tukša, un atgriež Būla vērtību

Lai uzzinātu vairāk, apmeklējiet Java WeakHashMap (oficiālā Java dokumentācija).

Interesanti raksti...