![]() |
AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
|
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[]) |
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 rehash
ing 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 remove
ing 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.
aud.HashMap< Key, Value >.HashMap | ( | int | size | ) |
create empty map
size | estimated size (sets initial number of buckets) |
Definition at line 135 of file HashMap.java.
References aud.HashMap< Key, Value >.size().
aud.HashMap< Key, Value >.HashMap | ( | ) |
boolean aud.HashMap< Key, Value >.contains | ( | Key | key | ) |
Is key
contained in map?
Definition at line 295 of file HashMap.java.
References aud.HashMap< Key, Value >.find().
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.
Referenced by aud.HashMap< Key, Value >.main(), and aud.HashMap< Key, Value >.toString().
Value aud.HashMap< Key, Value >.find | ( | Key | key | ) |
find value for key @endiliteral
null
if the key is not contained in map Definition at line 281 of file HashMap.java.
Referenced by aud.HashMap< Key, Value >.contains(), aud.HashMap< Key, Value >.main(), and aud.PriorityQueue< T >.main().
int aud.HashMap< Key, Value >.getHashtableSize | ( | ) |
get current size of hash table (will be prime)
Definition at line 148 of file HashMap.java.
double aud.HashMap< Key, Value >.getLoadFactor | ( | ) |
get current load factor
Definition at line 159 of file HashMap.java.
Value aud.HashMap< Key, Value >.insert | ( | Key | key, |
Value | value | ||
) |
insert key-value pair
key | if map already contains key then its value is changed |
value | stored (or new) value. Providing value=null is equivalent to remove ing an enty (null values are not stored!} |
null
if the key was not contained in map before Definition at line 260 of file HashMap.java.
Referenced by aud.HashMap< Key, Value >.main(), and aud.PriorityQueue< T >.main().
boolean aud.HashMap< Key, Value >.is_empty | ( | ) |
|
static |
Definition at line 308 of file HashMap.java.
References aud.HashMap< Key, Value >.entries(), aud.HashMap< Key, Value >.find(), aud.HashMap< Key, Value >.insert(), and aud.HashMap< Key, Value >.remove().
Value aud.HashMap< Key, Value >.remove | ( | Key | key | ) |
remove entry (equivalent to insert(key,null)
)
key |
null
if the key was not conatined in map) Definition at line 273 of file HashMap.java.
Referenced by aud.HashMap< Key, Value >.main().
void aud.HashMap< Key, Value >.reserve | ( | int | size | ) |
Reserve buckets for expected size
.
May call rehash
, will only increase table size.
size | expected number of entries |
Definition at line 175 of file HashMap.java.
References aud.HashMap< Key, Value >.size().
int aud.HashMap< Key, Value >.size | ( | ) |
get current number of entries
Definition at line 153 of file HashMap.java.
Referenced by aud.HashMap< Key, Value >.HashMap(), and aud.HashMap< Key, Value >.reserve().
String aud.HashMap< Key, Value >.toString | ( | ) |
Definition at line 299 of file HashMap.java.
References aud.HashMap< Key, Value >.entries().