1. Gambaran keseluruhan
Dalam artikel ini, kita akan melihat cara menggunakan HashMap di Java, dan kita akan melihat bagaimana ia berfungsi secara dalaman.
Kelas yang sangat serupa dengan HashMap ialah Hashtable . Sila rujuk beberapa artikel kami yang lain untuk mengetahui lebih lanjut mengenai kelas java.util.Hashtable itu sendiri dan perbezaan antara HashMap dan Hashtable .
2. Penggunaan Asas
Mari lihat dahulu maksudnya bahawa HashMap adalah peta. Peta adalah pemetaan nilai-kunci, yang bermaksud bahawa setiap kunci dipetakan dengan tepat satu nilai dan kita dapat menggunakan kunci untuk mengambil nilai yang sesuai dari peta.
Seseorang mungkin bertanya mengapa tidak hanya menambahkan nilai ke dalam senarai. Mengapa kita memerlukan HashMap ? Sebab yang mudah adalah prestasi. Sekiranya kita ingin mencari elemen tertentu dalam senarai, kerumitan waktu adalah O (n) dan jika senarai itu disusun, O (log n) akan menggunakan, misalnya, carian binari.
Kelebihan HashMap adalah bahawa kerumitan masa untuk memasukkan dan mengambil nilai adalah O (1) secara purata. Kami akan melihat bagaimana perkara itu dapat dicapai kemudian. Mari lihat dahulu cara menggunakan HashMap .
2.1. Persediaan
Mari buat kelas sederhana yang akan kami gunakan sepanjang artikel:
public class Product { private String name; private String description; private List tags; // standard getters/setters/constructors public Product addTagsOfOtherProdcut(Product product) { this.tags.addAll(product.getTags()); return this; } }
2.2. Letak
Kita sekarang boleh membuat HashMap dengan kunci jenis String dan elemen jenis Produk :
Map productsByName = new HashMap();
Dan tambahkan produk ke HashMap kami :
Product eBike = new Product("E-Bike", "A bike with a battery"); Product roadBike = new Product("Road bike", "A bike for competition"); productsByName.put(eBike.getName(), eBike); productsByName.put(roadBike.getName(), roadBike);
2.3. Dapatkan
Kami dapat memperoleh nilai dari peta dengan kuncinya:
Product nextPurchase = productsByName.get("E-Bike"); assertEquals("A bike with a battery", nextPurchase.getDescription());
Sekiranya kita berusaha mencari nilai untuk kunci yang tidak ada dalam peta, kita akan mendapat nilai nol :
Product nextPurchase = productsByName.get("Car"); assertNull(nextPurchase);
Dan jika kita memasukkan nilai kedua dengan kunci yang sama, kita hanya akan mendapat nilai yang terakhir dimasukkan untuk kunci itu:
Product newEBike = new Product("E-Bike", "A bike with a better battery"); productsByName.put(newEBike.getName(), newEBike); assertEquals("A bike with a better battery", productsByName.get("E-Bike").getDescription());
2.4. Null sebagai Kunci
HashMap juga membolehkan kita mempunyai nol sebagai kunci:
Product defaultProduct = new Product("Chocolate", "At least buy chocolate"); productsByName.put(null, defaultProduct); Product nextPurchase = productsByName.get(null); assertEquals("At least buy chocolate", nextPurchase.getDescription());
2.5. Nilai dengan Kekunci yang Sama
Selanjutnya, kita dapat memasukkan objek yang sama dua kali dengan kunci yang berbeza:
productsByName.put(defaultProduct.getName(), defaultProduct); assertSame(productsByName.get(null), productsByName.get("Chocolate"));
2.6. Keluarkan Nilai
Kita boleh membuang pemetaan nilai-kunci dari HashMap :
productsByName.remove("E-Bike"); assertNull(productsByName.get("E-Bike"));
2.7. Periksa Jika Kunci atau Nilai Ada di Peta
Untuk memeriksa sama ada kunci ada di peta, kita dapat menggunakan kaedah containKey () :
productsByName.containsKey("E-Bike");
Atau, untuk memeriksa apakah ada nilai dalam peta, kita boleh menggunakan kaedah includeValue () :
productsByName.containsValue(eBike);
Kedua-dua panggilan kaedah akan berlaku dalam contoh kami. Walaupun kelihatan sangat mirip, terdapat perbezaan penting dalam prestasi antara kedua-dua panggilan kaedah ini. Kerumitan untuk memeriksa sama ada kunci ada adalah O (1) , sementara kerumitan untuk memeriksa elemen adalah O (n), kerana perlu melingkari semua elemen dalam peta.
2.8. Pengulangan Melalui Peta Pegangan
Terdapat tiga cara asas untuk melakukan lelaran ke atas semua pasangan nilai-kunci dalam HashMap .
Kita boleh melakukan rutin ke atas semua kunci:
for(String key : productsByName.keySet()) { Product product = productsByName.get(key); }
Atau kita boleh mengulangi set semua entri:
for(Map.Entry entry : productsByName.entrySet()) { Product product = entry.getValue(); String key = entry.getKey(); //do something with the key and value }
Pada akhirnya, kita dapat mengulangi semua nilai:
List products = new ArrayList(productsByName.values());
3. Kunci
Kami boleh menggunakan kelas apa pun sebagai kunci dalam HashMap kami . Namun, agar peta berfungsi dengan baik, kita perlu menyediakan implementasi untuk sama dengan () dan hashCode (). Katakan kita ingin mempunyai peta dengan produk sebagai kunci dan harga sebagai nilainya:
HashMap priceByProduct = new HashMap(); priceByProduct.put(eBike, 900);
Mari kita laksanakan kaedah sama () dan hashCode () :
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Product product = (Product) o; return Objects.equals(name, product.name) && Objects.equals(description, product.description); } @Override public int hashCode() { return Objects.hash(name, description); }
Perhatikan bahawa hashCode () dan sama dengan () hanya perlu diganti untuk kelas yang ingin kita gunakan sebagai kunci peta, bukan untuk kelas yang hanya digunakan sebagai nilai dalam peta. Kami akan melihat mengapa ini perlu di bahagian 5 artikel ini.
4. Additional Methods as of Java 8
Java 8 added several functional-style methods to HashMap. In this section, we'll look at some of these methods.
For each method, we'll look at two examples. The first example shows how to use the new method, and the second example shows how to achieve the same in earlier versions of Java.
As these methods are quite straightforward, we won't look at more detailed examples.
4.1. forEach()
The forEach method is the functional-style way to iterate over all elements in the map:
productsByName.forEach( (key, product) -> { System.out.println("Key: " + key + " Product:" + product.getDescription()); //do something with the key and value });
Prior to Java 8:
for(Map.Entry entry : productsByName.entrySet()) { Product product = entry.getValue(); String key = entry.getKey(); //do something with the key and value }
Our article Guide to the Java 8 forEach covers the forEach loop in greater detail.
4.2. getOrDefault()
Using the getOrDefault() method, we can get a value from the map or return a default element in case there is no mapping for the given key:
Product chocolate = new Product("chocolate", "something sweet"); Product defaultProduct = productsByName.getOrDefault("horse carriage", chocolate); Product bike = productsByName.getOrDefault("E-Bike", chocolate);
Prior to Java 8:
Product bike2 = productsByName.containsKey("E-Bike") ? productsByName.get("E-Bike") : chocolate; Product defaultProduct2 = productsByName.containsKey("horse carriage") ? productsByName.get("horse carriage") : chocolate;
4.3. putIfAbsent()
With this method, we can add a new mapping, but only if there is not yet a mapping for the given key:
productsByName.putIfAbsent("E-Bike", chocolate);
Prior to Java 8:
if(productsByName.containsKey("E-Bike")) { productsByName.put("E-Bike", chocolate); }
Our article Merging Two Maps with Java 8 takes a closer look at this method.
4.4. merge()
And with merge(), we can modify the value for a given key if a mapping exists, or add a new value otherwise:
Product eBike2 = new Product("E-Bike", "A bike with a battery"); eBike2.getTags().add("sport"); productsByName.merge("E-Bike", eBike2, Product::addTagsOfOtherProdcut);
Prior to Java 8:
if(productsByName.containsKey("E-Bike")) { productsByName.get("E-Bike").addTagsOfOtherProdcut(eBike2); } else { productsByName.put("E-Bike", eBike2); }
4.5. compute()
With the compute() method, we can compute the value for a given key:
productsByName.compute("E-Bike", (k,v) -> { if(v != null) { return v.addTagsOfOtherProdcut(eBike2); } else { return eBike2; } });
Prior to Java 8:
if(productsByName.containsKey("E-Bike")) { productsByName.get("E-Bike").addTagsOfOtherProdcut(eBike2); } else { productsByName.put("E-Bike", eBike2); }
It's worth noting that the methods merge() and compute() are quite similar. The compute() method accepts two arguments: the key and a BiFunction for the remapping. And merge() accepts three parameters: the key, a default value to add to the map if the key doesn't exist yet, and a BiFunction for the remapping.
5. HashMap Internals
In this section, we'll look at how HashMap works internally and what are the benefits of using HashMap instead of a simple list, for example.
As we've seen, we can retrieve an element from a HashMap by its key. One approach would be to use a list, iterate over all elements, and return when we find an element for which the key matches. Both the time and space complexity of this approach would be O(n).
With HashMap, we can achieve an average time complexity of O(1) for the put and get operations and space complexity of O(n). Let's see how that works.
5.1. The Hash Code and Equals
Instead of iterating over all its elements, HashMap attempts to calculate the position of a value based on its key.
The naive approach would be to have a list that can contain as many elements as there are keys possible. As an example, let's say our key is a lower-case character. Then it's sufficient to have a list of size 26, and if we want to access the element with key ‘c', we'd know that it's the one at position 3, and we can retrieve it directly.
However, this approach would not be very effective if we have a much bigger keyspace. For example, let's say our key was an integer. In this case, the size of the list would have to be 2,147,483,647. In most cases, we would also have far fewer elements, so a big part of the allocated memory would remain unused.
HashMap stores elements in so-called buckets and the number of buckets is called capacity.
When we put a value in the map, the key's hashCode() method is used to determine the bucket in which the value will be stored.
To retrieve the value, HashMap calculates the bucket in the same way – using hashCode(). Then it iterates through the objects found in that bucket and use key's equals() method to find the exact match.
5.2. Keys' Immutability
In most cases, we should use immutable keys. Or at least, we must be aware of the consequences of using mutable keys.
Let's see what happens when our key changes after we used it to store a value in a map.
For this example, we'll create the MutableKey:
public class MutableKey { private String name; // standard constructor, getter and setter @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } MutableKey that = (MutableKey) o; return Objects.equals(name, that.name); } @Override public int hashCode() { return Objects.hash(name); } }
And here goes the test:
MutableKey key = new MutableKey("initial"); Map items = new HashMap(); items.put(key, "success"); key.setName("changed"); assertNull(items.get(key));
As we can see, we're no longer able to get the corresponding value once the key has changed, instead, null is returned. This is because HashMap is searching in the wrong bucket.
The above test case may be surprising if we don't have a good understanding of how HashMap works internally.
5.3. Collisions
For this to work correctly, equal keys must have the same hash, however, different keys can have the same hash. If two different keys have the same hash, the two values belonging to them will be stored in the same bucket. Inside a bucket, values are stored in a list and retrieved by looping over all elements. The cost of this is O(n).
As of Java 8 (see JEP 180), the data structure in which the values inside one bucket are stored is changed from a list to a balanced tree if a bucket contains 8 or more values, and it's changed back to a list if, at some point, only 6 values are left in the bucket. This improves the performance to be O(log n).
5.4. Capacity and Load Factor
To avoid having many buckets with multiple values, the capacity is doubled if 75% (the load factor) of the buckets become non-empty. The default value for the load factor is 75%, and the default initial capacity is 16. Both can be set in the constructor.
5.5. Summary of put and get Operations
Let's summarize how the put and get operations work.
When we add an element to the map,HashMap calculates the bucket. If the bucket already contains a value, the value is added to the list (or tree) belonging to that bucket. If the load factor becomes bigger than the maximum load factor of the map, the capacity is doubled.
When we want to get a value from the map,HashMap calculates the bucket and gets the value with the same key from the list (or tree).
6. Conclusion
Dalam artikel ini, kami melihat bagaimana menggunakan HashMap dan bagaimana ia berfungsi secara dalaman. Bersama dengan ArrayList , HashMap adalah salah satu struktur data yang paling kerap digunakan di Java, jadi sangat berguna untuk mengetahui bagaimana menggunakannya dan bagaimana ia berfungsi di bawah kendali. Artikel kami The HashMap Java Under the Hood merangkumi dalaman HashMap dengan lebih terperinci.
Seperti biasa, kod sumber lengkap boleh didapati di Github.