3import java.util.Iterator;
37 static final int PRIMES[]={
39 769, 1543, 3079, 6151, 12289,
40 24593, 49157, 98317, 196613, 393241,
41 786433, 1572869, 3145739, 6291469, 12582917,
42 25165843, 50331653, 100663319, 201326611, 402653189,
46 static int getNextPrime(
int n) {
50 throw new RuntimeException(
"table size exceeds limits");
55 Bucket(Key key,Value value) {
75 public class Entries implements Iterable<Entry> {
79 return new EntryIterator(hashmap_);
84 class EntryIterator
implements Iterator<Entry> {
89 @SuppressWarnings(
"unchecked")
90 EntryIterator(
HashMap<Key,Value> hashmap) {
93 for (idx_=0;idx_<hashmap_.table_.length;++idx_) {
94 if ((bucket_=(Bucket) hashmap_.table_[idx_])!=
null)
100 @SuppressWarnings(
"unchecked") void advance() {
101 assert(bucket_!=
null);
102 bucket_=bucket_.next;
104 while (bucket_==
null) {
105 if (++idx_>=hashmap_.table_.length)
107 bucket_=(Bucket) hashmap_.table_[idx_];
111 @Override
public boolean hasNext() {
112 return bucket_!=
null;
115 @Override
public Entry next() {
116 assert(bucket_!=
null);
117 Entry entry=
new Entry(bucket_.key,bucket_.value);
122 @Override
public void remove() {
123 throw new UnsupportedOperationException();
128 Object table_[] =
null;
129 double maxLoad_ = 0.75;
136 int n=(int) ((
double)
size/maxLoad_);
139 table_=
new Object[n];
149 return table_.length;
153 public int size() {
return size_; }
160 return (
double) size_/(double) table_.length;
164 void setMaximumLoadFactor(
double alpha) {
177 rehash((
int) ((
double)
size)/maxLoad_);
181 void rehash(
double alpha) {
182 rehash((
int) (alpha* (
double) size_));
188 @SuppressWarnings(
"unchecked")
189 void rehash(
int newSize) {
190 int n=getNextPrime(newSize);
191 System.err.println(
"INFO: rehash "+table_.length+
" -> "+n+
193 HashMap<Key,Value> ht=
new HashMap<Key,Value>(n);
194 for (Object position : table_) {
195 for (Bucket b=(Bucket) position;b!=
null;b=b.next) {
196 ht._set(b.key,b.value);
216 @SuppressWarnings(
"unchecked")
217 Value _set(Key key,Value value) {
218 int hash = key.hashCode();
219 hash = hash < 0 ? -hash : hash;
220 int i=hash % table_.length;
221 if (table_[i]==
null) {
223 table_[i]=
new Bucket(key,value);
227 for (Bucket b=(Bucket) table_[i];b!=
null;b=b.next) {
228 if (b.key.equals(key)) {
245 tail.next=
new Bucket(key,value);
260 public Value
insert(Key key,Value value) {
261 Value v=_set(key,value);
262 if (v==
null && value!=
null) {
264 rehash(table_.length*2);
273 public Value
remove(Key key) {
274 return _set(key,
null);
280 @SuppressWarnings(
"unchecked")
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))
296 return find(key)!=
null;
302 s+=(entry.key.toString()+
"=>"+entry.value+
",");
303 s=s.substring(0,s.length()-1);
308 public static void main(String args[]) {
321 System.out.println(entry.key+
" => "+entry.value);
323 System.out.println(map);
325 System.out.println(map);
326 System.out.println(map.
find(
"c"));
327 System.out.println(map.
find(
"x"));
provide HashMap iterator over key-value pairs (Entry)
Iterator< Entry > iterator()
entry (key,value) in HashMap (iterator)
Implementation of an unordered map based on a hash table.
HashMap(int size)
create empty map
static void main(String args[])
Value insert(Key key, Value value)
insert key-value pair
Entries entries()
get iterable instance for (HashMap<K,T>.Entry entry : map.entries()) { ... } @endiliteral
HashMap()
create empty map
Value find(Key key)
find value for key @endiliteral
boolean is_empty()
is map empty?
Value remove(Key key)
remove entry (equivalent to insert(key,null))
boolean contains(Key key)
Is key contained in map?
int size()
get current number of entries
int getHashtableSize()
get current size of hash table (will be prime)
double getLoadFactor()
get current load factor
void reserve(int size)
Reserve buckets for expected size.