Map集合

Collection是所有集合的父親,但是Map,和Collection沒有一點關聯。Collection是單獨存儲的,而Map是以鍵值對成對出現存儲的,在某些情況下,用Map集合會比Collection有更好的效果。用一個小例子來說明一下Map和Collection的具體區別:

需求:存儲學生數據,根據學生ID找到學生。

學生類:

public class Student {
	private Integer id;
	private String name;
	public Student(Integer id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + "]";
	}
}

Collection集合實現(ArrayList)

public class ListDemo {
	public static void main(String[] args) {
		List<Student> list = new ArrayList<Student>();
		list.add(new Student(1, "張三"));
		list.add(new Student(2, "李四"));
		list.add(new Student(3, "王五"));
		//需求,獲取id爲3的學生
		for (Student student : list) {
			if (student.getId()==3) {
				System.out.println(student);
			}
		}
	}
}
Map集合實現(HashMap)

public class MapDemo {
	public static void main(String[] args) {
		Map<Integer, Student> map = new HashMap<Integer, Student>();
		map.put(1, new Student(1, "張三"));
		map.put(2, new Student(2, "李四"));
		map.put(3, new Student(3, "王五"));
		//需求,獲取id爲3的學生信息
		System.out.println(map.get(3));
	}
}
上面打印出來的結果是:Student [id=3, name=王五]
雖然他們都實現了相同的效果,但是,可以發現,要取到id爲3的學生,需要遍歷整個List,而Map集合就方便多了,不用遍歷,直接就可以取到。方便了很多。

    public V get(Object key) {
        Node<K,V> e;
        return (e = getNode(hash(key), key)) == null ? null : e.value;
    }
    final Node<K,V> getNode(int hash, Object key) {
        Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
        if ((tab = table) != null && (n = tab.length) > 0 &&
            (first = tab[(n - 1) & hash]) != null) {<span style="white-space:pre">			</span>//檢驗Map是否爲空。
            if (first.hash == hash && // always check first node
                ((k = first.key) == key || (key != null && key.equals(k))))<span style="white-space:pre">	</span>//從頭結點開始遍歷
                return first;
            if ((e = first.next) != null) {
                if (first instanceof TreeNode)
                    return ((TreeNode<K,V>)first).getTreeNode(hash, key);
                do {
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        return e;
                } while ((e = e.next) != null);
            }
        }
        return null;
    }
HashMap結果的遍歷:

方式一:得到Key集合,然後根據key集合依次得到value,其中keySet()方法就是將Map中的key轉換爲集合

public class MapDemo {
	public static void main(String[] args) {
		Map<Integer, Student> map = new HashMap<Integer, Student>();
		map.put(1, new Student(1, "張三"));
		map.put(2, new Student(2, "李四"));
		map.put(3, new Student(3, "王五"));
		Set<Integer> keySet = map.keySet();		//得到key集合
		for (Integer integer : keySet) {
			System.out.println(map.get(integer));
		}
	}
}
方式二:將key和value作爲一對,依次取出。

public class MapDemo {
	public static void main(String[] args) {
		Map<Integer, Student> map = new HashMap<Integer, Student>();
		map.put(1, new Student(1, "張三"));
		map.put(2, new Student(2, "李四"));
		map.put(3, new Student(3, "王五"));
		Set<Entry<Integer,Student>> entrySet = map.entrySet();	//得到Set視圖
		for (Entry<Integer, Student> entry : entrySet) {
			System.out.println(entry);
		}
	}
}
兩者方法差不多都是一樣的。

除了HashMap之外,還有TreeMap,兩者基本沒什麼區別,主要的區別在於,TreeMap是同步的,自然排序或者根據條件進行排序,和TreeSet相似。是基於紅黑樹來實現的。












發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章