public class MapLearn { public static void main(String[] args) throws ParseException { Map<String,String> map=new HashMap<>(); map.put("01", "01"); map.put("02", "02"); map.put("03", "03"); map.put("04", "04"); map.put("02", "055");//key值重複,會覆蓋前一個 //把map中key保存到set中,而後根據獲取set中的key值去取value Set<String> keySet=map.keySet(); Iterator<String> iterator=keySet.iterator(); while(iterator.hasNext()){ String key=iterator.next(); System.out.println("Key:"+key); String ss=map.get(key); System.out.println("Value:"+ss); } //把map中key與value的關係保存到set中 Set<Map.Entry<String, String>> me=map.entrySet(); Iterator<Map.Entry<String, String>> iterator2=me.iterator(); while(iterator2.hasNext()){ Map.Entry<String, String> s=iterator2.next(); String key=s.getKey(); String value=s.getValue(); System.out.println(key+":"+value); } } }