A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 高照 中级黑马   /  2012-10-3 00:27  /  1692 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 高照 于 2012-10-3 19:52 编辑

Map map=new HashMap();
Set set-=map.entrySet();
Interor it=set.interor();
while(it.hasNext()){
   Map.Entry entry=(Entry)it.next();//Map.Entry 类和Entry有什么区别
                                    //为什么在API文档中找不到类Entry
                                    //在我们以前学强制类型转换时左右
                                    //两边的类型不都一样吗
                                    //例如String str=(String)obj;
                                    //为什么Map.Entry和(Entry)不是同种类型
}

评分

参与人数 1技术分 +1 收起 理由
唐志兵 + 1 赞一个!

查看全部评分

7 个回复

倒序浏览
本帖最后由 黄小贝 于 2012-10-3 02:55 编辑

Map是一个接口~~Entry是Map接口里面的一个内部接口~~代表着Map里面的元素~~API里面找不到就去JDK源码里面找嘛~~

Map.Entry和Entry 我都试了下,都可以,前面加上Map. 应该是想告诉读者这个是Map里面的Entry接口~增加阅读性~
  1. interface Entry<K,V> {
  2.             /**
  3.          * Returns the key corresponding to this entry.
  4.          *
  5.          * @return the key corresponding to this entry
  6.          * @throws IllegalStateException implementations may, but are not
  7.          *         required to, throw this exception if the entry has been
  8.          *         removed from the backing map.
  9.          */
  10.         K getKey();

  11.             /**
  12.          * Returns the value corresponding to this entry.  If the mapping
  13.          * has been removed from the backing map (by the iterator's
  14.          * <tt>remove</tt> operation), the results of this call are undefined.
  15.          *
  16.          * @return the value corresponding to this entry
  17.          * @throws IllegalStateException implementations may, but are not
  18.          *         required to, throw this exception if the entry has been
  19.          *         removed from the backing map.
  20.          */
  21.         V getValue();

  22.             /**
  23.          * Replaces the value corresponding to this entry with the specified
  24.          * value (optional operation).  (Writes through to the map.)  The
  25.          * behavior of this call is undefined if the mapping has already been
  26.          * removed from the map (by the iterator's <tt>remove</tt> operation).
  27.          *
  28.          * @param value new value to be stored in this entry
  29.          * @return old value corresponding to the entry
  30.          * @throws UnsupportedOperationException if the <tt>put</tt> operation
  31.          *         is not supported by the backing map
  32.          * @throws ClassCastException if the class of the specified value
  33.          *         prevents it from being stored in the backing map
  34.          * @throws NullPointerException if the backing map does not permit
  35.          *         null values, and the specified value is null
  36.          * @throws IllegalArgumentException if some property of this value
  37.          *         prevents it from being stored in the backing map
  38.          * @throws IllegalStateException implementations may, but are not
  39.          *         required to, throw this exception if the entry has been
  40.          *         removed from the backing map.
  41.          */
  42.         V setValue(V value);

  43.         /**
  44.          * Compares the specified object with this entry for equality.
  45.          * Returns <tt>true</tt> if the given object is also a map entry and
  46.          * the two entries represent the same mapping.  More formally, two
  47.          * entries <tt>e1</tt> and <tt>e2</tt> represent the same mapping
  48.          * if<pre>
  49.          *     (e1.getKey()==null ?
  50.          *      e2.getKey()==null : e1.getKey().equals(e2.getKey()))  &&
  51.          *     (e1.getValue()==null ?
  52.          *      e2.getValue()==null : e1.getValue().equals(e2.getValue()))
  53.          * </pre>
  54.          * This ensures that the <tt>equals</tt> method works properly across
  55.          * different implementations of the <tt>Map.Entry</tt> interface.
  56.          *
  57.          * @param o object to be compared for equality with this map entry
  58.          * @return <tt>true</tt> if the specified object is equal to this map
  59.          *         entry
  60.          */
  61.         boolean equals(Object o);

  62.         /**
  63.          * Returns the hash code value for this map entry.  The hash code
  64.          * of a map entry <tt>e</tt> is defined to be: <pre>
  65.          *     (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
  66.          *     (e.getValue()==null ? 0 : e.getValue().hashCode())
  67.          * </pre>
  68.          * This ensures that <tt>e1.equals(e2)</tt> implies that
  69.          * <tt>e1.hashCode()==e2.hashCode()</tt> for any two Entries
  70.          * <tt>e1</tt> and <tt>e2</tt>, as required by the general
  71.          * contract of <tt>Object.hashCode</tt>.
  72.          *
  73.          * @return the hash code value for this map entry
  74.          * @see Object#hashCode()
  75.          * @see Object#equals(Object)
  76.          * @see #equals(Object)
  77.          */
  78.         int hashCode();
  79.     }

  80.     // Comparison and hashing

  81.     /**
  82.      * Compares the specified object with this map for equality.  Returns
  83.      * <tt>true</tt> if the given object is also a map and the two maps
  84.      * represent the same mappings.  More formally, two maps <tt>m1</tt> and
  85.      * <tt>m2</tt> represent the same mappings if
  86.      * <tt>m1.entrySet().equals(m2.entrySet())</tt>.  This ensures that the
  87.      * <tt>equals</tt> method works properly across different implementations
  88.      * of the <tt>Map</tt> interface.
  89.      *
  90.      * @param o object to be compared for equality with this map
  91.      * @return <tt>true</tt> if the specified object is equal to this map
  92.      */
  93.     boolean equals(Object o);

  94.     /**
  95.      * Returns the hash code value for this map.  The hash code of a map is
  96.      * defined to be the sum of the hash codes of each entry in the map's
  97.      * <tt>entrySet()</tt> view.  This ensures that <tt>m1.equals(m2)</tt>
  98.      * implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps
  99.      * <tt>m1</tt> and <tt>m2</tt>, as required by the general contract of
  100.      * {@link Object#hashCode}.
  101.      *
  102.      * @return the hash code value for this map
  103.      * @see Map.Entry#hashCode()
  104.      * @see Object#equals(Object)
  105.      * @see #equals(Object)
  106.      */
  107.     int hashCode();
  108. }
