AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
aud.HashMap< Key, Value > Class Template Reference

Implementation of an unordered map based on a hash table. More...

Classes

class  Bucket
 entry (key,value) in HashMap with link to next

 
class  Entries
 provide HashMap iterator over key-value pairs (Entry) More...
 
class  Entry
 entry (key,value) in HashMap (iterator) More...
 
class  EntryIterator
 iterator over key-value pairs (Entry) in HashMap
 

Public Member Functions

 HashMap (int size)
 create empty map More...
 
 HashMap ()
 create empty map More...
 
int getHashtableSize ()
 get current size of hash table (will be prime) More...
 
int size ()
 get current number of entries More...
 
boolean is_empty ()
 is map empty? More...
 
double getLoadFactor ()
 get current load factor More...
 
void reserve (int size)
 Reserve buckets for expected size. More...
 
Entries entries ()
 get iterable instance
for (HashMap<K,T>.Entry entry : map.entries()) { ... } @endiliteral
More...
 
Value insert (Key key, Value value)
 insert key-value pair More...
 
Value remove (Key key)
 remove entry (equivalent to insert(key,null)) More...
 
Value find (Key key)
 find value for key @endiliteral
More...
 
boolean contains (Key key)
 Is key contained in map? More...
 
String toString ()
 

Static Public Member Functions

static void main (String args[])
 

Detailed Description

Implementation of an unordered map based on a hash table.

