[java]源码详解三种map遍历方式(二)

2014-11-24 10:57:54 · 作者: · 浏览: 1
es遍历
*
* Collection java.util.Map.values()
*
*
* Returns a Collection view of the values contained in this map. The
* collection is backed by the map, so changes to the map are reflected in
* the collection, and vice-versa. If the map is modified while an iteration
* over the collection is in progress (except through the iterator's own
* remove operation), the results of the iteration are undefined. The
* collection supports element removal, which removes the corresponding
* mapping from the map, via the Iterator.remove, Collection.remove,
* removeAll, retainAll and clear operations. It does not support the add or
* addAll operations.
*
* Returns: a collection view of the values contained in this map
*
* @param m
*/
public static void useva lues(Map m) {
Collection c = m.values();
Iterator it = c.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
执行结果:
[plain]
下面是使用useKeySet()方法输出的结果:
0: I love you
1: You love him
2: He loves her
3: She loves me
下面是使用useEntrySet()方法输出的结果:
0: I love you
1: You love him
2: He loves her
3: She loves me
下面是使用useva lues()方法输出的结果:
I love you
You love him
He loves her
She loves me