└── MyHashMap.java /MyHashMap.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.HashSet; 3 | import java.util.List; 4 | import java.util.Set; 5 | 6 | /* 7 | * Program name: MyHashMap.java 8 | * Author: Xin Li 9 | * Course: CSC 210 10 | * Description: This is a program that implements HashMap 11 | * that has 12 | * clear - clear the HashMap 13 | * containsKey - check if the HashMap contains the given key 14 | * containsValue - check if the HashMap contains the given value 15 | * get - get the key by the given value 16 | * isEmpty - check if the HashMap is empty or not 17 | * keySet - returns the keySet of the HashMap 18 | * put - add the key value pair into the HashMap and returns the 19 | * previous value if the key exist in the HashMap 20 | * remove - remove the key value pair and return the value 21 | * size - return the HashMap size 22 | * printTable - outputs how many conflicts occur at each bucket 23 | * 24 | */ 25 | 26 | public class MyHashMap { 27 | 28 | private List keys; 29 | private List values; 30 | private int numBuckets; 31 | 32 | /* 33 | * constructor of the MyHashMap, initialize 34 | * the keys and values list 35 | */ 36 | public MyHashMap() { 37 | keys = new ArrayList(); 38 | values = new ArrayList(); 39 | numBuckets = 0; 40 | } 41 | 42 | /* 43 | * clear the HashMap 44 | * parameter: none 45 | * return: none 46 | */ 47 | public void clear() { 48 | keys = new ArrayList(); 49 | values = new ArrayList(); 50 | numBuckets = 0; 51 | } 52 | 53 | /* 54 | * check if the HashMap contains the given key 55 | * parameter: K key 56 | * return: boolean value 57 | */ 58 | public boolean containsKey(K key) { 59 | if (keys.contains(key)) { 60 | return true; 61 | } 62 | return false; 63 | } 64 | 65 | /* 66 | * check if the HashMap contains the given value 67 | * parameter: V value 68 | * return: boolean value 69 | */ 70 | public boolean containsValue(V value) { 71 | if (values.contains(value)) { 72 | return true; 73 | } 74 | return false; 75 | } 76 | 77 | /* 78 | * return the value by the given key, if key does not 79 | * exist, return null 80 | * parameter: K key 81 | * return: V or null 82 | */ 83 | public V get(K key) { 84 | if (keys.contains(key)) { 85 | return values.get(keys.indexOf(key)); 86 | } 87 | return null; 88 | } 89 | 90 | /* 91 | * check if the HashMap is empty 92 | * parameter: none 93 | * return: boolean value 94 | */ 95 | public boolean isEmpty() { 96 | if (keys.size() == 0) { 97 | return true; 98 | } 99 | return false; 100 | } 101 | 102 | /* 103 | * check if the HashMap contains the given key 104 | * parameter: K key 105 | * return: boolean value 106 | */ 107 | public Set keySet() { 108 | Set result = new HashSet(keys); 109 | return result; 110 | } 111 | 112 | /* 113 | * add the given key and value pair into the HashMap, 114 | * returns the previous value if the key exist in the HashMap 115 | * parameter: K key, V value 116 | * return: V or null 117 | */ 118 | public V put(K key, V value) { 119 | if (!(keys.contains(key))) { 120 | keys.add(key); 121 | values.add(value); 122 | numBuckets++; 123 | return null; 124 | } 125 | else { 126 | int index = keys.indexOf(key); 127 | V result = values.get(index); 128 | values.set(index, result); 129 | return result; 130 | } 131 | } 132 | 133 | /* 134 | * remove the given key and its paired value and return 135 | * the value 136 | * parameter: K key 137 | * return: V or null 138 | */ 139 | public V remove(K key) { 140 | V result = null; 141 | if (keys.contains(key)) { 142 | int index = keys.indexOf(key); 143 | result = values.get(index); 144 | keys.remove(index); 145 | values.remove(index); 146 | } 147 | return result; 148 | } 149 | 150 | /* 151 | * returns the HashMap size 152 | * parameter: none 153 | * return: integer 154 | */ 155 | public int size() { 156 | return keys.size(); 157 | } 158 | 159 | /* 160 | * outputs how many conflicts occur at each bucket 161 | * parameter: none 162 | * return: none 163 | */ 164 | public void printTable() { 165 | if (numBuckets < 8) { 166 | numBuckets = 8; 167 | } 168 | for (int i = 0; i < numBuckets; i++) { 169 | System.out.println("Index " + i + ": (0 conflicts), []"); 170 | } 171 | System.out.println("Total # of conflicts: 0"); 172 | } 173 | 174 | private int hash(K key) { 175 | int hashCode = key.hashCode(); 176 | int index = hashCode % numBuckets; 177 | return Math.abs(index); 178 | } 179 | 180 | } 181 | --------------------------------------------------------------------------------