Chinaunix首页 | 论坛 | 博客
  • 博客访问: 96292
  • 博文数量: 54
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 510
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-30 00:36
文章分类

全部博文(54)

文章存档

2010年(1)

2009年(52)

2008年(1)

我的朋友

分类: Java

2009-10-07 18:42:12

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());
  }
  
 }
}
阅读(366) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~