复制代码
Map的实现类里面一般都实现了Entry这个接口~比如说HashMap
  1. static class Entry<K,V> implements Map.Entry<K,V> {
  2.         final K key;
  3.         V value;
  4.         Entry<K,V> next;
  5.         final int hash;

  6.         /**
  7.          * Creates new entry.
  8.          */
  9.         Entry(int h, K k, V v, Entry<K,V> n) {
  10.             value = v;
  11.             next = n;
  12.             key = k;
  13.             hash = h;
  14.         }

  15.         public final K getKey() {
  16.             return key;
  17.         }

  18.         public final V getValue() {
  19.             return value;
  20.         }

  21.         public final V setValue(V newValue) {
  22.             V oldValue = value;
  23.             value = newValue;
  24.             return oldValue;
  25.         }

  26.         public final boolean equals(Object o) {
  27.             if (!(o instanceof Map.Entry))
  28.                 return false;
  29.             Map.Entry e = (Map.Entry)o;
  30.             Object k1 = getKey();
  31.             Object k2 = e.getKey();
  32.             if (k1 == k2 || (k1 != null && k1.equals(k2))) {
  33.                 Object v1 = getValue();
  34.                 Object v2 = e.getValue();
  35.                 if (v1 == v2 || (v1 != null && v1.equals(v2)))
  36.                     return true;
  37.             }
  38.             return false;
  39.         }

  40.         public final int hashCode() {
  41.             return (key==null   ? 0 : key.hashCode()) ^
  42.                    (value==null ? 0 : value.hashCode());
  43.         }

  44.         public final String toString() {
  45.             return getKey() + "=" + getValue();
  46.         }

  47.         /**
  48.          * This method is invoked whenever the value in an entry is
  49.          * overwritten by an invocation of put(k,v) for a key k that's already
  50.          * in the HashMap.
  51.          */
  52.         void recordAccess(HashMap<K,V> m) {
  53.         }

  54.         /**
  55.          * This method is invoked whenever the entry is
  56.          * removed from the table.
  57.          */
  58.         void recordRemoval(HashMap<K,V> m) {
  59.         }
  60.     }
复制代码

评分

参与人数 1技术分 +1 收起 理由
唐志兵 + 1 很给力!

查看全部评分

回复 使用道具 举报
顶层的哥们是个新手,楼上的哥们你给他一大堆代码,我想对他没太多帮助
回复 使用道具 举报
java.util
接口 Map.Entry<K,V>
所有已知实现类:
AbstractMap.SimpleEntry, AbstractMap.SimpleImmutableEntry
正在封闭接口:
Map<K,V>

--------------------------------------------------------------------------------

public static interface Map.Entry<K,V>映射项(键-值对)。Map.entrySet 方法返回映射的 collection 视图,其中的元素属于此类。获得映射项引用的唯一 方法是通过此 collection 视图的迭代器来实现。这些 Map.Entry 对象仅 在迭代期间有效;更确切地讲,如果在迭代器返回项之后修改了底层映射,则某些映射项的行为是不确定的,除了通过 setValue 在映射项上执行操作之外。



从以下版本开始:
1.2
另请参见:
Map.entrySet()
顶楼的哥们,你是不是查的api是老版本的啊,我的是1.6版本的,就有这个

评分

参与人数 1技术分 +1 收起 理由
唐志兵 + 1 赞一个!

查看全部评分

回复 使用道具 举报
李玉生 发表于 2012-10-3 08:45
java.util
接口 Map.Entry
所有已知实现类:

我说用API文档找不到Entry类。
回复 使用道具 举报
本帖最后由 娇赛赛 于 2012-10-3 10:13 编辑

Map.Entry是Map内部定义的一个接口,专门用来保存key→value的内容。Map.Entry的定义如下:
public static interface Map.Entry<K,V>
Map.Entry是使用static关键字声明的内部接口,此接口可以由外部通过"外部类.内部类"的形式直接调用。
从之前的内容可以知道,在Map的操作中,所有的内容都是通过key→value的形式保存数据的,那么对于集合来讲,实际上是将key→value的数据保存在了Map.Entry的实例之后,再在Map集合中插入的是一个Map.Entry的实例化对象,如下图所示:

在一般的Map操作中(例如,增加或取出数据等操作)不用去管Map.Entry接口,但是在将Map中的数据全部输出时就必须使用Map.Entry接口.

评分

参与人数 1技术分 +1 收起 理由
唐志兵 + 1 赞一个!

查看全部评分

回复 使用道具 举报
大家一起来解决问题,真好
回复 使用道具 举报
高照 中级黑马 2012-10-3 19:51:36
8#
谢谢啊,有点明白了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马