@Test
public void test7(){
Map<Integer,String> map = new HashMap<Integer,String>();
map.put(100,"jack");
map.put(200,"marry");
map.put(300,"sisi");
//将Map集合转换成Set集合,并Set集合中存放Map的key值
Set<Integer> set = map.keySet();
Iterator<Integer> it = set.iterator();
while(it.hasNext()){
Integer key = it.next();//键
String value = map.get(key);//值
System.out.println(key+"<->"+value);
}
}
@Test
public void test8(){
Map<Integer,String> map = new HashMap<Integer,String>();
map.put(100,"jack");
map.put(200,"marry");
map.put(300,"sisi");
Set<Entry<Integer,String>> set = map.entrySet();
for(Entry<Integer,String> entry : set){
System.out.print(entry.getKey() + "<->");
System.out.println(entry.getValue());
}
}
Map的遍历:keySet与entrySet
最新推荐文章于 2024-06-19 20:44:50 发布