集合框架AbstractMap

AbstractMap:

 

 

AbstractMap

 

 

        AbstractMap继承了Map,但没有实现entrySet()方法(该方法还是abstract修饰),如果要继承AbstractMap,需要自己实现entrySet()方法。没有真正实现put(K key, V value)方法,这里“没有真正实现”的意思是,该方法在形式上已经实现了,即没有用abstract修饰了,但是方法内部仅仅是抛出个异常,并没有真正实现方法体内容,从下面的源码中可以看到。

 

     此类提供 Map 接口的骨干实现,以最大限度地减少实现此接口所需的工作。

       要实现不可修改的映射,编程人员只需扩展此类并提供 entrySet 方法的实现即可,该方法将返回映射的映射关系 set 视图。通常,返回的 set 将依次在AbstractSet 上实现。此 set 不支持add 或 remove 方法,其迭代器也不支持 remove 方法。

      要实现可修改的映射,编程人员必须另外重写此类的 put 方法(否则将抛出 UnsupportedOperationException),entrySet().iterator() 返回的迭代器也必须另外实现其remove 方法。

     按照 Map 接口规范中的建议,编程人员通常应该提供一个 void(无参数)构造方法和 map 构造方法。

     此类中每个非抽象方法的文档详细描述了其实现。如果要实现的映射允许更有效的实现,则可以重写所有这些方法

public abstract class AbstractMap<K,V> implements Map<K,V>


