CoolFace
Apppublic

KaiquanMah/TurkuBasicOOPinJava

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
1In addition to lists (and tables), a very useful data structure is a HASHMAP. 2It closely resembles Python's scatter table (aka DICTIONARY), 3the idea being that each element has a KEY and a VALUE - the value is determined by the key.4 5The elements are in NO particular ORDER - there is no question of first, second or last element.6Instead, we have a set of elements. 7You can see this when you print the scatterplot - the elements can be PRINTED in ANY ORDER.8 9 10 11Let's first look at an example of a hashmap initialization and usage:12 13import java.util.HashMap;14 15public class Example {16    public static void main(String[] args){17        HashMap<String, Integer> heights = new HashMap<>();18        heights.put("Jack", 172);19        heights.put("Jane", 169);20        heights.put("Kim", 181);21        heights.put("Karl", 158);22 23        System.out.println(heights);24    }25}26 27 28Program outputs:29{Kim=181, Jack=172, Jane=169, Karl=158}30 31 32 33 34==========================================================35 36 37 38In the example, you will notice that 2 generic type specifications are given when defining a hashmap: KEY TYPE and VALUE TYPE. 39 40A new element can be inserted using the PUT method. 41The method takes as parameters the key and the value. 42If the key is NOT FOUND in the table, a new element is INSERTed. 43If the key already EXISTS, the value of the element is REPLACED.44 45 46Similarly, the GET method can be used to return a value from a hashmap: the method takes the key as a parameter.47 48 49HashMap<String, Integer> heights = new HashMap<>();50heights.put("Jane", 169);51heights.put("Karl", 158);52 53System.out.println(heights.get("Karl"));54System.out.println(heights.get("Jane"));55 56 57// Karl grows58heights.put("Karl", 161); //UPDATE59System.out.println(heights.get("Karl"));60 61 62 63Program outputs:64158651696616167 68 69 70 71==========================================================72 73 74 75 76If the get method does not find a value for the given key in the hashmap, it returns null, which is an empty value.77 78 79 80 81The containsKey method can be used to test whether the given key can be found in the hashmap:82 83HashMap<Integer, Double> squares = new HashMap<>();84squares.put(9, 3.0);85squares.put(4, 2.0);86squares.put(16, 4.0);87 88 89 90System.out.println(squares.get(9));91System.out.println(squares.get(4));92// this is not found93System.out.println(squares.get(10)); //null94 95System.out.println(squares.containsKey(16)); //true96System.out.println(squares.containsKey(15)); //false97 98 99Program outputs:1003.01012.0102null103true104false105 106 107 108 109 110==========================================================111 112 113 114Although a hashmap is primarily intended for situations where you know the key and can retrieve a value from it, 115it is sometimes useful to iterate through all the elements of the table. 116 117This is easily done with the keySet method:118 119 120HashMap<Integer, Double> squares = new HashMap<>();121squares.put(9, 3.0);122squares.put(4, 2.0);123squares.put(16, 4.0);124squares.put(25, 5.0);125 126for (int key : squares.keySet()) {127    System.out.println(key + ": " + squares.get(key));128}129 130Program outputs:13116: 4.01324: 2.01339: 3.013425: 5.0135 136 137 138 139 140 141 142