Map
一个将键映射到值的对象,一个映射不能包含重复的键,每个健最多映射到一个值
Map接口的一些共性方法
添加
put(K key, V value)
putAll(Map<? extends K, ? extends V> m)删除
clear();//清空
remove(Object key)判断
containsValue(Object value)
containsKey(Object key)
isEmpty()获取
get(Object key);
size()
values()
entrySet()
keySet();
HashMap
底层是哈希表数据结构,允许使用Null键和Null值,且该集合是不同步的,所以效率高
HashTable
底层是哈希表的数据结构,不允许存入null健null值,该集合是同步的,所以效率低
TreeMap
底层是二叉树数据结构,线程不同步,可以用于map集合中的健进行排序
增、删、判断的操作
public class Test {
public static void main(String[] args) {
Map<String,String> map = new HashMap();
map.put("001","lisi");
map.put("002","zhangsan");
map.put("003","wanger");
System.out.println(map);
System.out.println(map.containsValue("lisi"));
System.out.println(map.containsKey("001"));
map.remove("001");
System.out.println(map);
}
}
在增里面,有一个特殊点就是,map不能存两个相同的值,即不能存在两个相等的 key 值
V put(K key, V value); //map接口里面put方法的源码。我们可看到,它返回了一个值,一个value值
即,每当存入一个key时,都会返回一个value值,而这个value值就是这个原来key对应的value值,如果这个key值第一次添加进来,那么那么key原来对应的value值就是null了,但是如果存在key了那么就会返回之前的value值
public class Test {
public static void main(String[] args) {
Map<String,String> map = new HashMap();
System.out.println(map.put("001","lisi01"));
System.out.println(map.put("001","lisi02"));
System.out.println(map);
}
}
我们可以看到,第一次返回输出的是null,而第二次就是第一次放入的值了
获取方法get(Object key)、size()、values()
public class Test {
public static void main(String[] args) {
Map<String,String> map = new HashMap();
map.put("001","lisi");
map.put("002","zhangsan");
map.put("003","wanger");
System.out.println(map.get("002"));
System.out.println(map.size());
Collection<String> coll = map.values();
for(String str :coll){
System.out.println(str);
}
}
}
我们可以知道values返回的是一个Collection的类型,就把map里面所有的value值都装进去了
获取方法keySet()
在map里面是没有迭代器的,但是给我们提供了一个keySet()方法
Set<K> keySet();
这个方法的返回值是一个Set,所以通过keySet()方法就可以获取所有健的Set集合
public class Test {
public static void main(String[] args) {
Map<String,String> map = new HashMap();
map.put("001","lisi");
map.put("002","zhangsan");
map.put("003","wanger");
Set<String> keySet = map.keySet();
Iterator<String> it = keySet.iterator();
while (it.hasNext()){
String key = it.next();
System.out.println(key + "=" + map.get(key));
}
}
}
Map集合取出的原理:将map集合转换成Set集合,再通过迭代器取
这个时候,我们就通过keySet()获取到了map的所有key值,这个是否就方便我们对map进行具体的操作了,而不再是只能便利map,而是可以便利获得里面具体的值,且能操作
获取方法entrySet()
返回此映射中包含映射关系的Set视图
在这里我们又引出了一个Map.Entry<K,V>,它是Map的一个内部静态接口,源码如下,它提供了我们熟悉的 getKey(); getValue() 方法,这样方便我们更容易的获取到key value的值
interface Entry<K,V> {
K getKey();
V getValue();
V setValue(V value);
boolean equals(Object o);
int hashCode();
public static <K extends Comparable<? super K>, V> Comparator<Map.Entry<K,V>> comparingByKey() {
return (Comparator<Map.Entry<K, V>> & Serializable)
(c1, c2) -> c1.getKey().compareTo(c2.getKey());
}
public static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> comparingByValue() {
return (Comparator<Map.Entry<K, V>> & Serializable)
(c1, c2) -> c1.getValue().compareTo(c2.getValue());
}
public static <K, V> Comparator<Map.Entry<K, V>> comparingByKey(Comparator<? super K> cmp) {
Objects.requireNonNull(cmp);
return (Comparator<Map.Entry<K, V>> & Serializable)
(c1, c2) -> cmp.compare(c1.getKey(), c2.getKey());
}
public static <K, V> Comparator<Map.Entry<K, V>> comparingByValue(Comparator<? super V> cmp) {
Objects.requireNonNull(cmp);
return (Comparator<Map.Entry<K, V>> & Serializable)
(c1, c2) -> cmp.compare(c1.getValue(), c2.getValue());
}
}
public class Test {
public static void main(String[] args) {
Map<String, String> map = new HashMap();
map.put("001", "lisi");
map.put("002", "zhangsan");
map.put("003", "wanger");
Set<Map.Entry<String, String>> entries = map.entrySet();
Iterator<Map.Entry<String, String>> it = entries.iterator();
while (it.hasNext()){
Map.Entry<String,String> me = it.next();
String key = me.getKey();
String value = me.getValue();
System.out.println(key + "=" + value);
}
}
}
这样我们,依然可以对map进行遍历,获取具体的值