java集合框架之Map详解——Map接口

  • 接口定义

  • public interface Map<K,V>

JDK1.7及之前定义的接口数为14个,分别为

    int size();

    boolean isEmpty();

    boolean containsKey(Object key);

    boolean containsValue(Object value);

    V get(Object key);

    V put(K key, V value);

    V remove(Object key);

    void putAll(Map<? extends K, ? extends V> m);

    void clear();

    Set<K> keySet();

    Collection<V> values();

    Set<Map.Entry<K, V>> entrySet();

    boolean equals(Object o);

    int hashCode();


可以看到,基本涵盖了元素的增删改查,存在性判断,另外对key,value,entrySet的获取,也做了定义。
  •  内部接口Entry

  • interface Entry<K,V>

该接口定义的接口方法如下

        K getKey();

        V getValue();

        V setValue(V value);

        boolean equals(Object o);

        int hashCode();

为什么没有setKey的方法,因为key,只能新增或删除,不然会影响到hashCode的大小判断。

代码的注释中有这么一段

* The behavior of a map is not specified if the value of an object is
* changed in a manner that affects <tt>equals</tt> comparisons while the
* object is a key in the map

意思就是说,当一个object是map的key的时候,改变key的值,以至于影响到了equals的判断的操作,是不行的

发布了5 篇原创文章 · 获赞 0 · 访问量 205

猜你喜欢

转载自blog.csdn.net/liwei19911215/article/details/104257184