黑马程序员技术交流社区
标题: 有点小疑问 [打印本页]
作者: 高照 时间: 2012-10-3 00:27
标题: 有点小疑问
本帖最后由 高照 于 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)不是同种类型
}
作者: 黄小贝 时间: 2012-10-3 02:48
本帖最后由 黄小贝 于 2012-10-3 02:55 编辑
Map是一个接口~~Entry是Map接口里面的一个内部接口~~代表着Map里面的元素~~API里面找不到就去JDK源码里面找嘛~~
Map.Entry和Entry 我都试了下,都可以,前面加上Map. 应该是想告诉读者这个是Map里面的Entry接口~增加阅读性~- interface Entry<K,V> {
- /**
- * Returns the key corresponding to this entry.
- *
- * @return the key corresponding to this entry
- * @throws IllegalStateException implementations may, but are not
- * required to, throw this exception if the entry has been
- * removed from the backing map.
- */
- K getKey();
- /**
- * Returns the value corresponding to this entry. If the mapping
- * has been removed from the backing map (by the iterator's
- * <tt>remove</tt> operation), the results of this call are undefined.
- *
- * @return the value corresponding to this entry
- * @throws IllegalStateException implementations may, but are not
- * required to, throw this exception if the entry has been
- * removed from the backing map.
- */
- V getValue();
- /**
- * Replaces the value corresponding to this entry with the specified
- * value (optional operation). (Writes through to the map.) The
- * behavior of this call is undefined if the mapping has already been
- * removed from the map (by the iterator's <tt>remove</tt> operation).
- *
- * @param value new value to be stored in this entry
- * @return old value corresponding to the entry
- * @throws UnsupportedOperationException if the <tt>put</tt> operation
- * is not supported by the backing map
- * @throws ClassCastException if the class of the specified value
- * prevents it from being stored in the backing map
- * @throws NullPointerException if the backing map does not permit
- * null values, and the specified value is null
- * @throws IllegalArgumentException if some property of this value
- * prevents it from being stored in the backing map
- * @throws IllegalStateException implementations may, but are not
- * required to, throw this exception if the entry has been
- * removed from the backing map.
- */
- V setValue(V value);
- /**
- * Compares the specified object with this entry for equality.
- * Returns <tt>true</tt> if the given object is also a map entry and
- * the two entries represent the same mapping. More formally, two
- * entries <tt>e1</tt> and <tt>e2</tt> represent the same mapping
- * if<pre>
- * (e1.getKey()==null ?
- * e2.getKey()==null : e1.getKey().equals(e2.getKey())) &&
- * (e1.getValue()==null ?
- * e2.getValue()==null : e1.getValue().equals(e2.getValue()))
- * </pre>
- * This ensures that the <tt>equals</tt> method works properly across
- * different implementations of the <tt>Map.Entry</tt> interface.
- *
- * @param o object to be compared for equality with this map entry
- * @return <tt>true</tt> if the specified object is equal to this map
- * entry
- */
- boolean equals(Object o);
- /**
- * Returns the hash code value for this map entry. The hash code
- * of a map entry <tt>e</tt> is defined to be: <pre>
- * (e.getKey()==null ? 0 : e.getKey().hashCode()) ^
- * (e.getValue()==null ? 0 : e.getValue().hashCode())
- * </pre>
- * This ensures that <tt>e1.equals(e2)</tt> implies that
- * <tt>e1.hashCode()==e2.hashCode()</tt> for any two Entries
- * <tt>e1</tt> and <tt>e2</tt>, as required by the general
- * contract of <tt>Object.hashCode</tt>.
- *
- * @return the hash code value for this map entry
- * @see Object#hashCode()
- * @see Object#equals(Object)
- * @see #equals(Object)
- */
- int hashCode();
- }
- // Comparison and hashing
- /**
- * Compares the specified object with this map for equality. Returns
- * <tt>true</tt> if the given object is also a map and the two maps
- * represent the same mappings. More formally, two maps <tt>m1</tt> and
- * <tt>m2</tt> represent the same mappings if
- * <tt>m1.entrySet().equals(m2.entrySet())</tt>. This ensures that the
- * <tt>equals</tt> method works properly across different implementations
- * of the <tt>Map</tt> interface.
- *
- * @param o object to be compared for equality with this map
- * @return <tt>true</tt> if the specified object is equal to this map
- */
- boolean equals(Object o);
- /**
- * Returns the hash code value for this map. The hash code of a map is
- * defined to be the sum of the hash codes of each entry in the map's
- * <tt>entrySet()</tt> view. This ensures that <tt>m1.equals(m2)</tt>
- * implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps
- * <tt>m1</tt> and <tt>m2</tt>, as required by the general contract of
- * {@link Object#hashCode}.
- *
- * @return the hash code value for this map
- * @see Map.Entry#hashCode()
- * @see Object#equals(Object)
- * @see #equals(Object)
- */
- int hashCode();
- }
复制代码 Map的实现类里面一般都实现了Entry这个接口~比如说HashMap- static class Entry<K,V> implements Map.Entry<K,V> {
- final K key;
- V value;
- Entry<K,V> next;
- final int hash;
- /**
- * Creates new entry.
- */
- Entry(int h, K k, V v, Entry<K,V> n) {
- value = v;
- next = n;
- key = k;
- hash = h;
- }
- public final K getKey() {
- return key;
- }
- public final V getValue() {
- return value;
- }
- public final V setValue(V newValue) {
- V oldValue = value;
- value = newValue;
- return oldValue;
- }
- public final boolean equals(Object o) {
- if (!(o instanceof Map.Entry))
- return false;
- Map.Entry e = (Map.Entry)o;
- Object k1 = getKey();
- Object k2 = e.getKey();
- if (k1 == k2 || (k1 != null && k1.equals(k2))) {
- Object v1 = getValue();
- Object v2 = e.getValue();
- if (v1 == v2 || (v1 != null && v1.equals(v2)))
- return true;
- }
- return false;
- }
- public final int hashCode() {
- return (key==null ? 0 : key.hashCode()) ^
- (value==null ? 0 : value.hashCode());
- }
- public final String toString() {
- return getKey() + "=" + getValue();
- }
- /**
- * This method is invoked whenever the value in an entry is
- * overwritten by an invocation of put(k,v) for a key k that's already
- * in the HashMap.
- */
- void recordAccess(HashMap<K,V> m) {
- }
- /**
- * This method is invoked whenever the entry is
- * removed from the table.
- */
- void recordRemoval(HashMap<K,V> m) {
- }
- }
复制代码
作者: 李玉生 时间: 2012-10-3 08:41
顶层的哥们是个新手,楼上的哥们你给他一大堆代码,我想对他没太多帮助
作者: 李玉生 时间: 2012-10-3 08:45
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版本的,就有这个
作者: 高照 时间: 2012-10-3 09:28
李玉生 发表于 2012-10-3 08:45
java.util
接口 Map.Entry
所有已知实现类:
我说用API文档找不到Entry类。
作者: 徐-星星 时间: 2012-10-3 10:12
本帖最后由 娇赛赛 于 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接口.
作者: 李玉生 时间: 2012-10-3 13:43
大家一起来解决问题,真好
作者: 高照 时间: 2012-10-3 19:51
谢谢啊,有点明白了
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |