Map用于保存具有映射关系的数据,Map集合里保存着两组值,一组用于保存Map的ley,另一组保存着Map的value。本文介绍了HashMap、TreeMap两种集合的基本使用方法和区别。
Map集合
Map接口被HashMap
和TreeMap
两个类实现。
Map接口储存一组成对的键-值对象,提供key(键)到value(值)的映射,Map中的key不要求有序,不允许重复。value同样不要求有序,但可以重复。最常见的Map实现类是HashMap,他的储存方式是哈希表,优点是查询指定元素效率高。
![](https://img-blog.csdnimg.cn/cf5d746b09fe43239af756928e9fd9c8.png)
HashMap
HashMap 是一个散列表,它存储的内容是键值对(key-value)映射。
HashMap 实现了 Map 接口,根据键的 HashCode 值存储数据,具有很快的访问速度,最多允许一条记录的键为 null,不支持线程同步。
HashMap 是无序的,即不会记录插入的顺序。
HashMap 继承于AbstractMap,实现了 Map、Cloneable、java.io.Serializable 接口。
HashMap 的 key 与 value 类型可以相同也可以不同,可以是字符串(String)类型的 key 和 value,也可以是整型(Integer)的 key 和字符串(String)类型的 value。
创建HashMap
创建一个 HashMap 对象 Sites, 整型(Integer)的 key 和字符串(String)型的 value:
public static void main(String[] args) {
HashMap<Integer, String> Sites = new HashMap<Integer, String>();
}
添加元素
HashMap 使用 put() 方法添加键值对(key-value):
public static void main(String[] args) {
// 创建 HashMap 对象 Sites
HashMap<Integer, String> Sites = new HashMap<Integer, String>();
// 添加键值对
Sites.put(1, "Google");
Sites.put(2, "Baidu");
Sites.put(3, "CSDN");
Sites.put(4, "Zhihu");
System.out.println(Sites);
}
![](https://img-blog.csdnimg.cn/4f6a4f06f24649cd9da1a16b8ea3b55c.png)
访问元素
使用 get(key) 方法可以获取 key 对应的 value:
public static void main(String[] args) {
// 创建 HashMap 对象 Sites
HashMap<Integer, String> Sites = new HashMap<Integer, String>();
// 添加键值对
Sites.put(1, "Google");
Sites.put(2, "Baidu");
Sites.put(3, "CSDN");
Sites.put(4, "Zhihu");
System.out.println(Sites.get(3));
}
![](https://img-blog.csdnimg.cn/a3657a2711184ee2b64df773c5390489.png)
删除元素
使用 remove(key) 方法可以删除 key 对应的键值对(key-value):
public static void main(String[] args) {
// 创建 HashMap 对象 Sites
HashMap<Integer, String> Sites = new HashMap<Integer, String>();
// 添加键值对
Sites.put(1, "Google");
Sites.put(2, "Baidu");
Sites.put(3, "CSDN");
Sites.put(4, "Zhihu");
Sites.remove(4);
System.out.println(Sites);
}
![](https://img-blog.csdnimg.cn/8cb0e35e2de14660ac73120390f8dede.png)
TreeMap
在Map集合框架中,除了HashMap以外,TreeMap也是常用到的集合对象之一。
与HashMap相比,TreeMap是一个能比较元素大小的Map集合,会对传入的key进行了大小排序。其中,可以使用元素的自然顺序,也可以使用集合中自定义的比较器来进行排序;
不同于HashMap的哈希映射,TreeMap实现了红黑树的结构,形成了一颗二叉树。
TreeMap的特点:
- 不允许出现重复的key;
- 可以插入null键,null值;
- 可以对元素进行排序;
- 无序集合(插入和遍历顺序不一致);
创建TreeMap
public static void main(String[] args) {
// 创建 TreeMap对象 treeMap
TreeMap<String, Integer> treeMap = new TreeMap<>();
System.out.println("初始化后,TreeMap元素个数为:" + treeMap.size());
}
![](https://img-blog.csdnimg.cn/95f0c41c5d0940418aefea464b4c6eda.png)
添加元素
public static void main(String[] args) {
TreeMap<String, Integer> treeMap = new TreeMap<>();
System.out.println("初始化后,TreeMap元素个数为:" + treeMap.size());
treeMap.put("hello", 1);
treeMap.put("world", 2);
treeMap.put(",", 3);
treeMap.put("i", 4);
treeMap.put("am", 5);
treeMap.put("a", 6);
treeMap.put("coder", 6);
System.out.println("添加元素后,TreeMap元素个数为:" + treeMap.size());
}
![](https://img-blog.csdnimg.cn/31c7bea17ae84990b7051752f4498e39.png)
访问元素
public static void main(String[] args) {
TreeMap<String, Integer> treeMap = new TreeMap<>();
System.out.println("初始化后,TreeMap元素个数为:" + treeMap.size());
treeMap.put("hello", 1);
treeMap.put("world", 2);
treeMap.put(",", 3);
treeMap.put("i", 4);
treeMap.put("am", 5);
treeMap.put("a", 6);
treeMap.put("coder", 6);
System.out.println("添加元素后,TreeMap元素个数为:" + treeMap.size());
// 访问元素
System.out.println(treeMap.get("i"));
}
![](https://img-blog.csdnimg.cn/b7a5240ac34940368658100d20f74611.png)
删除元素
public static void main(String[] args) {
TreeMap<String, Integer> treeMap = new TreeMap<>();
System.out.println("初始化后,TreeMap元素个数为:" + treeMap.size());
treeMap.put("hello", 1);
treeMap.put("world", 2);
treeMap.put(",", 3);
treeMap.put("i", 4);
treeMap.put("am", 5);
treeMap.put("a", 6);
treeMap.put("coder", 6);
System.out.println("添加元素后,TreeMap元素个数为:" + treeMap.size());
//访问元素
System.out.println(treeMap.get("i"));
//删除元素
treeMap.remove("a");
System.out.println(treeMap.get("a"));
}
![](https://img-blog.csdnimg.cn/dd2d7e79fca641e5824fee101585f485.png)
HashMap、TreeMap区别
- HashMap 适用于在Map中插入、删除和定位元素。
- Treemap 适用于按自然顺序或自定义顺序遍历键(key)。
- HashMap通常比TreeMap快一点(树和哈希表的数据结构使然),建议多使用HashMap,在需要排序的Map时候才用TreeMap。
感谢大家的耐心阅读,如有建议请私信或评论留言