class Ideone
{
public static void main
(String[] args
) {
Hashtable
<String, Integer
> table
= new Hashtable
<String, Integer
>();
Iterator<Integer> itr = table.items();
while (itr.hasNext())
{
System.
out.
println(itr.
next()); }
}
static interface Iterator<T>
{
boolean hasNext();
T next();
}
static class Hashtable<K,T>
{
{
{
this.key = key;
this.obj = obj;
}
}
static final int TABLE_SIZE = 32;
int count = 0;
{
table
= new List[TABLE_SIZE
]; }
public int getCount()
{
return count;
}
public boolean set(K key, T item)
{
if (key == null || item == null)
{
return false;
}
int h = key.hashCode() % TABLE_SIZE;
if (table[h] == null)
{
table
[h
] = new List(key, item
); }
else
{
for (;;)
{
if (key.equals(list.key))
{
return false;
}
if (list.next == null)
{
break;
}
else
{
list = list.next;
}
}
list.
next = new List(key, item
); }
++count;
return true;
}
public Iterator<T> items()
{
return new Iterator<T>()
{
int c = count;
int i = 0;
@Override
public boolean hasNext()
{
return c > 0;
}
@Override
public T next()
{
if (c == 0)
{
return null;
}
if (list == null)
{
for ( ; i < TABLE_SIZE; ++i)
{
if (t[i] != null)
{
result = t[i];
++i;
break;
}
}
}
else
{
result = list;
}
if (result == null)
{
return null;
}
list = result.next;
--c;
return (T)result.obj;
}
};
}
}
}
Y2xhc3MgSWRlb25lCnsKCXB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpCgl7CgkJSGFzaHRhYmxlPFN0cmluZywgSW50ZWdlcj4gdGFibGUgPQoJCQluZXcgSGFzaHRhYmxlPFN0cmluZywgSW50ZWdlcj4oKTsKCQlTeXN0ZW0ub3V0LnByaW50bG4odGFibGUuc2V0KCJIT0dFIiwgSW50ZWdlci52YWx1ZU9mKDEyMykpKTsKCQlTeXN0ZW0ub3V0LnByaW50bG4odGFibGUuc2V0KCJIT0dFIiwgSW50ZWdlci52YWx1ZU9mKDM0NSkpKTsKCQlTeXN0ZW0ub3V0LnByaW50bG4odGFibGUuc2V0KCJIQUdFIiwgSW50ZWdlci52YWx1ZU9mKDU1NSkp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