如何用for循环生成多个map
1个回答
2016-11-23 · 知道合伙人软件行家
关注
展开全部
@Test
public void testMap() {
Map map = new LinkedHashMap();
map.put("1", "aaa");
map.put("2", "bbb");
map.put("3", "ccc");
//传统1
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);
}
//传统2
Map<String, String> map2 = new LinkedHashMap<String, String>();
map2.put("1", "2张三");
map2.put("2", "2李四");
map2.put("3", "2王五");
Iterator iterator2 = map2.entrySet().iterator();
while (iterator2.hasNext()) {
Map.Entry<String, String> entry = (Entry<String, String>) iterator2.next();
System.out.println("key:" + entry.getKey() + " value"+ entry.getValue());
}
//for
for(Object obj:map.keySet()){
String keyString = (String)obj;
String valueString = (String)map.get(keyString);
System.out.println("key:" + keyString + " value:"+ valueString);
}
}
public void testMap() {
Map map = new LinkedHashMap();
map.put("1", "aaa");
map.put("2", "bbb");
map.put("3", "ccc");
//传统1
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);
}
//传统2
Map<String, String> map2 = new LinkedHashMap<String, String>();
map2.put("1", "2张三");
map2.put("2", "2李四");
map2.put("3", "2王五");
Iterator iterator2 = map2.entrySet().iterator();
while (iterator2.hasNext()) {
Map.Entry<String, String> entry = (Entry<String, String>) iterator2.next();
System.out.println("key:" + entry.getKey() + " value"+ entry.getValue());
}
//for
for(Object obj:map.keySet()){
String keyString = (String)obj;
String valueString = (String)map.get(keyString);
System.out.println("key:" + keyString + " value:"+ valueString);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询