You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -391,7 +391,7 @@ Olemme tähän mennessä käyttäneet HashMapin avaimina ainoastaan String- ja I
391
391
392
392
In addition to `equals`, the `hashCode` method can also be used for **approximate comparison of objects**. The method creates from the object a "hash code", i.e, a number, that tells a bit about the object's content. If two objects have the same hash value, they may be equal. On the other hand, if two objects have different hash values, they are certainly unequal.
393
393
394
-
Hash codes are used in HashMaps, for instance. HashMap's internal behavior is based on the fact that key-value pairs are stored in an array of lists based on the key's hash value. Each array index points to a list. The hash value identifies the array index, whereby the list located at the array index is traversed. The value associated with the key will be returned if, and only if, the exact same value is found in the list (equality comparison is done using the equals method). This way, the search only needs to consider a fraction of the keys stored in the hash table.
394
+
Hash codes are used in HashMaps, for instance. HashMap's internal behavior is based on the fact that key-value pairs are stored in an array of lists based on the key's hash value. Each array index points to a list. The hash value identifies the array index, whereby the list located at the array index is traversed. The value associated with the key will be returned if, and only if, the exact same value is found in the list (equality comparison is done using the equals method). This way, the search only needs to consider a fraction of the keys stored in the hash map.
395
395
396
396
So far, we've only used String and Integer-type objects as HashMap keys, which have conveniently had ready-made `hashCode` methods implemented. Let's create an example where this is not the case: we'll continue with the books and keep track of the books that are on loan. We'll implement the book keeping with a HashMap. The key is the book and the value attached to the book is a string that tells the borrower's name:
397
397
@@ -433,7 +433,7 @@ Jotta HashMap toimisi haluamallamme tavalla, eli palauttaisi lainaajan kun avaim
433
433
434
434
Olemme aiemmin käyttäneet `String`-olioita menestyksekkäästi HashMapin avaimena, joten voimme päätellä että `String`-luokassa on oma järkevästi toimiva `hashCode`-toteutus. _Delegoidaan_, eli siirretään laskemisvastuu `String`-oliolle. -->
435
435
436
-
We find the borrower when searching for the same object that was given as a key to the hash table's `put` method. However, when searching by the exact same book but with a different object,a borrwer isn't found, and we get the _null_ reference instead. The reason lies in the default implementation of the `hashCode` method in the `Object` class. The default implementation creates a `hashCode` value based on the object's reference, which means that books having the same content that are nonetheless different objects get different results from the hashCode method. As such, the object is not being searched for in the right place.
436
+
We find the borrower when searching for the same object that was given as a key to the hash map's `put` method. However, when searching by the exact same book but with a different object,a borrwer isn't found, and we get the _null_ reference instead. The reason lies in the default implementation of the `hashCode` method in the `Object` class. The default implementation creates a `hashCode` value based on the object's reference, which means that books having the same content that are nonetheless different objects get different results from the hashCode method. As such, the object is not being searched for in the right place.
437
437
438
438
For the HashMap to work in thw way we want it to, that is, to return the borrower when given an object with the correct _content_ (not necessarily the same object as the original key), the class that's the key must overwrite the `hashCode` method in addition to the `equals` method. The method must be overwritten so that it gives the same numerical result for all objects with the same content. Also, some objects with different contents may get the same result from the hashCode method. However, with the HashMap's performance in mind, it is essential that objects with different contents get the same hash value as rarely as possible.
439
439
@@ -499,7 +499,7 @@ public int hashCode() {
499
499
500
500
<!-- Nyt kirjan käyttö hajautustaulun avaimena on mahdollista. Samalla aiemmin kohtaamamme ongelma ratkeaa ja kirjojen lainaajat löytyvät: -->
501
501
502
-
It's now possible to use the book as the hash table's key. This way the problem we faced earlier gets solved and the book borrowers are found:
502
+
It's now possible to use the book as the hash map's key. This way the problem we faced earlier gets solved and the book borrowers are found:
503
503
504
504
<!-- ```java
505
505
HashMap<Kirja, String> lainaajat = new HashMap<>();
- You know how to use a list as a hash table's value
16
-
- You know how to categorize data using a hash table
15
+
- You know how to use a list as a hash map's value
16
+
- You know how to categorize data using a hash map
17
17
18
18
</text-box>
19
19
20
20
<!-- Hajautustaulu sisältää korkeintaan yhden arvon yhtä avainta kohti. Seuraavassa esimerkissä tallennamme henkilöiden puhelinnumeroita hajautustauluun. -->
21
-
A hash table has at most one value per each key. In the following example, we store the phone numbers of people into the hash table.
21
+
A hash map has at most one value per each key. In the following example, we store the phone numbers of people into the hash map.
22
22
23
23
24
24
<!-- ```java
@@ -61,7 +61,7 @@ Pekka's number: 09-111333
61
61
Koska hajautustaulun avaimet ja arvot voivat olla mitä tahansa muuttujia, myös listojen käyttäminen hajautustaulun arvona on mahdollista. Useamman arvon lisääminen yhdelle avaimelle onnistuu liittämällä avaimeen lista. Muutetaan puhelinnumeroiden tallennustapaa seuraavasti: -->
62
62
What if we wanted to assign multiple values to a single key, such as multiple phone numbers for a given person?
63
63
64
-
Since keys and values in a hash table can be any variable, it is also possible to use lists as values in a hash table. You can add more values to a single key by attaching a list to the key. Let's change the way the numbers are stored in the following way:
64
+
Since keys and values in a hash map can be any variable, it is also possible to use lists as values in a hash map. You can add more values to a single key by attaching a list to the key. Let's change the way the numbers are stored in the following way:
65
65
66
66
<!-- ```java
67
67
HashMap<String, ArrayList<String>> puhelinnumerot = new HashMap<>();
@@ -71,7 +71,7 @@ HashMap<String, ArrayList<String>> phoneNumbers = new HashMap<>();
71
71
```
72
72
73
73
<!-- Nyt hajautustaulussa on jokaiseen avaimeen liitettynä lista. Vaikka new-komento luo hajautustaulun, ei hajautustaulu sisällä alussa yhtäkään listaa. Ne on luotava tarvittaessa erikseen. -->
74
-
Each key of the hash table now has a list attached to it. Although the new command creates a hash table, the hash table initially does not contain a single list. They need to be created separately as needed.
74
+
Each key of the hash map now has a list attached to it. Although the new command creates a hash map, the hash map initially does not contain a single list. They need to be created separately as needed.
75
75
76
76
<!-- ```java
77
77
HashMap<String, ArrayList<String>> puhelinnumerot = new HashMap<>();
<!-- Määrittelimme muuttujan puhelinnumero tyypiksi `HashMap<String, ArrayList<String>>`. Tämä tarkoittaa hajautustaulua, joka käyttää avaimena merkkijonoa ja arvona merkkijonoja sisältävää listaa. Hajautustauluun lisättävät arvot ovat siis konkreettisia listoja. -->
115
-
We define the type of the phone number as `HashMap<String, ArrayList<String>>`. This refers to a hash table that uses a string as a key and a list containing strings as its value. As such, the values added to the hash table are concrete lists.
115
+
We define the type of the phone number as `HashMap<String, ArrayList<String>>`. This refers to a hash map that uses a string as a key and a list containing strings as its value. As such, the values added to the hash map are concrete lists.
title: 'Fast data fetching and grouping information'
4
-
hidden: true
4
+
hidden: false
5
5
---
6
6
7
7
<!--
@@ -22,6 +22,4 @@ Vastaa vielä lopuksi seuraavaan kahdeksannen osan osaamistavoitteita tarkastele
22
22
23
23
The eighth part also started the second part of this course. If you just jumped in, welcome aboard! Finally, please take a moment to answer a self-reflective survey on the learning goals of the eighth part.
0 commit comments