if(table[i]!
=null&&table[i].key!
=null)
builder.append(table[i].toString());
}
returnbuilder.append("]").toString();
}
}
interfaceMyMap{
/**Removealloftheentriesfromthismap*/
publicvoidclear();
/**Returntrueifthespecifiedkeyisinthemap*/
publicbooleancontainsKey(Kkey);
/**Returntrueifthismapcontainsthespecifiedvalue*/
publicbooleancontainsValue(Vvalue);
/**Returnasetofentriesinthemap*/
publicjava.util.Set>entrySet();
/**Returnthefirstvaluethatmatchesthespecifiedkey*/
publicVget(Kkey);
/**Returnallvaluesforthespecifiedkeyinthismap*/
publicjava.util.SetgetAll(Kkey);
/**Returntrueifthismapcontainsnoentries*/
publicbooleanisEmpty();
/**Returnasetconsistingofthekeysinthismap*/
publicjava.util.SetkeySet();
/**Addanentry(key,value)intothemap*/
publicVput(Kkey,Vvalue);
/**Removetheentriesforthespecifiedkey*/
publicvoidremove(Kkey);
/**Returnthenumberofmappingsinthismap*/
publicintsize();
/**Returnasetconsistingofthevaluesinthismap*/
publicjava.util.Setvalues();
/**DefineinnerclassforEntry*/
publicstaticclassEntry{
Kkey;
Vvalue;
publicEntry(Kkey,Vvalue){
this.key=key;
this.value=value;
}
publicKgetKey(){
returnkey;
}
publicVgetValue(){
returnvalue;
}
@Override
publicStringtoString(){
return"["+key+","+value+"]";
}
}
}
}
27.2
publicclassExercise27_02{
/**Mainmethod*/
publicstaticvoidmain(String[]args){
MyHashMapmap=newMyHashMap<>();
map.put(2,2);
System.out.println("Iskey2inthemap?
"+map.containsKey
(2));
map.remove
(2);
System.out.println("Iskey2inthemap?
"+map.containsKey
(2));
}
staticclassMyHashMapimplementsMyMap{
//Definethedefaulthashtablesize.
privatestaticintDEFAULT_INITIAL_CAPACITY=4;
//Definethemaximumhashtablesize.1<<30issameas2^30
privatestaticintMAXIMUM_CAPACITY=1<<30;
//Currenthashtablecapacity.
privateintcapacity;
//Definedefaultloadfactor
privatestaticfloatDEFAULT_MAX_LOAD_FACTOR=0.4f;
//Specifyaloadfactorusedinthehashtable
privatefloatloadFactorThreshold;
//Thenumberofentriesinthemap
privateintsize=0;
//Hashtableisanarraywitheachcellthatisalinkedlist
MyMap.Entry[]table;
/**Constructamapwiththedefaultcapacityandloadfactor*/
publicMyHashMap(){
this(DEFAULT_INITIAL_CAPACITY,DEFAULT_MAX_LOAD_FACTOR);
}
/**Constructamapwiththespecifiedinitialcapacityand
*defaultloadfactor*/
publicMyHashMap(intinitialCapacity){
this(initialCapacity,DEFAULT_MAX_LOAD_FACTOR);
}
/**Constructamapwiththespecifiedinitialcapacity
*andloadfactor*/
publicMyHashMap(intinitialCapacity,floatloadFactorThreshold){
this.capacity=initialCapacity;
this.loadFactorThreshold=loadFactorThreshold;
table=newMyMap.Entry[capacity];
}
/**Removealloftheentriesfromthismap*/
publicvoidclear(){
size=0;
removeEntries();
}
/**Returntrueifthespecifiedkeyisinthemap*/
publicbooleancontainsKey(Kkey){
if(get(key)!
=null)
returntrue;
else
returnfalse;
}
/**Returntrueifthismapcontainsthespecifiedvalue*/
publicbooleancontainsValue(Vvalue){
for(inti=0;i
if(table[i]!
=null&&table[i].value.equals(value))
returntrue;
returnfalse;
}
/**Returnasetofentriesinthemap*/
publicjava.util.Set>entrySet(){
java.util.Set>set=
newjava.util.HashSet>();
for(inti=0;iif(table[i]!
=null)
set.add(table[i]);
returnset;
}
/**Returnthefirstvaluethatmatchesthespecifiedkey*/
publicVget(Kkey){
//Performlinearprobing
intk=hash(key.hashCo