上一篇里介绍了容器家族里的大族长——Collection接口,今天来看看容器家族里的二族长——Map接口。
Map也是容器家族的一个大分支,但里面的元素都是以键值对(key-value)的形式存放的,就像字典一样,用相应的key就可以拿到相应的value。
先来看看Map接口的内容,下面是阉割版的Map接口(去掉了default method),去掉的部分涉及Stream操作,属于Map的高级用法,所以暂时不做介绍。
import java.io.Serializable; java.util.Collection; java.util.Comparator; java.util.Objects; java.util.Set; public interface Map<K,V> { // 查询操作 /** * 返回键值对数量 */ int size(); * Map是否为空 boolean isEmpty(); * Map中是否包含指定的key containsKey(Object key); * Map中是否包含指定的value containsValue(Object value); * 根据key获取对应的value */ V get(Object key); Modification Operations * 插入键值对,如果Map中已经存在该key,则新的value会覆盖原来的value V put(K key,V value); * 移除指定key对应的键值对,并返回相应的value V remove(Object key); 批量操作 * 将另一个Map中的键值对全部复制过来 void putAll(Map<? extends K,? extends V> m); * 移除所有键值对 void clear(); 视图 * 返回包含Map中所有key的(Set类型)键视图,对Map的修改也会影响到键视图 Set<K> keySet(); * 返回包含Map中所有value的(Collection类型)值视图,对Map的修改也会影响到值视图 Collection<V> values(); * 返回包含Map中所有键值对的(java.util.Map.Entry类型)键值对视图 Set<Map.Entry<K,V>> entrySet(); * Map 键值对接口 interface Entry<K,1)"> { * 返回键 K getKey(); * 返回值 V getValue(); * 设置键 V setValue(V value); equals(Object o); hashCode(); * 键比较器(内部比较器) */ static <K extends Comparable<? super K>,V> Comparator<Map.Entry<K,1)"> comparingByKey() { return (Comparator<Map.Entry<K,V>> & Serializable) (c1,c2) -> c1.getKey().compareTo(c2.getKey()); } * 值比较器(内部比较器) static <K,V super V>> Comparator<Map.Entry<K,1)"> comparingByValue() { c1.getValue().compareTo(c2.getValue()); } * 键比较器(外部比较器) super K> cmp) { Objects.requireNonNull(cmp); cmp.compare(c1.getKey(),c2.getKey()); } * 值比较器(外部比较器) super V> cmp.compare(c1.getValue(),c2.getValue()); } } 比较和散列 equals(Object o); hashCode(); }
可以看到,Map接口的内容,其实比Collection接口更丰富,这里因为省略了很多高级方法,而且里面包含了另外一个接口,Map.Entry接口,也就是一直所说的键值对,这个接口是Map中元素需要实现的接口。
Map有三种遍历方式:1.通过遍历KeySet来遍历所有键值对,2.通过遍历EntrySet来实现,3.通过EntrySet的Iterator来遍历。这里还有一个新概念——视图,视图其实就是一个集合,但是是一个不能修改的集合,只能对视图进行查询和遍历操作,在Map中一共有三个视图,键视图,值视图,键值对视图,下面可以看一个小栗子:
class Test { static main(String[] args){ Map<Integer,Integer> map = new HashMap<>(); map.put(1,11); map.put(2,22); map.put(3,33); Set<Integer> keys = map.keySet(); Collection<Integer> values = map.values(); Set<Map.Entry<Integer,Integer>> entries = map.entrySet(); Iterator<Map.Entry<Integer,Integer>> iterator = entries.iterator(); System.out.println(keys); System.out.println(values); System.out.println(entries); System.out.println("按keyset遍历"); for (Integer key : keys){ System.out.println("key:" + key + " value:" + map.get(key)); } System.out.println("按键值对遍历"for (Map.Entry<Integer,Integer> entry : entries){ System.out.println("entry:" + entry); } System.out.println("按iterator遍历"while (iterator.hasNext()){ Map.Entry<Integer,Integer> entry = iterator.next(); System.out.println("entry:" + entry); } map.put(2,444); map.put(4,44); System.out.println("修改后的视图"); System.out.println(keys); System.out.println(values); System.out.println(entries); keys.add(5); values.add(55); } }
输出如下:
[1,2,3] [11,22,1)">] [1=11,2=22,3=33] 按keyset遍历 key:1 value:11 key:2 value:22 key:3 value:33 按键值对遍历 entry:1=11 entry:2=22 entry:3=33 按iterator遍历 entry:1=11 修改后的视图 [1,3,4 at java.util.AbstractCollection.add(AbstractCollection.java:262)
at com.frank.chapter19.Test.main(Test.java:44)
栗子里介绍了三种遍历方式,也看到了三种视图的样子,当我们试图修改视图时,抛出了一个UnsupportedOperationException异常,表明该视图集合无法修改。
在Map.Entry接口里,还可以看到外部比较器和内部比较器,这两个概念暂时也不做介绍,在之后的文章里会介绍。
关于Map,要说的主要就这么多了,目前来说只需要知道Map是以键值对的形式进行存取,并了解Map接口中的主要方法及其作用,了解Map的遍历方法,和视图的概念就已经足够了。
本篇到此结束,欢迎大家继续关注。