Question

Exercise
Task is to implement a simple hash table with linked lists to deal with collisions. The given files are HashMapInterface, Pair and ResizingHashMap. More specifically the task is to implement HashMapInterface<K, V> in the class ResizingHashMap<K, V>. (Generic signatures are not necessary). It is NOT permitted to use packages java.util or java.lang.Math. ResizingHashMapPublicTest needs to pass 100%. There is another control test, which should be passed. The only file that needs to be worked on is ResizingHashMap.
You would only need to add and possibly correct some parts (see ResizingHashMap).

a) The constructor of class ResizingHashMap gets a value which determines the size of internal tables buckets. This is constant until method resize(). Initialize the hash table accordingly. (Hash table can solve collisions within a field of buckets through taking up an unlimited number of elements.

b) Method size() returns the number of elements currently saved in the hash table. Complexity of method size() needs to be O(1). Size should be used to administer the number of elements.

c) Method insertEntry() inserts a given key-value-entry at a given index into the hash table (without checking whether an entry with the same value exists). If the respective bucket is empty, the entry can be saved right away. If not, there is a collision and the new entry must be inserted at the end of the linked list. (Use of pointers with the field Pair.netPairInBucket).
This illustration depicts the administration which should be updated. Every entry in the array buckets corresponds with a bucket and points at the first entry (Key-entry-couple) of a linked list, at which further entries will be added In case of a collision.

d) Method getBucketIndex() calculates to a key-object the corresponding table index. Use HashCode()-Method. Hashcode needs to be portrayed on the index range of the table, which is saved in the field buckets. (Hashcode() can have negative values). Use modulo and additions to convert values into the right range (without loops and without java.lang.math).

e) Implement the method getEntry(). Returns entry with key-value-pair, which is identified via the key. Comparisons must happen with equals()-method. If there’s no matching pair, return null.

f) Method get() works like getEntry(), but does not return the entire entry(pair) but the value that is associated with a certain key.

g) method contains() checks if there is a value saved for a certain key.

h) Implement put-operation. If the given key is already associated with a value the saved value is updated and no new entry is created. In this case return false. Else a new entry with type pair needs to be created and then inserted (use insertEntry()). Then the method returns true.

i) Implement the functionality of the remove operation. Search the hash table for the entry that matches the given key (if necessary search buckets). If there’s no matching entry, return false. Otherwise remove the entry from the table (respectively from the linked list inside the bucket). Update the internal administration and return true.

j) Using method resize() the number of buckets in the hash map can be increased. Reinitialise the relevant buckets like the internal administration. Then add the already existing entries in the hash table again. (Important: add entries in ascending order of indices of the buckets-arrays. If there’s a list of entries, then this must be added again in the order of the list, before continuing with the next bucket).

k) read comments in the java file regarding the interface definition and make adjustment to prevent mistakes.

ResizingHashMap
public class ResizingHashMap<K,V> implements HashMapInterface<K,V> {

    private int size;

    public Pair<K, V>[] buckets;

    @SuppressWarnings("unchecked")
    public ResizingHashMap(int capacity) {
       //TODO a
    buckets = new Pair[capacity];
    }

    @Override
    public int size() {
       //TODO b
       return this.size;
    }

    @Override
    public void insertEntry(int idx, Pair<K, V> entry) {
       //TODO c
    if(entry.key == null){
    throw new IllegalArgumentException ("key must not be null");
    }
    else{
    if(buckets[idx] == null){
    buckets[idx] = entry;
    }
    else{
   
    }
    }
   
    }
   
   

    @Override
    public int getBucketIndex(K key) {
       //TODO d
    if (key == null){
    throw new IllegalArgumentException ("Not Allowed");
    }
    return ((key.hashCode() % buckets.length) + buckets.length) % buckets.length;
    }
      

    @Override
    public Pair<K, V> getEntry(K key) {
       //TODO e
    int a = getBucketIndex(key);
    if (key==null){
    throw new IllegalArgumentException ("No no no");
    }
    //while(!neuf.nextPairInBucket.equals(null)
    if (buckets[a] == null){
    return null;
   
    }else{
       return buckets[a];
    }
    }

