Anyone? J unit test cases

profilecompSci
hashmap.java

package indexinvertor; /* * To change this template, choose Tools | Templates and open the template in * the editor. */ import java.util.*; /** * * @author AN */ public class HashMap implements Map<Object, Object> { private int size; private float loadFactor; private int count; private int maxChainLength; class Entry implements Map.Entry<Object, Object> { int hash; Object key; Object value; protected Entry(int hash, Object key, Object firstValue) { this.hash = hash; this.key = key; this.value = firstValue; } @Override public Object getKey() { return key; } @Override public Object getValue() { return value; } @Override public Object setValue(Object value) { Object temp = this.value; this.value = value; return temp; } }; private ArrayList<ArrayList<Entry>> hashTable; /* * Returns the number of keys in this hashtable. */ @Override public int size() { return size; } /* * Tests if this hashtable maps no keys to values. */ @Override public boolean isEmpty() { return size == 0; } /* * Tests if the specified object is a key in this hashtable. * returns true if and only if the specified object */ @Override public boolean containsKey(Object key) { return get(key) != null; } /* * Tests if some key maps into the specified value in this hashtable. * returns true if and only if some key maps to the value argument & false otherwise. */ @Override public boolean containsValue(Object value) { if (value != null) { for (int i = 0; i < hashTable.size(); i++) { for (int j = 0; j < hashTable.get(i).size(); j++) { Entry e = hashTable.get(i).get(j); if (e.value.equals(value)) { return true; } } } } else { System.out.println(" VALUE CANNOT BE NULL"); } return false; } /* * Returns the list of values to which the specified key is mapped, * or null if this map contains no mapping for the key. */ @Override public Object get(Object key) { try { if (key != null) { String keyString = (String) key; int hash = (keyString.hashCode() & 0x7FFFFFFF) % Math.max(size, hashTable.size()); List<Object> values = new LinkedList<>(); if (hashTable.get(hash).isEmpty()) { return null; } for (int i = 0; i < hashTable.get(hash).size(); i++) { Entry e = hashTable.get(hash).get(i); if (e.key.toString().equals(key.toString()) && e.hash == hash) { values.add(e.value); } } if (!values.isEmpty()) { return values; } else { return null; } } System.out.println("KEY IS NULL"); return null; } catch (Exception e) { System.out.println("EXCEPTION : " + e.getMessage()); e.printStackTrace(); System.exit(1); } return null; } /* * Maps the specified key to the specified value in this hashtable. * Neither the key nor the value can be null * * The value can be retrieved by calling the get method * with a key that is equal to the original key. * * returns the previous values of the specified key in this hashtable, * or null if it did not have one */ @Override public List put(Object key, Object value) { if (key != null && value != null) { int hash = (key.hashCode() & 0x7FFFFFFF) % Math.max(size, hashTable.size()); List values = (List) get(key); hashTable.get(hash).add(new HashMap.Entry(hash, key, value)); if (values != null) { if (maxChainLength < values.size() + 1) { maxChainLength = values.size() + 1; } return values; } else { count++; loadFactor = hashTable.size() / count; return null; } } else { System.out.println("NO ENTRY ADDED, THE PAIR WAS INVALID"); } return null; } /* * Removes the key (and its corresponding values) from this * hashtable. This method does nothing if the key is not in the hashtable. */ @Override public Object remove(Object key) { if (key != null) { String keyString = (String) key; int hash = (keyString.hashCode() & 0x7FFFFFFF) % Math.max(size, hashTable.size()); for (int i = 0; i < hashTable.get(hash).size(); i++) { Entry e = hashTable.get(hash).get(i); if (e.key.toString().equals(key.toString()) && e.hash == hash) { hashTable.get(hash).remove(e); } } } System.out.println("KEY IS NULL"); return null; } /* * Copies all of the mappings from the specified map to this hashtable. * These mappings will replace any mappings that this hashtable had for any * of the keys currently in the specified map. */ @Override public void putAll(Map<? extends Object, ? extends Object> m) { for (Map.Entry<? extends Object, ? extends Object> e : m.entrySet()) { put(e.getKey(), e.getValue()); } } /** * Clears this hashtable so that it contains no keys. */ @Override public void clear() { for (int i = 0; i < hashTable.size(); i++) { hashTable.set(i, null); count--; } } /* * Returns a Set of the keys contained in this map. */ @Override public Set<Object> keySet() { Set<Object> keySet = null; for (int i = 0; i < hashTable.size(); i++) { for (int j = 0; j < hashTable.get(j).size(); j++) { if (hashTable.get(i).get(j) != null) { keySet.add(hashTable.get(j).get(j).key); } } } return keySet; } /* * Returns a Collecion of the values contained in this map. */ @Override public Collection<Object> values() { Collection<Object> valuesCollection = null; for (int i = 0; i < hashTable.size(); i++) { for (int j = 0; j < hashTable.get(i).size(); j++) { if (hashTable.get(i).get(j) != null) { valuesCollection.add(hashTable.get(i).get(j).value); } } } return valuesCollection; } /* * Returns a Set of the entries contained in this map. */ @Override public Set<Map.Entry<Object, Object>> entrySet() { Set<Map.Entry<Object, Object>> entrySet = null; for (int i = 0; i < hashTable.size(); i++) { for (int j = 0; j < hashTable.get(i).size(); j++) { if (hashTable.get(i).get(j) != null) { entrySet.add(hashTable.get(i).get(j)); } } } return entrySet; } /** * Constructs a new, empty hashtable with the specified size */ public HashMap(int size) { this.size = Math.max(500, size); this.loadFactor = 0.75f; maxChainLength = count = 0; hashTable = new ArrayList<>(this.size); for (int i = 0; i < this.size; i++) { hashTable.add(new ArrayList<Entry>()); } } /* * Returns the loadFactor of this hashtable. */ public float getLoadFactor() { return loadFactor; } /* * Returns the length of longest chain of values in this hashtable. */ public int getMaxChainLength() { return maxChainLength; } }