AbstractMap源码:

 

 
  1. //抽象类

  2. public abstract class AbstractMap<K,V> implements Map<K,V> {

  3. /**

  4. * 空构造函数

  5. */

  6. protected AbstractMap() {

  7. }

  8.  
  9. /*----------查询操作--------------------*/

  10.  
  11. public int size() {

  12. return entrySet().size();

  13. }

  14.  
  15.  
  16. public boolean isEmpty() {

  17. return size() == 0;

  18. }

  19.  
  20. // 如果此映射将一个或多个键映射到指定值,则返回 true。

  21. public boolean containsValue(Object value) {

  22. Iterator<Entry<K,V>> i = entrySet().iterator();//entrySet要自己实现

  23. if (value==null) {

  24. while (i.hasNext()) {

  25. Entry<K,V> e = i.next();

  26. if (e.getValue()==null)

  27. return true;

  28. }

  29. } else {

  30. while (i.hasNext()) {

  31. Entry<K,V> e = i.next();

  32. if (value.equals(e.getValue()))

  33. return true;

  34. }

  35. }

  36. return false;

  37. }

  38.  
  39. //如果此映射包含指定键的映射关系,则返回 true。

  40. public boolean containsKey(Object key) {

  41. Iterator<Map.Entry<K,V>> i = entrySet().iterator();

  42. if (key==null) {

  43. while (i.hasNext()) {

  44. Entry<K,V> e = i.next();

  45. if (e.getKey()==null)

  46. return true;

  47. }

  48. } else {

  49. while (i.hasNext()) {

  50. Entry<K,V> e = i.next();

  51. if (key.equals(e.getKey()))

  52. return true;

  53. }

  54. }

  55. return false;

  56. }

  57.  
  58. //返回指定键所映射的值;如果此映射不包含该键的映射关系,则返回 null。

  59. public V get(Object key) {

  60. Iterator<Entry<K,V>> i = entrySet().iterator();

  61. if (key==null) {

  62. while (i.hasNext()) {

  63. Entry<K,V> e = i.next();

  64. if (e.getKey()==null) //AbstractMap 的键值对可以为NULL

  65. return e.getValue();

  66. }

  67. } else {

  68. while (i.hasNext()) {

  69. Entry<K,V> e = i.next();

  70. if (key.equals(e.getKey()))

  71. return e.getValue();

  72. }

  73. }

  74. return null;

  75. }

  76.  
  77.  
  78. /*----------修改操作--------------------*/

  79.  
  80. //将指定的值与此映射中的指定键关联

  81. //要实现可修改的映射,必须另外重写此类的 put 方法

  82. public V put(K key, V value) {

  83. throw new UnsupportedOperationException();

  84. }

  85.  
  86. //如果存在一个键的映射关系,则将其从此映射中移除

  87. public V remove(Object key) {

  88. Iterator<Entry<K,V>> i = entrySet().iterator();

  89. Entry<K,V> correctEntry = null;

  90. if (key==null) {

  91. while (correctEntry==null && i.hasNext()) {

  92. Entry<K,V> e = i.next();

  93. if (e.getKey()==null)

  94. correctEntry = e;

  95. }

  96. } else {

  97. while (correctEntry==null && i.hasNext()) {

  98. Entry<K,V> e = i.next();

  99. if (key.equals(e.getKey()))

  100. correctEntry = e;

  101. }

  102. }

  103.  
  104. V oldValue = null;

  105. if (correctEntry !=null) {

  106. oldValue = correctEntry.getValue();

  107. i.remove();

  108. }

  109. return oldValue;

  110. }

  111.  
  112.  
  113. /*----------容量操作--------------------*/

  114.  
  115.  
  116. public void putAll(Map<? extends K, ? extends V> m) {

  117. for (Map.Entry<? extends K, ? extends V> e : m.entrySet())

  118. put(e.getKey(), e.getValue());

  119. }

  120.  
  121.  
  122. public void clear() {

  123. entrySet().clear();

  124. }

  125.  
  126.  
  127. /*----------视图--------------------*/

  128.  
  129.  
  130. transient volatile Set<K> keySet = null;

  131. transient volatile Collection<V> values = null;

  132.  
  133.  
  134. public Set<K> keySet() {

  135. if (keySet == null) {

  136. keySet = new AbstractSet<K>() {

  137. public Iterator<K> iterator() {

  138. return new Iterator<K>() {

  139. private Iterator<Entry<K,V>> i = entrySet().iterator();

  140.  
  141. public boolean hasNext() {

  142. return i.hasNext();

  143. }

  144.  
  145. public K next() {

  146. return i.next().getKey();

  147. }

  148.  
  149. public void remove() {

  150. i.remove();

  151. }

  152. };

  153. }

  154.  
  155. public int size() {

  156. return AbstractMap.this.size();

  157. }

  158.  
  159. public boolean isEmpty() {

  160. return AbstractMap.this.isEmpty();

  161. }

  162.  
  163. public void clear() {

  164. AbstractMap.this.clear();

  165. }

  166.  
  167. public boolean contains(Object k) {

  168. return AbstractMap.this.containsKey(k);

  169. }

  170. };

  171. }

  172. return keySet;

  173. }

  174.  
  175.  
  176. public Collection<V> values() {

  177. if (values == null) {

  178. values = new AbstractCollection<V>() {

  179. public Iterator<V> iterator() {

  180. return new Iterator<V>() {

  181. private Iterator<Entry<K,V>> i = entrySet().iterator();

  182.  
  183. public boolean hasNext() {

  184. return i.hasNext();

  185. }

  186.  
  187. public V next() {

  188. return i.next().getValue();

  189. }

  190.  
  191. public void remove() {

  192. i.remove();

  193. }

  194. };

  195. }

  196.  
  197. public int size() {

  198. return AbstractMap.this.size();

  199. }

  200.  
  201. public boolean isEmpty() {

  202. return AbstractMap.this.isEmpty();

  203. }

  204.  
  205. public void clear() {

  206. AbstractMap.this.clear();

  207. }

  208.  
  209. public boolean contains(Object v) {

  210. return AbstractMap.this.containsValue(v);

  211. }

  212. };

  213. }

  214. return values;

  215. }

  216.  
  217. public abstract Set<Entry<K,V>> entrySet();

  218.  
  219.  
  220. /*----------比较和散列-------------------*/

  221.  
  222.  
  223. public boolean equals(Object o) {

  224. if (o == this)

  225. return true;

  226.  
  227. if (!(o instanceof Map))

  228. return false;

  229. Map<K,V> m = (Map<K,V>) o;

  230. if (m.size() != size())

  231. return false;

  232.  
  233. try {

  234. Iterator<Entry<K,V>> i = entrySet().iterator();

  235. while (i.hasNext()) {

  236. Entry<K,V> e = i.next();

  237. K key = e.getKey();

  238. V value = e.getValue();

  239. if (value == null) {

  240. if (!(m.get(key)==null && m.containsKey(key)))

  241. return false;

  242. } else {

  243. if (!value.equals(m.get(key)))

  244. return false;

  245. }

  246. }

  247. } catch (ClassCastException unused) {

  248. return false;

  249. } catch (NullPointerException unused) {

  250. return false;

  251. }

  252.  
  253. return true;

  254. }

  255.  
  256. //Map<K,V>的hash值为每个映射的hash总和

  257. public int hashCode() {

  258. int h = 0;

  259. Iterator<Entry<K,V>> i = entrySet().iterator();

  260. while (i.hasNext())

  261. h += i.next().hashCode();

  262. return h;

  263. }

  264.  
  265.  
  266. public String toString() {

  267. Iterator<Entry<K,V>> i = entrySet().iterator();

  268. if (! i.hasNext())

  269. return "{}";

  270.  
  271. StringBuilder sb = new StringBuilder();

  272. sb.append('{');

  273. for (;;) {

  274. Entry<K,V> e = i.next();

  275. K key = e.getKey();

  276. V value = e.getValue();

  277. sb.append(key == this ? "(this Map)" : key);

  278. sb.append('=');

  279. sb.append(value == this ? "(this Map)" : value);

  280. if (! i.hasNext())

  281. return sb.append('}').toString();

  282. sb.append(',').append(' ');

  283. }

  284. }

  285.  
  286. //返回此 AbstractMap 实例的浅表副本:不复制键和值本身。

  287. protected Object clone() throws CloneNotSupportedException {

  288. AbstractMap<K,V> result = (AbstractMap<K,V>)super.clone();

  289. result.keySet = null;

  290. result.values = null;

  291. return result;

  292. }

  293.  
  294.  
  295. private static boolean eq(Object o1, Object o2) {

  296. return o1 == null ? o2 == null : o1.equals(o2);

  297. }

  298.  
  299. /*SimpleEntry实现了Map类中的Entry接口,

  300. 另外也实现了Serializable接口,可序列化

  301. */

  302. public static class SimpleEntry<K,V>

  303. implements Entry<K,V>, java.io.Serializable

  304. {

  305. private static final long serialVersionUID = -8499721149061103585L;

  306.  
  307. private final K key;

  308. private V value;

  309.  
  310.  
  311. public SimpleEntry(K key, V value) {

  312. this.key = key;

  313. this.value = value;

  314. }

  315.  
  316.  
  317. public SimpleEntry(Entry<? extends K, ? extends V> entry) {

  318. this.key = entry.getKey();

  319. this.value = entry.getValue();

  320. }

  321.  
  322.  
  323. public K getKey() {

  324. return key;

  325. }

  326.  
  327.  
  328. public V getValue() {

  329. return value;

  330. }

  331.  
  332.  
  333. public V setValue(V value) {

  334. V oldValue = this.value;

  335. this.value = value;

  336. return oldValue;

  337. }

  338.  
  339.  
  340. public boolean equals(Object o) {

  341. if (!(o instanceof Map.Entry))

  342. return false;

  343. Map.Entry e = (Map.Entry)o;

  344. return eq(key, e.getKey()) && eq(value, e.getValue());

  345. }

  346.  
  347.  
  348. public int hashCode() {

  349. return (key == null ? 0 : key.hashCode()) ^

  350. (value == null ? 0 : value.hashCode());

  351. }

  352.  
  353.  
  354. public String toString() {

  355. return key + "=" + value;

  356. }

  357.  
  358. }

  359.  
  360. /*SimpleEntry实现了Map类中的Entry接口,

  361. 另外也实现了Serializable接口,可序列化

  362. */

  363. public static class SimpleImmutableEntry<K,V>

  364. implements Entry<K,V>, java.io.Serializable

  365. {

  366. private static final long serialVersionUID = 7138329143949025153L;

  367.  
  368. private final K key;

  369. private final V value;

  370.  
  371.  
  372. public SimpleImmutableEntry(K key, V value) {

  373. this.key = key;

  374. this.value = value;

  375. }

  376.  
  377.  
  378. public SimpleImmutableEntry(Entry<? extends K, ? extends V> entry) {

  379. this.key = entry.getKey();

  380. this.value = entry.getValue();

  381. }

  382.  
  383.  
  384. public K getKey() {

  385. return key;

  386. }

  387.  
  388.  
  389. public V getValue() {

  390. return value;

  391. }

  392.  
  393.  
  394. public V setValue(V value) {

  395. throw new UnsupportedOperationException();

  396. }

  397.  
  398.  
  399. public boolean equals(Object o) {

  400. if (!(o instanceof Map.Entry))

  401. return false;

  402. Map.Entry e = (Map.Entry)o;

  403. return eq(key, e.getKey()) && eq(value, e.getValue());

  404. }

  405.  
  406.  
  407. public int hashCode() {

  408. return (key == null ? 0 : key.hashCode()) ^

  409. (value == null ? 0 : value.hashCode());

  410. }

  411.  
  412. //重写了toString方法,返回key=value形式

  413. public String toString() {

  414. return key + "=" + value;

  415. }

  416.  
  417. }

  418.  
  419. }

      从源码中可以看出,AbstractMap类提供了Map接口的主要实现,以最大限度地减少了实现此接口所需的工作,但是AbstractMap没有实现entrySet()方法。所以如果我们要实现不可修改的Map时,只需要扩展此类并提供entrySet()方法的实现即可,另外从源码中也可以看出,entrySet()方法返回的Set(即keySet)不支持add()或remove()方法。如果我们要实现可修改的Map,那么就必须另外重写put()方法,否则将抛出UnsupportedOperationException,而且entrySet.iterator()返回的迭代器i也必须实现其remove方法。

 

 

        不过一般我们不需要自己实现Map,因为已经有Map的实现类了,如HashMap和TreeMap等。


 

AbstractMap 结构:

 

 

其中:SimpleImmutableEntry<K,V>和SimpleEntry<K,V>两个嵌套类会在AbstractMap的子类中用到

 

 
  1. public static class SimpleEntry<K,V>

  2. implements Entry<K,V>, java.io.Serializable

  3.  
  4. public static class SimpleImmutableEntry<K,V>

  5. implements Entry<K,V>, java.io.Serializable


 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值