在应用中碰到了要遍历HashMap的情况。总结一下。
先上图。
用cycle创建了一个Set的实例enn。
用enn的iterator()返回一个迭代器Iterator。此时的迭代器是一个Key—Value对。
所以需要Map.Entry entry = itt.next()。
一下摘自JDK1.7
public static interface Map.Entry<K,V>
A map entry (key-value pair). The Map.entrySet method returns a collection-view of the map, whose elements are of this class.
The only way to obtain a reference to a map entry is from the iterator of this collection-view. These
Map.Entry objects are valid only for the duration of the iteration; more formally, the behavior of a map entry is undefined if the backing map has been modified after the entry was returned by the iterator, except through the
setValue operation on the map entry.
可到Map.entry的实例时候,就可以用他的getKey和getValue方法分别获取一个键值对里的Key和Value了。
可到Map.entry的实例时候,就可以用他的getKey和getValue方法分别获取一个键值对里的Key和Value了。