package com.map1;
import org.junit.Test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Main1 {
@Test
public void test1(){
Map map=new HashMap();
map.put("n1","abc1");
map.put("n2","abc2");
map.put("n3","abc3");
//方法1
/*
Set s1=map.entrySet();
Iterator iter=s1.iterator();
while (iter.hasNext()){
System.out.println(iter.next());
}
*/
//方法2 遍历key --value
Set ss1=map.entrySet();
Iterator it=ss1.iterator();
while (it.hasNext()){
Object obj=it.next();
Map.Entry entry=(Map.Entry)obj;
System.out.println("key:"+entry.getKey()+"---->"+entry.getValue());
}
//方法3 遍历key --value
Set keySet=map.keySet();
Iterator iterator=keySet.iterator();
while (iterator.hasNext()){
Object key=iterator.next();
Object value=map.get(key);
System.out.println("key:"+key+"---->"+value);
}
}
}
java中的Map如何遍历key和value
最新推荐文章于 2022-10-14 15:10:34 发布