Cara menggunakan hashmap di javascript w3schools

HashMap memperluas kelas AbstractMap dan mengimplementasikan antarmuka Peta. Ini berisi elemen dalam bentuk pasangan kunci-nilai. Itu tidak mempertahankan urutan apa pun untuk elemen-elemennya. Kunci duplikat tidak diperbolehkan. HashMap hanya dapat memiliki satu kunci nol dan beberapa nilai nol

  • Tidak ada urutan yang dipertahankan oleh kelas HashMap
  • Itu tidak disinkronkan
  • Kapasitas default awalnya adalah 16
  • Faktor bebannya adalah 0. 75

Deklarasi kelas HashMap

public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable

Di mana

  • K. Jenis kunci yang dikelola oleh HashMap
  • V. Jenis nilai yang dipetakan

Konstruktor kelas HashMap

S.No.ConstructorDescription1HashMap()It will create a default HashMap.2HashMap(Map m)It will create and initialize the hash map by using the elements of the given Map object m.3HashMap(int capacity)It will create and initialize the capacity of the hash map to the given integer value, capacity.4HashMap(int capacity, float loadFactor)It will create and initialize both the capacity and load factor of the hash map by using its arguments.

Metode kelas HashMap

S.No.MethodDescription1void clear()It will eliminate or delete all of the mappings from this map.2boolean isEmpty()It will return true if this map contains no key-value mappings.3Object clone()It will return a shallow copy of this HashMap instance: the keys and values themselves are not cloned.4Set entrySet()It will return a collection view of the mappings contained in this map.5Set keySet()It will return a set view of the keys contained in this map.6V put(Object key, Object value)It will add an entry to the map.7void putAll(Map map)It will add the specified map to the map.8V putIfAbsent(K key, V value)It will add the specified value with the specified key in the map only if it is not already specified.9V remove(Object key)It will delete or eliminate an entry for the specified key.10boolean remove(Object key, Object value)It will eliminate or delete the specified values with the associated specified keys from the map.111V compute(K key, BiFunction remappingFunction)It will compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).12V computeIfAbsent(K key, Function mappingFunction)It will compute its value using the given mapping function, if the specified key is not already associated with a value (or is mapped to null), and enters it into this map unless null.13V computeIfPresent(K key, BiFunction remappingFunction)To compute a new mapping given the key and its current mapped value if the value for the specified key is present and non-null.14boolean containsValue(Object value)It will return true if some value equal to the value exists within the map, else return false.15boolean containsKey(Object key)It will return true if some key equal to the key exists within the map, else return false.16boolean equals(Object o)It will compare the specified Object with the Map.17void forEach(BiConsumer action)It will perform the given action for each entry in the map until all entries have been processed or the action throws an exception.18V get(Object key)It will return the object that contains the value associated with the key.19V getOrDefault(Object key, V defaultValue)It will return the value to which the specified key is mapped, or defaultValue if the map contains no mapping for the key.20boolean isEmpty()It will return true if the map is empty; returns false if it contains at least one key.21V merge(K key, V value, BiFunction remappingFunction)It will associate the specified key with the given non-null value if it is not already associated with a value or is associated with null.22V replace(K key, V value)It will replace the specified value for a specified key.23boolean replace(K key, V oldValue, V newValue)It will replace the old value with the new value for a specified key.24void replaceAll(BiFunction function)It will replace each entry’s value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.25Collection values()It will return a collection view of the values contained in the map.26int size()It will return the number of entries on the map.

Contoh HashMap

Tes HashMap. Jawa

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
 
/**
 * This class is used to show the HashMap functionality.
 * @author w3spoint
 */
public class HashMapTest {
	public static void main(String args[]){
		//Create HashMap object.
		Map hashMap = new HashMap();
 
		//Add objects to the HashSet.
		hashMap.put(4, "Roxy");
		hashMap.put(2, "Sunil");
		hashMap.put(5, "Sandy");
		hashMap.put(1, "Munish");
		hashMap.put(3, "Pardeep");
 
		//Print the HashMap object.
		System.out.println("HashMap elements:");
		System.out.println(hashMap);
 
		//Get iterator
		Set set=hashMap.entrySet();  
		Iterator iterator=set.iterator();  
 
		//Print the HashMap elements using iterator.
		System.out.println("HashMap elements using iterator:");
		while(iterator.hasNext()){
		   Map.Entry mapEntry=(Map.Entry)iterator.next();  
		   System.out.println("Key: " + mapEntry.getKey() + ", " +
		   		"Value: " + mapEntry.getValue());  
		}  
	}
}
_

