Spaces:
Running
Running
| In addition to lists (and tables), a very useful data structure is a HASHMAP. | |
| It closely resembles Python's scatter table (aka DICTIONARY), | |
| the idea being that each element has a KEY and a VALUE - the value is determined by the key. | |
| The elements are in NO particular ORDER - there is no question of first, second or last element. | |
| Instead, we have a set of elements. | |
| You can see this when you print the scatterplot - the elements can be PRINTED in ANY ORDER. | |
| Let's first look at an example of a hashmap initialization and usage: | |
| import java.util.HashMap; | |
| public class Example { | |
| public static void main(String[] args){ | |
| HashMap<String, Integer> heights = new HashMap<>(); | |
| heights.put("Jack", 172); | |
| heights.put("Jane", 169); | |
| heights.put("Kim", 181); | |
| heights.put("Karl", 158); | |
| System.out.println(heights); | |
| } | |
| } | |
| Program outputs: | |
| {Kim=181, Jack=172, Jane=169, Karl=158} | |
| ========================================================== | |
| In the example, you will notice that 2 generic type specifications are given when defining a hashmap: KEY TYPE and VALUE TYPE. | |
| A new element can be inserted using the PUT method. | |
| The method takes as parameters the key and the value. | |
| If the key is NOT FOUND in the table, a new element is INSERTed. | |
| If the key already EXISTS, the value of the element is REPLACED. | |
| Similarly, the GET method can be used to return a value from a hashmap: the method takes the key as a parameter. | |
| HashMap<String, Integer> heights = new HashMap<>(); | |
| heights.put("Jane", 169); | |
| heights.put("Karl", 158); | |
| System.out.println(heights.get("Karl")); | |
| System.out.println(heights.get("Jane")); | |
| // Karl grows | |
| heights.put("Karl", 161); //UPDATE | |
| System.out.println(heights.get("Karl")); | |
| Program outputs: | |
| 158 | |
| 169 | |
| 161 | |
| ========================================================== | |
| If the get method does not find a value for the given key in the hashmap, it returns null, which is an empty value. | |
| The containsKey method can be used to test whether the given key can be found in the hashmap: | |
| HashMap<Integer, Double> squares = new HashMap<>(); | |
| squares.put(9, 3.0); | |
| squares.put(4, 2.0); | |
| squares.put(16, 4.0); | |
| System.out.println(squares.get(9)); | |
| System.out.println(squares.get(4)); | |
| // this is not found | |
| System.out.println(squares.get(10)); //null | |
| System.out.println(squares.containsKey(16)); //true | |
| System.out.println(squares.containsKey(15)); //false | |
| Program outputs: | |
| 3.0 | |
| 2.0 | |
| null | |
| true | |
| false | |
| ========================================================== | |
| Although a hashmap is primarily intended for situations where you know the key and can retrieve a value from it, | |
| it is sometimes useful to iterate through all the elements of the table. | |
| This is easily done with the keySet method: | |
| HashMap<Integer, Double> squares = new HashMap<>(); | |
| squares.put(9, 3.0); | |
| squares.put(4, 2.0); | |
| squares.put(16, 4.0); | |
| squares.put(25, 5.0); | |
| for (int key : squares.keySet()) { | |
| System.out.println(key + ": " + squares.get(key)); | |
| } | |
| Program outputs: | |
| 16: 4.0 | |
| 4: 2.0 | |
| 9: 3.0 | |
| 25: 5.0 | |