HashTable、HashMap及TreeMap比较特点(面试可能问)
package com.map;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* HashMap:基于哈希表的 Map 接口的实现。
* 并允许使用 null 值和 null 键
* (除了非同步和允许使用 null 之外
* HashMap 类与 Hashtable 大致相同。)
* 线程不同步:
* HashTable:哈希表、任何非 null 对象都可以用作键或值、线程安全
* TreeMap:底层是二叉树,线程不同步,可以给key排序
* @author 小明
*
*Map:共性方法
*/
public class HashMapDemo {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("01", "zhangsan1");
map.put("02", "zhangsan2");
map.put("03", "zhangsan3");
map.put("04", "zhangsan4");
map.put("04", "wangwu");
System.out.println(map);
System.out.println(map.containsKey("01"));
System.out.println(map.remove("04"));
System.out.println(map);
System.out.println(map.get("01"));
map.put(null, "null");
System.out.println(map.get(null));
Collection<String> values = map.values();
//返回结果没有顺序
System.out.println(values);
}
}