impor jawa. utilitas. HashMap; . utilitas. Iterator; . utilitas. Peta; . utilitas. Set; . * @author w3spoint */ public class HashMapTest { public static void main(String args[]){ //Buat objek HashMap. Peta hashMap = HashMap baru(); . hashMap. masukkan(4, "Roxy"); . masukkan(2, "Sunil"); . masukkan(5, "Berpasir"); . put(1, "Munish"); . masukkan(3, "Pardeep"); . Sistem. keluar. println("Elemen HashMap. "); Sistem. keluar. println(hashMap); . entriSet(); . iterator(); . Sistem. keluar. println("Elemen HashMap menggunakan iterator. "); while(iterator. hasNext()){ Peta. Entri mapEntry=(Peta. masuk) iterator. Berikutnya(); . keluar. println("Kunci. " + entri peta. getKey() + ", " + "Nilai. " + entri peta. getValue());

Keluaran

HashMap elements:
{1=Munish, 2=Sunil, 3=Pardeep, 4=Roxy, 5=Sandy}
HashMap elements using iterator:
Key: 1, Value: Munish
Key: 2, Value: Sunil
Key: 3, Value: Pardeep
Key: 4, Value: Roxy
Key: 5, Value: Sandy

Elemen HashMap. {1=Munish, 2=Sunil, 3=Pardeep, 4=Roxy, 5=Sandy} elemen HashMap menggunakan iterator. Kunci. 1, Nilai. Kunci Munish. 2, Nilai. Sunil Key. 3, Nilai. Kunci Pardeep. 4, Nilai. Kunci Roxy. 5, Nilai. berpasir

Contoh. Berbagai cara untuk memasukkan elemen

import java.util.*;  
public class HashMapExample{  
 public static void main(String args[]){  
   HashMap<Integer,String> hm=new HashMap<Integer,String>();    
    System.out.println("The initial list of elements:: "+hm);  
      hm.put(200,"A");    
      hm.put(201,"B");    
      hm.put(202,"C");   
 
      System.out.println("After invoking the put() method:: ");  
      for(Map.Entry m:hm.entrySet()){    
       System.out.println(m.getKey()+" "+m.getValue());    
      }  
 
      hm.putIfAbsent(203, "D");  
      System.out.println("After invoking the putIfAbsent() method:: ");  
      for(Map.Entry m:hm.entrySet()){    
           System.out.println(m.getKey()+" "+m.getValue());    
          }  
      HashMap<Integer,String> map=new HashMap<Integer,String>();  
      map.put(204,"E");  
      map.putAll(hm);  
      System.out.println("After invoking the putAll() method:: ");  
      for(Map.Entry m:map.entrySet()){    
           System.out.println(m.getKey()+" "+m.getValue());    
          }  
 }  
}

impor jawa. utilitas. *; . masukkan(200,"A"); . masukkan(201,"B"); . masukkan(202,"C"); . keluar. println("Daftar elemen awal. "); untuk(Peta. masuk m. Hmm. entrySet()) { Sistem. keluar. println(m. getKey()+" "+m. getValue()); . keluar. println("Daftar elemen yang diperbarui. "); Hmm. ganti(202, "c"); . masuk m. Hmm. entrySet()) { Sistem. keluar. println(m. getKey()+" "+m. getValue()); . keluar. println("Daftar elemen yang diperbarui. "); Hmm. ganti(201, "B", "b"); . masuk m. Hmm. entrySet()) { Sistem. keluar. println(m. getKey()+" "+m. getValue()); . keluar. println("Daftar elemen yang diperbarui. "); Hmm. replaceAll((k,v) -> "a"); . masuk m. Hmm. entrySet()) { Sistem. keluar. println(m. getKey()+" "+m. getValue());

Bagaimana cara menggunakan hashmap di JavaScript?

Itu. metode size() untuk objek hashmap JS mirip dengan array. length() dan mengembalikan jumlah elemen di hashmap. .
hashmap. .
hashmap. .
hashmap.has() checks to see if the hashmap contains the key that is passed as an argument..
hashmap. .
hashmap. .
hashmap

Bagaimana cara membuat hashmap di JavaScript?

Anda dapat menerapkan Tabel Hash di JavaScript dalam tiga langkah. .
Buat kelas HashTable dengan properti awal tabel dan ukuran
Tambahkan fungsi hash() untuk mengubah kunci menjadi indeks
Tambahkan metode set() dan get() untuk menambahkan dan mengambil pasangan kunci/nilai dari tabel

Apakah kita memiliki hashmap di JavaScript?

HashMap diimplementasikan oleh ES6 dalam JavaScript. Ini memiliki pasangan nilai kunci dan di keduanya, kita dapat menggunakan tipe data non-primitif untuk menyimpan data. Peta akan mengembalikan kunci, pasangan nilai dalam urutan yang sama dengan yang kami masukkan. Peta juga memiliki properti dan metode seperti ukuran peta dan lainnya

Bagaimana cara mendapatkan kunci dari hashmap di JavaScript?

Untuk mendapatkan kunci objek Map, Anda menggunakan metode keys() . keys() mengembalikan objek iterator baru yang berisi kunci elemen di peta.