Map 是一种键-值对(key-value)集合,Map 集合中的每一个元素都包含一个键对象和一个值对象。其中,键对象不允许重复,而值对象可以重复,并且值对象还可以是 Map 类型的,就像数组中的元素还可以是数组一样。
Map 接口主要有两个实现类:HashMap 类和 TreeMap 类。其中,HashMap 类按哈希算法来存取键对象,而 TreeMap 类可以对键对象进行排序。
Map接口的类继承结构图如下所示:

1.HashMap
特点:键对象不允许重复,而值对象可以重复, 键和值都可以为null,键没有排序功能。
例如:
public class Test {
public static void main(String[] args) {
Map map = new HashMap();
map.put("010","北京");
map.put("020","广州");
map.put("021","上海");
map.put("025","南京");
map.put("028","成都");
map.put("029","西安");
map.put(null,"东京");
map.put(null,null);
map.put("null","东京");
//遍历方式一:获取键的集合,再遍历值的集合。
System.out.println("---------遍历方式一--------");
Set set = map.keySet();
Iterator it = set.iterator();
while(it.hasNext()){
Object key = it.next();
Object value = map.get(key);
System.out.println(key + "=" + value);
}
//遍历方式二:遍历实体的集合
System.out.println("-----------遍历方式二---------");
Set<Map.Entry> entities = map.entrySet();
for(Map.Entry entry: entities){
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println(key + "=" + value);
}
}
}
运行结果:
---------遍历方式一--------
null=null
025=南京
null=东京
028=成都
029=西安
020=广州
010=北京
021=上海
-----------遍历方式二---------
null=null
025=南京
null=东京
028=成都
029=西安
020=广州
010=北京
021=上海
2.TreeMap
特点:键对象不允许重复,而值对象可以重复,键不能为null,但是值可以为null,键有排序功能。
例如:
public class Test {
public static void main(String[] args) {
Map map = new TreeMap();
map.put("021","上海");
map.put("020","广州");
map.put("029","西安");
map.put("010","北京");
map.put("025","南京");
map.put("028","成都");
//map.put(null,"东京"); //错误键不能为null
//map.put(null,null); //错误键不能为null
map.put("null","东京");
map.put("null",null);
//遍历方式一:获取键的集合,再遍历值的集合。
System.out.println("---------遍历方式一--------");
Set set = map.keySet();
Iterator it = set.iterator();
while(it.hasNext()){
Object key = it.next();
Object value = map.get(key);
System.out.println(key + "=" + value);
}
//遍历方式二:遍历实体的集合
System.out.println("-----------遍历方式二---------");
Set<Map.Entry> entities = map.entrySet();
for(Map.Entry entry: entities){
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println(key + "=" + value);
}
}
}
运行结果:
---------遍历方式一--------
010=北京
020=广州
021=上海
025=南京
028=成都
029=西安
null=null
-----------遍历方式二---------
010=北京
020=广州
021=上海
025=南京
028=成都
029=西安
null=null
注意:==通常Map的键都是使用String类型,由于String类默认实现了Comparable接口,所以String类型作为键的TreeMap都可以实现键的排序,但是如果键是其它对象类型,要求键元素类型必须实现Comparable接口。==
HashMap与TreeMap的比较:
1)HashMap基于哈希表实现。TreeMap基于树实现。
2)HashMap键可以为null,键不能排序,TreeMap键不可以为null,键可以排序。
3)HashMap可以通过调优初始容量和负载因子,优化HashMap空间的使用。TreeMap没有调优选项,因为该树总处于平衡状态
4)HashMap性能优于TreeMap。
3.LinkedHashMap
与LinkedHashSet类似,LinkedHashMap可以保证遍历时的有序性,即遍历顺序和插入顺序一致。
实例:
Map map = new LinkedHashMap();
map.put("010","北京");
map.put("029","西安");
map.put("021","上海");
map.put("025","南京");
map.put("028","成都");
map.put("020","广州");
map.put(null,"东京");
map.put(null,null);
map.put("null","东京");
//遍历方式一:获取键的集合,再遍历值的集合。
System.out.println("---------遍历方式一--------");
Set set = map.keySet();
Iterator it = set.iterator();
while(it.hasNext()){
Object key = it.next();
Object value = map.get(key);
System.out.println(key + "=" + value);
}
//遍历方式二:遍历实体的集合
System.out.println("-----------遍历方式二---------");
Set<Map.Entry> entities = map.entrySet();
for(Map.Entry entry: entities){
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println(key + "=" + value);
}
运行结果: ---------遍历方式一-------- 010=北京 029=西安 021=上海 025=南京 028=成都 020=广州 null=null null=东京 -----------遍历方式二--------- 010=北京 029=西安 021=上海 025=南京 028=成都 020=广州 null=null null=东京
小结:
Map 接口主要有两个实现类:HashMap 类和 TreeMap 类。其中,HashMap 类按哈希表来存取键对象,而 TreeMap 类基于树实现,可以对键对象进行排序。LinkedHashMap遍历时是有序的。