Question Locked

hashmap and hashset in java

0 helpful
2 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

how to
convert a hashtable to
hashmap and print in the order which i entered.
using java

2 replies

by cdumitru

This can be done using a LinkedHashMap, a brother of HashMap, implementing the same Map interface.

The code is written for JDK 5, but LinkedHashMap exists from JDK 1.4:

public Map convert( Hashtable table )
{
if( table == null ) return null;
Map map = new LinkedHashMap( table.size() );
for( Map.Entry entry: table.entrySet() )
{
map.put( entry.getKey(), entry.getValue() );
}
return map;
}

public void print( Hashtable table )
{
Map map = convert( table );
System.out.println( map );
}

This discussion is closed to new replies.