Map 增强for循环

	@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);
		}


	}

你可能感兴趣的:(Map 增强for循环)