map的put理解

package test; import java.util.Collections; import java.util.*; public class TestList { public static void main(String[] args) { Map map = new HashMap(); map.put("1","hello"); map.put("2","world"); map.put("3","nihao"); map.put("4","shijie"); map.put("5","zhong"); map.put("6","guo"); Iterator iterator = map.keySet().iterator(); while (iterator.hasNext()) { Object key = iterator.next(); Object value = map.get(key); System.out.println(key + "--" + value); } String str = (String)map.put("1", "linweihan"); //将索引为1的值给取代了,并返回旧的值 System.out.println(str); System.out.println("=============================================="); Iterator iterator1 = map.keySet().iterator(); while (iterator1.hasNext()) { Object key = iterator1.next(); Object value = map.get(key); System.out.println(key + "--" + value); } } } ============================================= 结果: 3--nihao 5--zhong 2--world 4--shijie 6--guo 1--hello hello ============================================== 3--nihao 5--zhong 2--world 4--shijie 6--guo 1--linweihan

你可能感兴趣的:(object,String,HashMap,iterator,Class)