Map集合
Map的概述
想要学习其他关于Java集合知识,查看我上期博客:Collection集合,List集合,Set集合
Map是一种依照键(key)存储元素的容器,键(key)很像下标,在List中下标是整数。在Map中键(key)可以使任意类型的对象。Map中不能有重复的键(Key),每个键(key)都有一个对应的值(value)。
一个键(key)和它对应的值构成map集合中的一个元素。
interface Map<K,V>
K - 此映射所维护的键的类型
V - 映射值的类型
Map中的元素是两个对象,一个对象作为键,一个对象作为值。键不可以重复,但是值可以重复。
Collection集合的格式: [元素1,元素2,元素3…]
Map集合的完整格式:{key1=value1 , key2=value2 , key3=value3 , …}
他们在Java集合的架构图中是并列的关系。
Map集合中的常用方法
map集合方法中添加键和值的方法
返回值 | 方法 名 | 作用 |
|
| 将指定的值与该映射中的指定键相关联(可选操作)。 |
|
| 将指定地图的所有映射复制到此映射(可选操作)。 |
|
| 如果指定的键尚未与某个值相关联(或映射到 |
案例:
public class Demo1 {
public static void main(String[] args) {
// 定义一个Map的容器对象
Map<String, Integer > map1 = new HashMap<>();
map1.put("张三", 20);
map1.put("李四", 19);
map1.put("王五", 12);
map1.put("赵六", 25);
System.out.println(map1); //[张三=20,李四=19,王五=12,赵六=25]
// 添加重复的键值(值不同),会返回集合中原有(重复键)的值
System.out.println(map1.put("张三", 30)); //20
Map<String, Integer> map2 = new HashMap<>();
map2.put("刘峰", 100);
map2.put("王伦", 20);
System.out.println("map2:" + map2);
// 从指定映射中将所有映射关系复制到此映射中。
map1.putAll(map2);
System.out.println("map1:" + map1);
}
}
map集合方法中删除键和值的方法
返回值 | 方法名 | 作用 |
|
| 如果存在(从可选的操作),从该地图中删除一个键的映射。 |
|
| 从该地图中删除所有的映射(可选操作)。 |
案例:
public class Demo1 {
public static void main(String[] args) {
// 定义一个Map的容器对象
Map<String, Integer > map1 = new HashMap<>();
map1.put("张三", 20);
map1.put("李四", 19);
map1.put("王五", 12);
map1.put("赵六", 25);
System.out.println(map1); //[张三=20,李四=19,王五=12,赵六=25]
System.out.println("value:" + map1.remove("java"));
map1.clear();
System.out.println("map1:" + map1);
}
}
map集合方法中获取键和值的方法
返回值 | 方法名 | 作用 |
|
| 返回到指定键所映射的值,或 |
|
| 返回此地图中键值映射的数量。 |
案例:
public class Demo2 {
public static void main(String[] args) {
// 定义一个Map的容器对象
Map<String, Integer > map1 = new HashMap<>();
map1.put("张三", 20);
map1.put("李四", 19);
map1.put("王五", 12);
map1.put("赵六", 25);
System.out.println(map1); //[张三=20,李四=19,王五=12,赵六=25]
System.out.println("value:" + map1.get("张三"));
System.out.println("map.size:" + map1.size());
}
}
map集合方法中判断的方法
返回值 | 方法名 | 作用 |
|
| 如果此地图不包含键值映射,则返回 |
|
| 如果此映射包含指定键的映射,则返回 |
|
| 如果此地图将一个或多个键映射到指定的值,则返回 |
案例:
public class Demo3 {
public static void main(String[] args) {
// 定义一个Map的容器对象
Map<String, Integer > map1 = new HashMap<>();
map1.put("张三", 20);
map1.put("李四", 19);
map1.put("王五", 12);
map1.put("赵六", 25);
System.out.println(map1); //[张三=20,李四=19,王五=12,赵六=25]
System.out.println(map1.isEmpty());
System.out.println(map1.containsKey("张三"));
System.out.println(map1.containsValue(12));
}
}
map集合遍利的方式
使用keySet遍利集合
将Map转成Set(keySet()),通过Set的迭代器取出Set集合中的每一个元素(Iterator)就是Map集合中的所有的键,再通过get方法获取键对应的值。
案例:
public class Demo2 {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "张三");
map.put(2, "李四");
map.put(3, "王五");
System.out.println(map);
Set<Integer> set = map.keySet();
Iterator<Integer> it = set.iterator();
while (it.hasNext()) {
Integer key = it.next();
String value = map.get(key);
System.out.println("key=" + key + " value=" + value);
}
}
}
使用Values获取值(但是不能获取到key的值)
案例:
public class Demo3 {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "张三");
map.put(2, "李四");
map.put(3, "王五");
System.out.println(map);
Collection<Stirng> collections = map.values();
Iterator<Integer> it = collections.iterator();
while (it.hasNext()) {
Integer key = it.next();
String value = map.get(key);
System.out.println("value=" + value);
}
}
}
使用entrySet获取
案例:
public class Demo3 {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "张三");
map.put(2, "李四");
map.put(3, "王五");
System.out.println(map);
Set<Map.Entry<Integer,String>> set = map.entrySet();
Iterator<Map.Entry<Integer,String>> it = set.iterator();
while (it.hasNext()) {
Map.Entry<Integer, String> en = it.next();
Integer key = en.getKey();
String value = en.getValue();
System.out.println("key = "+ key +"value=" + value);
}
}
}
Map集合的实现类
HashMap
底层是哈希表数据结构,线程是不同步的,可以存入null键,null值。要保证键的唯一性,需要覆盖hashCode方法,和equals方法。
TreeMap
TreeMap可以对键进行排序,通过元素的自身的比较性,容器具备的比较性(Comparator接口)
面试题:
HashMap,TreeMap,LinkedHashMap的区别?
LinkedHashMap 可以保证 HashMap 集合有序。存入的顺序和取出的顺序一致。TreeMap 实现
SortMap 接口,能够把它保存的记录根据键排序,默认是按键值的升序排序,也可以指定排序的
比较器,当用 Iterator 遍历 TreeMap 时,得到的记录是排过序的。HashMap 不保证顺序,即为
无序的,具有很快的访问速度。HashMap 最多只允许一条记录的键为 Null;允许多条记录的值为
Null;HashMap 不支持线程的同步。
HashMap和Hashtable的区别?
1.HashTable是线程安全的,HashMap是非线程安全的。
2.Hashtable不允许null值(Key和value都不可以),HashMap允许,都允许。
3.遍利的方法差不多相同,但Hashtable仅仅比HashMap多了个elements方法