    @Override
    public V get(K key) {
       //TODO f
    int c = getBucketIndex(key);
    V e = null
    if(buckets[c].equals(null)){
       return null;
       Pair<K, V> neuf = buckets[c];
       for(int i=0; neuf.nextPairInBucket.getKey().equals(key); i++){ //while(!neuf.nextPairInBucket.equals(null)
       if (neuf.key.equals(key)){
       neuf = neuf.nextPairInBucket;
       e=null;
       // while(buckets[c].nextPairInBucket.equals(null)){
       // if (buckets[c].nextPairInBucket.equals(null)){
      
       }
       }
    }   
    else{return buckets[c].value;
    //return new Pair<V, K>
    }
      
    }

    @Override
    public boolean contains(K key) {
       //TODO g
    Pair<K, V> d = getEntry(key);
    if (d== null){
       return false;
    }
    else{
    return true;
    }
    }

    @Override
    public boolean put(K key, V value) {
       //TODO h
    boolean l = contains(key);
    if (key == null){
    throw new IllegalArgumentException ("key must not be null");
    }
    if (l == true){
    getEntry(key).value = value;
    return false;
    }
    else{
    Pair<K, V> nuovo = new Pair<K, V>(key, value, null);
    System.out.println("Xx");
    insertEntry(getBucketIndex(key), nuovo);
    return true;
    }
    }

    @Override
    public boolean remove(K key) {
       //TODO i
       return false;
    }

    @Override
    @SuppressWarnings("unchecked")
    public void resize(int updatedCapacity) {
       //TODO j
    }
}




Interface
/**
* An interface for a simple hash map.
*
* @param <K>
*            The data type of a key.
* @param <V>
*            The data type of a value.
*/
public interface HashMapInterface<K,V> {

    /**
    * Return the number of key-value pairs stored in the map.
    * @return Number of stored entries.
    */
    public int size();

    /**
    * Inserts the given key-value mapping into the bucket with the given index
    * and increases the size(). Does not check whether a mapping for the key
    * of the given entry already exists.
    *
    * @param idx Index of the bucket for the given mapping.
    * @param entry The new mapping to insert at the given index.
    */
    public void insertEntry(int idx, Pair<K, V> entry);

    /**
    * Establish a key-value mapping. If a mapping with the same key already
    * exists, the value of the existing mapping is updated with the specified
    * value. Otherwise a new mapping is created and inserted.
    *
    * @param key
    *            The key of the mapping.
    * @param value
    *            The value of the mapping.
    * @return Returns true if a new mapping was added. Returns false if the mapping already exists or the
    *         value of an existing mapping was only updated.
    * @throws IllegalArgumentException
    *             Thrown if the specified key is null.
    */
    public boolean put(K key, V value);

    /**
    * Calculates the index of the bucket for the given key.
    *
    * @param key
    *          Potential key to store.
    * @return Returns the index of the bucket.
    * @throws IllegalArgumentException
    *             Thrown if the given key is null.
    */
    public int getBucketIndex(K key);

    /**
    * Retrieve the value associated with the specified key.
    *
    * @param key
    *            The key whose value is to be returned.
    * @return Returns the value associated with the specified key or null if no
    *         such key is stored in the map.
    * @throws IllegalArgumentException
    *             Thrown if the specified key is null.
    */
    public V get(K key);

    /**
    * Retrieve the key-value entry associated with the specified key.
    *
    * @param key
    *            The key whose key-value entry is to be returned.
    * @return Returns the key-value entry whose key matches the specified key.
    * @throws IllegalArgumentException
    *             Thrown if the specified key is null.
    */
    public Pair<K, V> getEntry(K key);

    /**
    * Remove a key-value mapping from the map.
    *
    * @param key
    *            The key of the key-value mapping to remove.
    * @return True if a mapping with the specified key was found and removed.
    *         False if the map did not contain a mapping with the specified
    *         key.
    * @throws IllegalArgumentException
    *             Thrown if the specified key is null.
    */
    public boolean remove(K key);

    /**
    * Test whether the map contains an entry for the given key.
    *
    * @param key
    *            The key to test.
    * @return Returns true if the map contains an entry with the specified
    *         key, false otherwise.
    */
    public boolean contains(K key);

