HashMap

417阅读 0评论2009-10-07 sytrydor
分类:Java

HashMap对key进行散列。
keySet()、values()、entrySet()。
keySet()获取键,
values()获取值
entrySet()获取键值
 
 
import java.util.*;
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 hm=new HashMap();
  hm.put("one","zhangsan");
  hm.put("two","lisi");
  hm.put("three","wangwu");
  
  System.out.println(hm.get("one"));
  System.out.println(hm.get("two"));
  System.out.println(hm.get("three"));
  
  
  Set keys=hm.keySet();
  System.out.println("Key:");
  printElements(keys);
  
  Collection values=hm.values();
  System.out.println("Value:");
  printElements(values);
  
  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());
  }
  
 }
}
上一篇:HashSet和TreeSet的比较
下一篇:HashMap和TreeMap的比较