Question: / / - - - - - - - - - - - - - - - - - - - - - - -
MapEntry.java by DaleJoyceWeems Chapter
Provides key, value pairs for use with a Map.
Keys are immutable.
package chmaps;
public class MapEntry
protected K key;
protected V value;
MapEntryK k V v
key k; value v;
public K getKeyreturn key;
public V getValuereturn value;
public void setValueV vvalue v;
@Override
public String toString
Returns a string representing this MapEntry.
return "Key : key
Value: value;
HMap.java by DaleJoyceWeems Chapter
Implements a map using an arraybased hash table, linear probing collision
resolution.
The remove operation is not supported. Invoking it will result in the
unchecked UnsupportedOperationException being thrown.
A map provides K key, V value pairs, mapping the key onto the value.
Keys are unique. Keys cannot be null.
Methods throw IllegalArgumentException if passed a null key argument.
Values can be null, so a null value returned by put or get does
not necessarily mean that an entry did not exist.
package chmaps;
import java.util.Iterator;
public class HMap implements MapInterface
protected MapEntry map;
protected final int DEFCAP ; default capacity
protected final double DEFLOAD ; default load
protected int origCap; original capacity
protected int currCap; current capacity
protected double load;
protected int numPairs ; number of pairs in this map
public HMap
map new MapEntryDEFCAP;
origCap DEFCAP;
currCap DEFCAP;
load DEFLOAD;
public HMapint initCap, double initLoad
map new MapEntryinitCap;
origCap initCap;
currCap initCap;
load initLoad;
private void enlarge
Increments the capacity of the map by an amount
equal to the original capacity.
create a snapshot iterator of the map and save current size
Iterator i iterator;
int count numPairs;
create the larger array and reset variables
map new MapEntrycurrCap origCap;
currCap currCap origCap;
numPairs ;
put the contents of the current map into the larger array
MapEntry entry;
for int n ; n count; n
entry inext;
this.putKentrygetKeyVentrygetValue;
public V putK k V v
If an entry in this map with key k already exists then the value
associated with that entry is replaced by value v and the original
value is returned; otherwise, adds the k v pair to the map and
returns null.
if k null
throw new IllegalArgumentExceptionMaps do not allow null keys.";
MapEntry entry new MapEntryk v;
int location Math.abskhashCode currCap;
while maplocation null && maplocationgetKeyequalsk
location location currCap;
if maplocation null k was not in map
maplocation entry;
numPairs;
if floatnumPairscurrCap load
enlarge;
return null;
else k already in map
V temp VmaplocationgetValue;
maplocation entry;
return temp;
public V getK k
If an entry in this map with a key k exists then the value associated
with that entry is returned; otherwise null is returned.
if k null
throw new IllegalArgumentExceptionMaps do not allow null keys.";
int location Math.abskhashCode currCap;
while maplocation null && maplocationgetKeyequalsk
location location currCap;
if maplocation null k was not in map
return null;
else k in map
return VmaplocationgetValue;
public V removeK k
Throws UnsupportedOperationException.
throw new UnsupportedOperationExceptionHMap does not allow remove.";
public boolean containsK k
Returns true if an entry in this map with key k exists;
Returns false otherwise.
if k null
throw new IllegalArgumentExceptionMaps do not allow null keys.";
int location Math.abskhashCode currCap;
while maplocation null
if maplocation
must modify the books HMap class directly to complete this.
It includes a toString method that prints out the entire contents.
Provide a working remove method, using an additional boolean value associated with each hash table slot to track removal.
Instead of probing it uses buckets of linked lists of MapEntry objects.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
