您的位置:首页 > 编程语言 > Java开发

Java学习笔记之HashMap

2011-12-15 22:21 561 查看
HashMap用法
   [折叠]
 
package hashmap;
 
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
 
public class HashMap1 {
//初始化
private void init(Map map,String kind)
{
   if(map != null)
   {
    for(int i=1; i<6; i++)
    {
     map.put(String.valueOf(i),kind+i);
    }
   }
}
 
//结果输出
private void outPut(Map map)
{
   if(map != null)
   {
    Object key    = null;
    Object value = null;
    Iterator iterater = map.keySet().iterator();
    while(iterater.hasNext())
    {
     key = iterater.next();
     value = map.get(key);
     System.out.print(key+": "+value+"/t");
    }
    System.out.println("/n");
   }
}
public static void main(String args[])
{
    HashMap hashmap = new HashMap();
    hashmap.put("x", "1");
    hashmap.put("u", "2");
    hashmap.put("z", "3");
    hashmap.put("h", "4");
    hashmap.put("a", "5");
    hashmap.put("o", "6");
    hashmap.put("g", "7");
    hashmap.put("u", "8");
    hashmap.put("a", "9");
    hashmap.put("n", "10");
    hashmap.put("g", "11");

     Object key    = null;
     Object value = null;
     Iterator iterater = hashmap.keySet().iterator();
     while(iterater.hasNext())
     {
      key = iterater.next();
      value = hashmap.get(key);
      System.out.print(key+": "+value+"/t");
     }
     System.out.println("/n");

}
//声明HashMap对象
private void setHashMap()
{
   HashMap hashMap = new HashMap();
   init(hashMap,"HashMap");
   hashMap.put(null,"键值为空");
   hashMap.put("值为空",null);
   System.out.println("这是HashMap对象的键与值:");
   outPut(hashMap);
}
     //声明Hashtable对象
private void setHashtable(){
   Hashtable hashtable = new Hashtable();
   init(hashtable,"Hashtable");
   //hashtable.put(null,"键值为空"); Hashtable不允许键或值为null;
   //hashtable.put("值为空",null);
   System.out.println("这是Hashtable对象的键与值:");
   outPut(hashtable);
}
     //声明LinkedHashMap对象
private void setLinkedHashMap(){
   LinkedHashMap linkedHashMap = new LinkedHashMap();
   init(linkedHashMap,"LinkedHashMap");
   linkedHashMap.put(null,"键值为空");
   linkedHashMap.put("值为空",null);
   System.out.println("这是LinkedHashMap对象的键与值:");
   outPut(linkedHashMap);
}
     //声明TreeMap对象
private void setTreeMap(){
   TreeMap treeMap = new TreeMap();
   //TreeMap treeMap = new TreeMap(new MySort());//按自定义的方式排序
   init(treeMap,"TreeMap");
   treeMap.put("0", "后插入的值");
   //treeMap.put(null,"键值为空"); TreeMap不允许键或值为null
   //treeMap.put("值为空",null);
   System.out.println("这是TreeMap对象的键与值:");
   outPut(treeMap);
}
// public static void main(String[] args){
//   HashMapDemo tm = new HashMapDemo();
//   tm.setHashMap();
//   tm.setHashtable();
//   tm.setLinkedHashMap();
//   tm.setTreeMap();
// 
//   Map hashMap = new HashMap();
//   hashMap.put(null, "键值为null");
//   hashMap.put("值为null", null);
//   System.out.println("新建HashMap对象元素的记录数是:"+hashMap.size()); 
//   hashMap.remove(null);
//   System.out.println("删除键值为null的HashMap对象元素的记录数是:"+hashMap.size()); 
// }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: