map 遍历第二种方法 MapEntry

来源:互联网 发布:天蝎网络第三季百度云 编辑:程序博客网 时间:2024/04/26 19:29


接口 Map.Entry<K,V>

映射项(键-值对)。Map.entrySet 方法返回映射的 collection 视图,

其中的元素属于此类。获得映射项引用的唯一 方法

是通过此 collection 视图的迭代器来实现。这些 Map.Entry 对象 在迭代期间有效;

更确切地讲,如果在迭代器返回项之后修改了底层映射

则某些映射项的行为是不确定的,除了通过 setValue 在映射项上执行操作之外


方法摘要 booleanequals(Object o) 
          比较指定对象与此项的相等性。 KgetKey() 
          返回与此项对应的键。 VgetValue() 
          返回与此项对应的值。 inthashCode() 
          返回此映射项的哈希码值。 VsetValue(V value) 
          用指定的值替换与此项对应的值(可选操作)。


package com.map;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Map.Entry;import java.util.Set;/** * 遍历:第二种方法 * @author 小明 * */public class EntrySetDemo {public static void main(String[] args) {Map<String, String> map = new HashMap<String, String>();map.put("01", "zhangsan1");map.put("02", "zhangsan2");map.put("03", "zhangsan3");map.put("04", "zhangsan4");Set<Entry<String, String>> entrySet = map.entrySet();Iterator<Entry<String, String>> it = entrySet.iterator();while(it.hasNext()){Entry<String, String> next = it.next();String key = next.getKey();String value = next.getValue();System.out.println(key + " "+value);}}}


0 0
原创粉丝点击