import java.util.ArrayList; public class HashMapLinearProbing { private ArrayList > hashArray; //storing the data private int size; //need to keep track of the size private KeyValue nonData; public...

You are required to implement a map between “name” and its corresponding “content”, with a database of size 10.2.1 Using Screenshot to demonstrate the major functions implemented.2.2 Write a simple user manual to show how to run your code.Linear Probing (Partial Marks)Quadratic Probing (Full Marks)


import java.util.ArrayList; public class HashMapLinearProbing { private ArrayList<>> hashArray; //storing the data private int size; //need to keep track of the size private KeyValue nonData; public HashMapLinearProbing(int size) { this.size = size; //size of the hashmap hashArray = new ArrayList<>>(size); //Creating a new hashArray with the data type of T <>> for(int i = 0; i < hasharray.size();="" i++)="" hasharray.add(new=""><>(-1)); nonData = new KeyValue(-1); //each key will have a -1 which will be an empty spot } //Nondata is a way to say to the key to not be a key anymore public int hash(int key) { return key % size; //This will generate our hash } //// Add method public void add(KeyValue data) { int key = data.getKey(); //Retrieving / getting the key / get data int hash = hash(key); //Hashing the key while(hashArray.get(hash) != null //Searching through array && hashArray.get(hash).getKey() != -1) //While has = null and hasharray key is not equal to -1 { ++hash; //Finding an empty element hash %= size; } hashArray.set(hash, data); //sets the data to that hash } //// Find Method public KeyValue find(int key) { int hash = hash(key); //go through array to find element that we looking for while(hashArray.get(hash) != null) { if(hashArray.get(hash).getKey() == key) return hashArray.get(hash); ++hash; //Keeps searching until finds empty slot hash %= size; } return null; //If it can't find it at all } //// Delete method public KeyValue delete(int key) { int hash = hash(key); //go through array to find element that we looking for while(hashArray.get(hash) != null) { if(hashArray.get(hash).getKey() == key) { KeyValue temp = hashArray.get(hash); //Temporary key value hashArray.set(hash, nonData); //Replacing hash with empty NonData return temp; } ++hash; //Keeps searching until finds empty slot hash %= size; } return null; //If it can't find it at all } //// Display (helper method, not essensial but can help with debugging and making sure it works) public void Display() { System.out.println("Map: "); for(int i=0; i < size;="" i++)="" {="" if(hasharray.get(i).getvalue()="" !="null)" {="" system.out.println(hasharray.get(i).getkey()="" +="" ",="" ");="" system.out.println(hasharray.get(i).getvalue()="" +="" ",="" ");="" }="" else{="" system.out.println("**");="" }="" }="" system.out.println("");="" }="" }="" import="" java.util.hashmap;="" hashmap="" utility="" public="" class="" javahashmap="" {="" public="" static="" void="" main(string[]="" args)="" {=""> map = new HashMap<>(); //First string is key value, second is value type map.put("George", "123"); //Imputing data into the HashMap map.put("John", "45678"); System.out.println(map.get("George")); //Searching the hashmap to get the value type } } public class KeyValue { private int key; private T value; public KeyValue(int key) { this.key = key; } public KeyValue(int key, T value) { this.key = key; this.value = value; } public int getKey() { return key; } public T getValue() { return value; } public void setKey(int key) { this.key = key; } public void setValue(T value) { this.value = value; } }
Aug 22, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here