    /**
    * Resizes the map to use a greater number of buckets.
    * Keeps all current entries of the SimpleHashMap, but they may be in a different order.
    *
    * @param updatedCapacity The new capacity (i.e. the new number of buckets after resizing).
    * @throws IllegalArgumentException
    *             Thrown if the desired capacity is smaller than the current capacity.
    */
    public void resize(int updatedCapacity);

}

Pair
/**
* Entries for all implementations of the {@link HashMapInterface} interface.
*/
public class Pair<eK, eV> {

    public final eK key;
    public eV value;

    public Pair<eK, eV> nextPairInBucket;

    public Pair(eK key, eV value, Pair<eK, eV> nextPairInBucket) {
       this.key = key;
       this.value = value;
       this.nextPairInBucket = nextPairInBucket;
    }

    public eK getKey() {
       return key;
    }

    public eV getValue() {
       return value;
    }
}

PublicTest1
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;

import java.util.LinkedList;
import java.util.List;

import static org.junit.Assert.*;

public class ResizingHashMapPublicTest {

private <K, V> ResizingHashMap<K, V> instantiate(int size) throws Exception {
return new ResizingHashMap<K,V>(size);
}

//requires replacement of init, getBucketIndex, and insertEntry
private void fillWithSimpleEntries(HashMapInterface<String, String> map, int n) {
for (int i = 0; i < n; i++) {
String k = i + "", v = "value" + i;
Pair<String, String> entry = new Pair<>(k, v, null);
map.insertEntry(map.getBucketIndex(k), entry);
}
}

//requires replacement of init, getBucketIndex, and insertEntry
private void putKeyStringEntry(HashMapInterface<Key, String> map, Key key, String value) {
Pair<Key, String> entry = new Pair<>(key, value, null);
map.insertEntry(map.getBucketIndex(entry.getKey()), entry);
}

@Test(timeout = 100)
public void mainTestInitialBucketSize() throws Exception {
ResizingHashMap<Integer, String> hashMap = new ResizingHashMap<Integer, String>(11);

assertEquals("Number of buckets does not match (await 11 got " + hashMap.buckets.length + ")", 11, hashMap.buckets.length);
}

@Test(timeout = 100)
public void mainTestInitialSizeIsZero() throws Exception {
ResizingHashMap<Integer, String> hashMap = new ResizingHashMap<Integer, String>(11);

assertEquals("Initial size wrong. HashMap is not empty", 0, hashMap.size());
}

@Test(timeout = 100)
public void mainTestInsertEntry() throws Exception {
ResizingHashMap<Integer, String> hashMap = new ResizingHashMap<Integer, String>(11);

Pair<Integer, String> e1 = new Pair<Integer, String>(1, "Test1", null);
hashMap.insertEntry(1, e1);
assertEquals("Size wrong ", 1, hashMap.size());
assertEquals("Pair not found in expected bucket with index 1", e1, hashMap.buckets[1]);
}

@Test(timeout = 100)
public void mainTestBucketIndices() throws Exception {
ResizingHashMap<Integer, String> hashMap = new ResizingHashMap<Integer, String>(11);

// Calculation of bucket indices
assertEquals("Wrong bucket index for given key", 2, hashMap.getBucketIndex(2));
assertEquals("Wrong bucket index for given key", 8, hashMap.getBucketIndex(8));
assertEquals("Wrong bucket index for given key", 10, hashMap.getBucketIndex(10));
}

@Test(timeout = 100)
public void testSimpleBucketIndex() throws Exception {
ResizingHashMap<Key, Object> map = instantiate(5);
int hash = 1;
assertEquals("Returned wrong index for storing key in buckets", hash, map.getBucketIndex(new Key(hash, "")));
}

@Test(timeout = 100)
public void mainTestGetEntry() throws Exception {
ResizingHashMap<Integer, String> hashMap = new ResizingHashMap<Integer, String>(11);
Pair<Integer, String> e1 = new Pair<Integer, String>(3, "Test1", null);
hashMap.insertEntry(3, e1);

// Get Pair Object
Pair<Integer, String> q = hashMap.getEntry(3);

assertEquals("GetEntry returned unexpected object", e1, q);
}

@Test(timeout = 100)
public void mainTestGet() throws Exception {
ResizingHashMap<Integer, String> hashMap = new ResizingHashMap<Integer, String>(11);
Pair<Integer, String> e2 = new Pair<Integer, String>(9, "Test2", null);

hashMap.insertEntry(9, e2);
String t2 = hashMap.get(9);

assertEquals("Get did not return recently inserted entry", "Test2", t2);
}

@Test(timeout = 100)
public void mainTestContains() throws Exception {
ResizingHashMap<Integer, String> hashMap = new ResizingHashMap<Integer, String>(11);
Pair<Integer, String> e3 = new Pair<Integer, String>(8, "Test3", null);
hashMap.insertEntry(8, e3);

assertEquals("GetEntry did return wrong entry", e3, hashMap.getEntry(8));
assertTrue("Contains wrong for recently added entry", hashMap.contains(8));
assertFalse("Contains was true for non-existing entry", hashMap.contains(16));
}

@Test(timeout = 100)
public void mainTestPut() throws Exception {
ResizingHashMap<Integer, String> hashMap = new ResizingHashMap<Integer, String>(11);

// Add KV-Pair
Pair<Integer, String> e1 = new Pair<Integer, String>(1, "Test1", null);
hashMap.insertEntry(1, e1);
// Get
Pair<Integer, String> e2 = new Pair<Integer, String>(9, "Test2", null);
hashMap.insertEntry(9, e2);
//contains
Pair<Integer, String> e3 = new Pair<Integer, String>(8, "Test3", null);
hashMap.insertEntry(8, e3);

assertEquals("Size wrong before put", 3, hashMap.size());

hashMap.put(3, "Test");

assertEquals("Size wrong after put", 4, hashMap.size());
assertTrue("Contains wrong for recently added entry", hashMap.contains(3));
String t = hashMap.get(3);
assertEquals("Get wrong for recently added entry", "Test", t);
}

@Test(timeout = 100)
public void mainTestRemoveFirstPart() throws Exception {
ResizingHashMap<Integer, String> hashMap2 = new ResizingHashMap<Integer, String>(11);

Pair<Integer, String> e1 = new Pair<Integer, String>(1, "Test1", null);
Pair<Integer, String> e2 = new Pair<Integer, String>(9, "Test2", null);
Pair<Integer, String> e3 = new Pair<Integer, String>(8, "Test3", null);

hashMap2.insertEntry(1, e1);
hashMap2.insertEntry(9, e2);
hashMap2.insertEntry(8, e3);

assertEquals("Size wrong", 3, hashMap2.size());

assertTrue("Did you really remove the entry?", hashMap2.remove(1));
assertEquals("Size wrong", 2, hashMap2.size());
}

@Test(timeout = 100)
public void mainTestRemoveSecondPart() throws Exception {
ResizingHashMap<Integer, String> hashMap2 = new ResizingHashMap<Integer, String>(11);

Pair<Integer, String> e2 = new Pair<Integer, String>(9, "Test2", null);
Pair<Integer, String> e3 = new Pair<Integer, String>(8, "Test3", null);
Pair<Integer, String> e4 = new Pair<Integer, String>(2, "Test4", null);
Pair<Integer, String> e5 = new Pair<Integer, String>(10, "Test5", null);
Pair<Integer, String> e6 = new Pair<Integer, String>(7, "Test6", null);

hashMap2.insertEntry(9, e2);
hashMap2.insertEntry(8, e3);

assertFalse("Contains was true for non-existing entry", hashMap2.contains(2));
hashMap2.insertEntry(2, e4);
assertTrue("Contains wrong for recently added entry", hashMap2.contains(2));

hashMap2.insertEntry(10, e5);
hashMap2.insertEntry(7, e6);

assertTrue("Return value of remove wrong", hashMap2.remove(8));
assertEquals("Size wrong after remove", 4, hashMap2.size());
}

@Test(timeout = 100)
public void mainTestResize() throws Exception {
ResizingHashMap<Integer, String> hashMap3 = new ResizingHashMap<Integer, String>(11);

Pair<Integer, String> e1 = new Pair<Integer, String>(1, "Test1", null);
Pair<Integer, String> e2 = new Pair<Integer, String>(9, "Test2", null);
Pair<Integer, String> e3 = new Pair<Integer, String>(8, "Test3", null);
Pair<Integer, String> e4 = new Pair<Integer, String>(2, "Test4", null);
Pair<Integer, String> e5 = new Pair<Integer, String>(10, "Test5", null);
Pair<Integer, String> e6 = new Pair<Integer, String>(7, "Test6", null);
hashMap3.insertEntry(1, e1);
hashMap3.insertEntry(9, e2);
hashMap3.insertEntry(8, e3);
hashMap3.insertEntry(2, e4);
hashMap3.insertEntry(10, e5);
hashMap3.insertEntry(7, e6);

hashMap3.resize(19);
assertEquals("Size wrong", 6, hashMap3.size());
assertEquals("Wrong number of buckets after resize", 19, hashMap3.buckets.length);
assertEquals("Wrong bucket index for given key", 4, hashMap3.getBucketIndex(4));
assertEquals("Wrong bucket index for given key", 8, hashMap3.getBucketIndex(8));

}


// =========================================================================

private static final class Key {
private int hash;
private String key;

public Key(int hash, String key) {
this.hash = hash;
this.key = key;
}

@Override
public int hashCode() {
return hash;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Key other = (Key) obj;
if (key == null) {
if (other.key != null)
return false;
} else if (!key.equals(other.key))
return false;
return true;
}
}

public static void main(String args[]) {
// to compile on command line: javac -cp .:/usr/share/java/junit4.jar *.java
// to run on command line: java -cp .:/usr/share/java/junit4.jar ResizingHashMapPublicTest

// starts junit runner - don't try to understand!
org.junit.runner.JUnitCore.main(new Object() { }.getClass().getEnclosingClass().getSimpleName());
}
}

