`

jdk1.5新特性 增强for循环

阅读更多
[code="java"]public class Demo3 {
@Test
public void test1(){
Map map = new HashMap();
map.put("1","a");
map.put("2","b");
map.put("3","c");
map.put("4","d");

Set set = map.keySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()){
String key = (String) iterator.next();
String value = (String) map.get(key);
System.out.println(key+"="+value);
}
}
@Test
public void test2(){
Map map = new HashMap();
map.put("1", "a");
map.put("2", "b");
map.put("3", "c");
map.put("4", "d");
Set entry = map.entrySet();
Iterator iterator = entry.iterator();
while(iterator.hasNext()){
Map.Entry en = (Entry) iterator.next();
String key = (String) en.getKey();
String value = (String) en.getValue();
System.out.println(key+"="+value);
}
}
@Test
public void test3(){
Map map = new HashMap();
map.put("1", "a");
map.put("2", "b");
map.put("3", "c");
map.put("4", "d");
Set entry = map.entrySet();
for(Object obj:entry){
Map.Entry en = (Entry) obj;
String key = (String) en.getKey();
String value = (String) en.getValue();
System.out.println(key+"="+value);
}
}
@Test
public void test4(){
Map map = new LinkedHashMap();
map.put("1", "a");
map.put("2", "b");
map.put("3", "c");
map.put("4", "d");
Set keySet = map.keySet();
for(Object obj :keySet){
String key = (String) obj;
String value = (String) map.get(key);
System.out.println(key+"="+value);
}
}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics