쵸코푸들이장군 2018. 2. 10. 21:37

Map<String, String> map = new HashMap<String, String>();

         

        map.put("key1", "value1");

        map.put("key2", "value2");

        map.put("key3", "value3");

        map.put("key4", "value4");

        map.put("key5", "value5");


         

         

        // 방법1

        Iterator<String> keys = map.keySet().iterator();

        while( keys.hasNext() ){

            String key = keys.next();

            System.out.println( String.format("키 : %s, 값 : %s", key, map.get(key)) );

System.out.println( "key :"+key+", value:"+map.get(key) );

        }

         

        // 방법2

        for( Map.Entry<String, String> elem : map.entrySet() ){

            System.out.println( String.format("key : "+ elem.getKey() +", value : "+  elem.getValue()) );

        }

         

        // 방법3

        for( String key : map.keySet() ){

            System.out.println( String.format("key :"+key+", value : "+ map.get(key)) );

        }



* 3번이 쓰긴 편함



////////////////

map이 원래 순서가 보장 되지 않는다.


그래서 LinkedHashMap 이란게 있다.



아오!