本篇介绍java.util.Map接口下的两个方法HashMap与Hashtable

HashMap与Hashtable的区别在于状态,前者:非同步;后者:同步(线程)

        注:HashMap笔者认为是无序映射集合,Hashtable是按添加顺序排列(未证实)

1.HashMap>>

   
   
   
   
  1. package cn.test.map;  
  2.  
  3. import java.util.Collection;  
  4. import java.util.HashMap;  
  5. import java.util.Set;  
  6. import java.util.Map.Entry;  
  7.  
  8. public class TestHashMap {  
  9.  
  10.     /**  
  11.      * @param args  
  12.      */ 
  13.     public static void main(String[] args) {  
  14.         // TODO Auto-generated method stub  
  15.         HashMap hm = new HashMap();  
  16.         hm.put(10new Person("小沈阳"20"中国"));  
  17.         hm.put(2new Person("沈阳"20"日本"));  
  18.         hm.put(40new Person("小沈"20"美国"));  
  19.         hm.put(25new Person("小阳"20"英国"));  
  20.         //去键存入Set中  
  21.         Set s = hm.keySet();  
  22.         for (int i : s) {  
  23.             System.out.println(i);  
  24.         }  
  25.         //取值存入Collection  
  26.         Collection c = hm.values();  
  27.         for (Person p : c) {  
  28.             System.out.println(p.getName());  
  29.         }  
  30.         //转换成set集合(键值对)  
  31.         Set> st = hm.entrySet();  
  32.         for(Entry e:st)  
  33.         {  
  34.             Person p=e.getValue();  
  35.             System.out.println(e.getKey()+"\t"+p.getName()+"\t"+p.getAge()+"\t"+p.getAddress());  
  36.         }  
  37.     }  
  38.       
  39. }  

2.Hashtable>>

   
   
   
   
  1. package cn.test.map;  
  2.  
  3. import java.util.Collection;  
  4. import java.util.Hashtable;  
  5. import java.util.Set;  
  6. import java.util.Map.Entry;  
  7.  
  8. public class TestHashTable {  
  9.  
  10.     /**  
  11.      * @param args  
  12.      */ 
  13.     public static void main(String[] args) {  
  14.         // TODO Auto-generated method stub  
  15.         Hashtable ht = new Hashtable();  
  16.         ht.put(32new Person("小沈阳"20"北京"));  
  17.         ht.put(102new Person("赵本山"23"辽宁"));  
  18.         ht.put(22new Person("老胡"76"未知"));  
  19.         Set s = ht.keySet();  
  20.         for (Integer i : s) {  
  21.             System.out.println(i);  
  22.         }  
  23.         Collection c = ht.values();  
  24.         for (Person p : c) {  
  25.             System.out.println(p.getName() + "\t" + p.getAge() + "\t" 
  26.                     + p.getAddress());  
  27.         }  
  28.         Set> se = ht.entrySet();  
  29.         for (Entry e : se) {  
  30.             Person p = e.getValue();  
  31.             System.out.println(e.getKey() + "\t" + p.getName() + "\t" 
  32.                     + p.getAge() + "\t" + p.getAddress());  
  33.         }  
  34.     }  
  35.  
  36. }  

附:Person类

   
   
   
   
  1. package cn.test.map;  
  2.  
  3. public class Person {  
  4.     private String name;  
  5.     private int age;  
  6.     private String address;  
  7.  
  8.     public void setName(String name) {  
  9.         this.name = name;  
  10.     }  
  11.  
  12.     public String getName() {  
  13.         return this.name;  
  14.     }  
  15.  
  16.     public int getAge() {  
  17.         return age;  
  18.     }  
  19.  
  20.     public void setAge(int age) {  
  21.         this.age = age;  
  22.     }  
  23.  
  24.     public String getAddress() {  
  25.         return address;  
  26.     }  
  27.  
  28.     public void setAddress(String address) {  
  29.         this.address = address;  
  30.     }  
  31.  
  32.     public Person(String name, int age, String adress) {  
  33.         this.name = name;  
  34.         this.age = age;  
  35.         this.address = adress;  
  36.     }  
  37. }