Other Test (less important)

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;


import static org.junit.Assert.*;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class CustomeHashMapTest_Alpha {


@Test//(timeout = 100)
public void A1_test_remove_colided_Array_object_e3() throws Exception {
ResizingHashMap<Integer, String> hashMap2 = new ResizingHashMap<Integer, String>(11);

Pair<Integer, String> e2 = new Pair<Integer, String>(9, " e2 ", null);
Pair<Integer, String> e3 = new Pair<Integer, String>(8, " e3 ", null);
Pair<Integer, String> e4 = new Pair<Integer, String>(2, " e4 ", null);
Pair<Integer, String> e5 = new Pair<Integer, String>(10, " e5 ", null);
Pair<Integer, String> e6 = new Pair<Integer, String>(7, " e6 ", null);

hashMap2.insertEntry(9, e2);
hashMap2.insertEntry(2, e4);
hashMap2.insertEntry(10, e5);

hashMap2.insertEntry(8, e3);
hashMap2.insertEntry(8, e6);

assertTrue("The wrong object was going to be removed - should be e3 but was " +hashMap2.buckets[8], hashMap2.buckets[8].equals(e3));
assertNotNull("The object e6 should colide with object e3 but e3 is null and does not point to e6",hashMap2.buckets[8].nextPairInBucket);

assertTrue("The object e6 should colide with object e3 but e3 does not point to e6 but to "+hashMap2.buckets[8].nextPairInBucket, hashMap2.buckets[8].nextPairInBucket.equals(e6));
print(hashMap2);
assertTrue("Return value of remove wrong", hashMap2.remove(8));
printAfterOperation(hashMap2);
assertNotNull("The bucket on index 8 should not be empty after removing it", hashMap2.buckets[8]);
assertTrue("The wrong object replaced e3- should be e6 but was " +hashMap2.buckets[8], hashMap2.buckets[8].equals(e6));

assertEquals("Size wrong after remove", 4, hashMap2.size());
}

@Test//(timeout = 100)
public void A2_test_remove_colided_List_object_e5() throws Exception {
//TODO
ResizingHashMap<Integer, String> hashMap2 = new ResizingHashMap<Integer, String>(11);

Pair<Integer, String> e2 = new Pair<Integer, String>(9, " e2 ", null);
Pair<Integer, String> e4 = new Pair<Integer, String>(2, " e4 ", null);

Pair<Integer, String> e3 = new Pair<Integer, String>(8, " e3 ", null);
Pair<Integer, String> e5 = new Pair<Integer, String>(19, " e5 ", null);
Pair<Integer, String> e6 = new Pair<Integer, String>(30, " e6 ", null);

hashMap2.insertEntry(9, e2);
hashMap2.insertEntry(2, e4);

hashMap2.insertEntry(8, e3);
hashMap2.insertEntry(8, e5);
hashMap2.insertEntry(8, e6);
print(hashMap2);

assertTrue("The wrong object was going to be removed - should be e5 but was object with value = " +hashMap2.buckets[8].nextPairInBucket.value, hashMap2.buckets[8].nextPairInBucket.equals(e5));
assertNotNull("The object e6 should colide with object e3 but e3 is null and does not point to e6",hashMap2.buckets[8].nextPairInBucket);

assertTrue("The object e5 should colide with object e3 but e3 does not point to e5 but to "+hashMap2.buckets[8].nextPairInBucket.value, hashMap2.buckets[8].nextPairInBucket.equals(e5));
assertTrue("Return value of remove wrong", hashMap2.remove(e5.key));
printAfterOperation(hashMap2);
assertNotNull("The bucket on index 8 should not be empty after removing it", hashMap2.buckets[8]);
assertTrue("The wrong object replaced e5- should be e6 but was " +hashMap2.buckets[8].nextPairInBucket.value, hashMap2.buckets[8].nextPairInBucket.equals(e6));

assertEquals("Size wrong after remove", 4, hashMap2.size());
}

@Test//(timeout = 100)
public void A3_resize_with_colided_entry_e7() throws Exception {
ResizingHashMap<Integer, String> hashMap3 = new ResizingHashMap<Integer, String>(11);

Pair<Integer, String> e1 = new Pair<Integer, String>(1, " e1 ", null);
Pair<Integer, String> e2 = new Pair<Integer, String>(9, " e2 ", null);
Pair<Integer, String> e3 = new Pair<Integer, String>(8, " e3 ", null);
Pair<Integer, String> e4 = new Pair<Integer, String>(2, " e3 ", null);
Pair<Integer, String> e5 = new Pair<Integer, String>(10, " e5 ", null);
Pair<Integer, String> e6 = new Pair<Integer, String>(7, " e6 ", null);
Pair<Integer, String> e7 = new Pair<Integer, String>(18, " e7 ", null);


hashMap3.insertEntry(1, e1);
hashMap3.insertEntry(9, e2);
hashMap3.insertEntry(8, e3);
hashMap3.insertEntry(2, e4);
hashMap3.insertEntry(10, e5);
hashMap3.insertEntry(7, e6);
hashMap3.insertEntry(7, e7);

print(hashMap3);
assertEquals("e3 bucket slot does not match with the bucket index generated with getBucketIndex befor resizing the Array",hashMap3.buckets[hashMap3.getBucketIndex(e3.key)],hashMap3.buckets[8]);
assertEquals("bucket witch points to e7 does not match with the bucket index generated with getBucketIndex befor resizing the Array",hashMap3.buckets[hashMap3.getBucketIndex(e6.key)].nextPairInBucket.value,hashMap3.buckets[7].nextPairInBucket.value);

hashMap3.resize(19);
assertEquals("e3 bucket slot does not match with the bucket index generated with getBucketIndex after resizing the Array",hashMap3.buckets[hashMap3.getBucketIndex(e3.key)],hashMap3.buckets[8]);
assertEquals("bucket witch points to e7 does not match with the bucket index generated with getBucketIndex befor resizing the Array",hashMap3.buckets[hashMap3.getBucketIndex(e6.key)].nextPairInBucket.value,hashMap3.buckets[7].nextPairInBucket.value);
printAfterOperation(hashMap3);


assertEquals("Size wrong", 7, hashMap3.size());
assertEquals("Wrong number of buckets after resize", 19, hashMap3.buckets.length);
assertEquals("Wrong bucket index for given key", 4, hashMap3.getBucketIndex(4));
assertEquals("Wrong bucket index for given key", 8, hashMap3.getBucketIndex(8));

assertNotNull("The object e7 should colide with object e6 but e6 is null and does not point to e7", hashMap3.buckets[7].nextPairInBucket);
assertTrue("The object e7 should colide with object e6 but e6 does not point to e7 but to " +hashMap3.buckets[7].nextPairInBucket, hashMap3.buckets[7].nextPairInBucket.equals(e7));
}


// =========================================================================
public void print(ResizingHashMap<Integer,String > hashMap) {
StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[2];
String methodenName = stackTraceElement.getMethodName();

System.out.println("#########################################################################");
System.out.println("############### "+methodenName +" ");
System.out.println("#########################################################################");

System.out.println(" ------------ ");
for(int i=0; i< hashMap.buckets.length; i++){

if(hashMap.buckets[i] != null){

System.out.print("|   "+hashMap.buckets[i].value+"    |");

if(hashMap.buckets[i].nextPairInBucket != null){
Pair<Integer, String> next=hashMap.buckets[i].nextPairInBucket;

while(next != null){

System.out.print("--->|");
System.out.print("   "+next.value+"    |");
next=next.nextPairInBucket;
}
System.out.println();

}else{
System.out.println();
}
}else{
System.out.println("|            |");

}
System.out.println(" ------------ ");
}
System.out.println();
}

public void printAfterOperation(ResizingHashMap<Integer,String > hashMap) {

System.out.println("=========== AFTER OPERATION =========");
System.out.println();

System.out.println(" ------------ ");
for(int i=0; i< hashMap.buckets.length; i++){

if(hashMap.buckets[i] != null){

System.out.print("|   "+hashMap.buckets[i].value+"    |");

if(hashMap.buckets[i].nextPairInBucket != null){
Pair<Integer, String> next=hashMap.buckets[i].nextPairInBucket;

while(next != null){

System.out.print("--->|");
System.out.print("   "+next.value+"    |");
next=next.nextPairInBucket;
}
System.out.println();

}else{
System.out.println();
}
}else{
System.out.println("|            |");

}
System.out.println(" ------------ ");
}
System.out.println();
}

public static void main(String args[]) {
// to compile on command line: javac -cp .:/usr/share/java/junit4.jar *.java
// to run on command line: java -cp .:/usr/share/java/junit4.jar ResizingHashMapPublicTest

// starts junit runner - don't try to understand!
org.junit.runner.JUnitCore.main(new Object() { }.getClass().getEnclosingClass().getSimpleName());
}
}

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

public class ResizingHashMap<K, V> implements HashMapInterface<K, V> {

    private int size;

    public Pair<K, V>[] buckets;

    @SuppressWarnings("unchecked")
    public ResizingHashMap(int capacity) {
       //TODO a
       buckets = new Pair[capacity];
       // init the array
       for (int i = 0; i < buckets.length; i++) {
            buckets[i] = null;            
       }
       // init the size
       size = 0;
    }...

This is only a preview of the solution. Please use the purchase button to see the entire solution

$40.00

or $1 if you
register a new account!

Assisting Tutor

Related Homework Solutions

Java Program For Vending Machine
Homework Solution
$40.00
Java
Programming
Computer Science
Vending Machine
Drinks
Password
Screen
Inventory
Menu
Errors
Money
Java Programming Assignment
Homework Solution
$30.00
Java Programming
Variables
ToString Method
Equals Method
Overload
Computer Science
Matrix Problem in Java
Homework Solution
$13.00
Java
Programming
Coding
Computer Science
Matrix
Diagonals
Input File
Output File
Integers
Loops
Conditions
Statements
Swapping Numbers
Random Values
Variables
Simple & Compound Interest Using Java
Homework Solution
$65.00
Java
Programming
Codes
Algorithms
Computer Science
Statements
Variables
Loops
Input
Output
Integers
Strings
Simple Interest
Compound Interest
Money
Finance
Annual Rates
Calculations
Functions
Screen Messages
Get help from a qualified tutor
Live Chats