HashMap implements a hash table with separate chaining for collision handling. (For better performance, it implements its own singly linked list made of Bucket instances.) In case a user defined threshold on the load factor (see getLoadFactor, setMaximumLoadFactor is exceeded on insert, a rehashing is triggered. The size of the hash table getHashtableSize is doubled on rehashing and automatically maintained to be prime (see getNextPrime). It may be a good ideap to reserve enough buckets to avoid rehashing if there is a reasonable guess on the expected number of entries.

HashMap provides an iterator that enumerates all key-value-pairs (Entry) in an arbitrary order. In particular, the order may change after an insert operation. Any manipulation of the map (insert or remove) invalidates all iterators in use!

The key-value mapping uses the special convention that null values are never mapped. Inserting a key wit a null value is hence equivalent to removeing the entry! (Use another value instance like "nil" as a placeholder in case you need such semantics.)

Definition at line 31 of file HashMap.java.

Constructor & Destructor Documentation

◆ HashMap() [1/2]

aud.HashMap< Key, Value >.HashMap ( int  size)

create empty map

Parameters
sizeestimated size (sets initial number of buckets)

Definition at line 135 of file HashMap.java.

135 {
136 int n=(int) ((double) size/maxLoad_);
137 if (n<size) n=size;
138 n=getNextPrime(n);
139 table_=new Object[n];
140 }
int size()
get current number of entries
Definition: HashMap.java:153

References aud.HashMap< Key, Value >.size().

+ Here is the call graph for this function:

◆ HashMap() [2/2]

aud.HashMap< Key, Value >.HashMap ( )

create empty map

Definition at line 143 of file HashMap.java.

143 {
144 this(PRIMES[0]);
145 }

Member Function Documentation

◆ contains()

boolean aud.HashMap< Key, Value >.contains ( Key  key)

Is key contained in map?

Definition at line 295 of file HashMap.java.

295 {
296 return find(key)!=null;
297 }
Value find(Key key)
find value for key @endiliteral
Definition: HashMap.java:281

References aud.HashMap< Key, Value >.find().

+ Here is the call graph for this function:

◆ entries()

Entries aud.HashMap< Key, Value >.entries ( )

get iterable instance
for (HashMap<K,T>.Entry entry : map.entries()) { ... } @endiliteral

Definition at line 207 of file HashMap.java.

207{ return new Entries(this); }

Referenced by aud.HashMap< Key, Value >.main(), and aud.HashMap< Key, Value >.toString().

+ Here is the caller graph for this function:

◆ find()

Value aud.HashMap< Key, Value >.find ( Key  key)

find value for key @endiliteral

Returns
value or null if the key is not contained in map

Definition at line 281 of file HashMap.java.

281 {
282 int hash=key.hashCode();
283 hash = hash < 0 ? -hash : hash;
284 int i=hash % table_.length;
285 if (table_[i]!=null) {
286 for (Bucket b=(Bucket) table_[i];b!=null;b=b.next) {
287 if (b.key.equals(key))
288 return b.value;
289 }
290 }
291 return null;
292 }

Referenced by aud.HashMap< Key, Value >.contains(), aud.HashMap< Key, Value >.main(), and aud.PriorityQueue< T >.main().

+ Here is the caller graph for this function:

◆ getHashtableSize()

int aud.HashMap< Key, Value >.getHashtableSize ( )

get current size of hash table (will be prime)

Definition at line 148 of file HashMap.java.

148 {
149 return table_.length;
150 }

◆ getLoadFactor()

double aud.HashMap< Key, Value >.getLoadFactor ( )

get current load factor

Definition at line 159 of file HashMap.java.

159 {
160 return (double) size_/(double) table_.length;
161 }

◆ insert()

Value aud.HashMap< Key, Value >.insert ( Key  key,
Value  value 
)

insert key-value pair

Parameters
keyif map already contains key then its value is changed
valuestored (or new) value. Providing value=null is equivalent to removeing an enty (null values are not stored!}
Returns
old value, i.e., null if the key was not contained in map before

Definition at line 260 of file HashMap.java.

260 {
261 Value v=_set(key,value);
262 if (v==null && value!=null) { // increased size()?
263 if (getLoadFactor()>maxLoad_)
264 rehash(table_.length*2); // rehash if maximum load factor exceeded
265 }
266 return v;
267 }
double getLoadFactor()
get current load factor
Definition: HashMap.java:159

Referenced by aud.HashMap< Key, Value >.main(), and aud.PriorityQueue< T >.main().

+ Here is the caller graph for this function:

◆ is_empty()

boolean aud.HashMap< Key, Value >.is_empty ( )

is map empty?

Definition at line 156 of file HashMap.java.

156{ return size_==0; }

◆ main()

static void aud.HashMap< Key, Value >.main ( String  args[])
static

Definition at line 308 of file HashMap.java.

308 {
309 HashMap<String,Integer> map=new HashMap<String,Integer>();
310
311 map.insert("a",1);
312 map.insert("b",2);
313 map.insert("c",3);
314 map.insert("d",4);
315 map.insert("e",5);
316 map.insert("f",6);
317 map.insert("g",7);
318 map.insert("h",8);
319
320 for (HashMap<String,Integer>.Entry entry : map.entries()) {
321 System.out.println(entry.key+" => "+entry.value);
322 }
323 System.out.println(map);
324 map.remove("a");
325 System.out.println(map);
326 System.out.println(map.find("c"));
327 System.out.println(map.find("x"));
328 }

References aud.HashMap< Key, Value >.entries(), aud.HashMap< Key, Value >.find(), aud.HashMap< Key, Value >.insert(), and aud.HashMap< Key, Value >.remove().

+ Here is the call graph for this function:

◆ remove()

Value aud.HashMap< Key, Value >.remove ( Key  key)

remove entry (equivalent to insert(key,null))

Parameters
key
Returns
value of entry (null if the key was not conatined in map)

Definition at line 273 of file HashMap.java.

273 {
274 return _set(key,null);
275 }

Referenced by aud.HashMap< Key, Value >.main().

+ Here is the caller graph for this function:

◆ reserve()

void aud.HashMap< Key, Value >.reserve ( int  size)

Reserve buckets for expected size.

May call rehash, will only increase table size.

Parameters
sizeexpected number of entries

Definition at line 175 of file HashMap.java.

175 {
176 if (size>size_)
177 rehash((int) ((double) size)/maxLoad_);
178 }

References aud.HashMap< Key, Value >.size().

+ Here is the call graph for this function:

◆ size()

int aud.HashMap< Key, Value >.size ( )

get current number of entries

Definition at line 153 of file HashMap.java.

153{ return size_; }

Referenced by aud.HashMap< Key, Value >.HashMap(), and aud.HashMap< Key, Value >.reserve().

+ Here is the caller graph for this function:

◆ toString()

String aud.HashMap< Key, Value >.toString ( )

Definition at line 299 of file HashMap.java.

299 {
300 String s="{ ";
301 for (Entry entry : entries())
302 s+=(entry.key.toString()+"=>"+entry.value+",");
303 s=s.substring(0,s.length()-1);
304 return s+=" };";
305 }
Entries entries()
get iterable instance for (HashMap<K,T>.Entry entry : map.entries()) { ... } @endiliteral
Definition: HashMap.java:207

References aud.HashMap< Key, Value >.entries().

+ Here is the call graph for this function:

The documentation for this class was generated from the following file: