求好心人,帮初学者看看这个HashMap为什么打印不出来呢
package s;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class HashMapTest {
public static void printElements(Collection c)
{
Iterator it=c.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
public static void main(String[] args)
{
HashMap <Object,Object> hm=new HashMap <Object,Object> ();
hm.put( "one ", "zhangsan ");
hm.put( "two ", "lisi ");
hm.put( "three ", "wangwu ");
Set entry=hm.entrySet();
//printElements(entry);
Iterator it=entry.iterator();
while(it.hasNext());
{
Map.Entry me=(Map.Entry)it.next();
System.out.println(me.getKey()+ ": "+me.getValue());
}
}
}
[解决办法]
你的while循环
while(it.hasNext());
{
Map.Entry me=(Map.Entry)it.next();
System.out.println(me.getKey()+ ": "+me.getValue());
}
那“while(it.hasNext());”后面多了个分号