天天看點

映射Map

本篇介紹java.util.Map接口下的兩個方法HashMap與Hashtable

HashMap與Hashtable的差別在于狀态,前者:非同步;後者:同步(線程)

        注:HashMap筆者認為是無序映射集合,Hashtable是按添加順序排列(未證明)

1.HashMap>>

  1. package cn.test.map;  
  2. import java.util.Collection;  
  3. import java.util.HashMap;  
  4. import java.util.Set;  
  5. import java.util.Map.Entry;  
  6. public class TestHashMap {  
  7.     /**  
  8.      * @param args  
  9.      */ 
  10.     public static void main(String[] args) {  
  11.         // TODO Auto-generated method stub  
  12.         HashMap<Integer, Person> hm = new HashMap<Integer, Person>();  
  13.         hm.put(10, new Person("小沈陽", 20, "中國"));  
  14.         hm.put(2, new Person("沈陽", 20, "日本"));  
  15.         hm.put(40, new Person("小沈", 20, "美國"));  
  16.         hm.put(25, new Person("小陽", 20, "英國"));  
  17.         //去鍵存入Set中  
  18.         Set<Integer> s = hm.keySet();  
  19.         for (int i : s) {  
  20.             System.out.println(i);  
  21.         }  
  22.         //取值存入Collection  
  23.         Collection<Person> c = hm.values();  
  24.         for (Person p : c) {  
  25.             System.out.println(p.getName());  
  26.         }  
  27.         //轉換成set集合(鍵值對)  
  28.         Set<Entry<Integer, Person>> st = hm.entrySet();  
  29.         for(Entry<Integer,Person> e:st)  
  30.         {  
  31.             Person p=e.getValue();  
  32.             System.out.println(e.getKey()+"\t"+p.getName()+"\t"+p.getAge()+"\t"+p.getAddress());  
  33.         }  
  34.     }  
  35. }  
  1. package cn.test.map;  
  2. import java.util.Collection;  
  3. import java.util.Hashtable;  
  4. import java.util.Set;  
  5. import java.util.Map.Entry;  
  6. public class TestHashTable {  
  7.     /**  
  8.      * @param args  
  9.      */ 
  10.     public static void main(String[] args) {  
  11.         // TODO Auto-generated method stub  
  12.         Hashtable<Integer, Person> ht = new Hashtable<Integer, Person>();  
  13.         ht.put(32, new Person("小沈陽", 20, "北京"));  
  14.         ht.put(102, new Person("趙本山", 23, "遼甯"));  
  15.         ht.put(22, new Person("老胡", 76, "未知"));  
  16.         Set<Integer> s = ht.keySet();  
  17.         for (Integer i : s) {  
  18.             System.out.println(i);  
  19.         }  
  20.         Collection<Person> c = ht.values();  
  21.         for (Person p : c) {  
  22.             System.out.println(p.getName() + "\t" + p.getAge() + "\t" 
  23.                     + p.getAddress());  
  24.         }  
  25.         Set<Entry<Integer, Person>> se = ht.entrySet();  
  26.         for (Entry<Integer, Person> e : se) {  
  27.             Person p = e.getValue();  
  28.             System.out.println(e.getKey() + "\t" + p.getName() + "\t" 
  29.                     + p.getAge() + "\t" + p.getAddress());  
  30.         }  
  31.     }  
  32. }  
  1. package cn.test.map;  
  2. public class Person {  
  3.     private String name;  
  4.     private int age;  
  5.     private String address;  
  6.     public void setName(String name) {  
  7.         this.name = name;  
  8.     }  
  9.     public String getName() {  
  10.         return this.name;  
  11.     }  
  12.     public int getAge() {  
  13.         return age;  
  14.     }  
  15.     public void setAge(int age) {  
  16.         this.age = age;  
  17.     }  
  18.     public String getAddress() {  
  19.         return address;  
  20.     }  
  21.     public void setAddress(String address) {  
  22.         this.address = address;  
  23.     }  
  24.     public Person(String name, int age, String adress) {  
  25.         this.name = name;  
  26.         this.age = age;  
  27.         this.address = adress;  
  28.     }  
  29. }