class Ideone
{
	public static void main(String[] args)
	{
		Hashtable<String, Integer> table =
			new Hashtable<String, Integer>();
		System.out.println(table.set("HOGE", Integer.valueOf(123)));
		System.out.println(table.set("HOGE", Integer.valueOf(345)));
		System.out.println(table.set("HAGE", Integer.valueOf(555)));
		System.out.println(table.set("abcd", Integer.valueOf(999)));
		System.out.println(table.set("xyz0", Integer.valueOf(111)));
		
		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>
	{
		static class List
		{
			public List next = null;
			public Object key = null;
			public Object obj = null;
			public List(Object key, Object obj)
			{
				this.key = key;
				this.obj = obj;
			}
		}
		
		static final int TABLE_SIZE = 32;
		
		List[] table = null;
		int count = 0;
		
		Hashtable()
		{
			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
			{
				List list = table[h];
				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>()
			{
				List[] t = table;
				int c = count;
				int i = 0;
				List list = null;
				@Override
				public boolean hasNext()
				{
					return c > 0;
				}
				@Override
				public T next()
				{
					if (c == 0)
					{
						return null;
					}
					List result = 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;
				}
			};
		}
	}
}