Map에 HashMap넣는건 동시성 문제생기니 코테때만 사용하기
Value Map은 그냥 Map 2개 만들어서 사용하는게 더 편할듯하다

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


 		/**
         * 1. MAP<String, LIST>
         */

        HashMap<String, List> mapList = new HashMap<>();
        //PUT
        mapList.put("A", Arrays.asList("list1","list2"));
        //GET
        mapList.get("A").get(0);

        /**
        //2. MAP<String, MAP<Key,Value>>
         *
         */
        Map<String, Map<String,Integer>> mapMapOf = new HashMap<>();
        Map<String, Map<String,Integer>> mapMapOfEntry = new HashMap<>();
        HashMap<String, HashMap<String,Integer>> mapMapPut = new HashMap<>();
        Map<String, int[]> mapArr = new HashMap<>();


        /**
         * ※ 수정이 불가능
         * 자바 9버전 이상에서는 Map.of 사용가능
         * 10개 제한

         */
        mapMapOf.put("key1",Map.of(
                                    "mapKey1",1,
                                    "mapKey2",2
                                   )
        );

        System.out.println("mapMapOf = "+mapMapOf);
        System.out.println("mapMapOf = "+mapMapOf.get("key1").get("mapKey1"));
      //mapMapOf.get("key1").put("mapKey1",3);
        System.out.println("mapMapOf = "+mapMapOf);
      //mapMapOf.put("key1",mapMapOf.get("key1").put("mapKey1",3));
        /**
         * ※ 수정이 불가능
         * 10개 이상 초기화 가능
         */
        new HashMap<String,Integer>().entrySet();
        mapMapOfEntry.put("key1",Map.ofEntries(
                                      entry("sndKey1",1),
                                      entry("sndKey2",2)
                                 )
        );

        System.out.println("mapMapOfEntry = "+mapMapOfEntry);
        System.out.println("mapMapOfEntry = "+mapMapOfEntry.get("key1").get("sndKey2"));
      //  mapMapOfEntry.get("key1").put("sndKey2",3);
      //  System.out.println("mapMapOfEntry = "+mapMapOfEntry);

        /**
         * 자바 8버전 이하에서도 사용 가능
         * ※ 수정가능
         */
        mapMapPut.put("key1",new HashMap<String,Integer>(){{ put("sndKey1",1);
                                                          put("sndKey2",2); }});

        //값 얻기
        System.out.println("mapMapPut = "+mapMapPut.get("key1").get("sndKey1"));
        mapMapPut.get("key1").put("sndKey2",3);
        System.out.println("mapMapPut = "+mapMapPut);

        //혹은 map 상시 생성
        HashMap<String,Integer> hs = new HashMap<>();
        System.out.println("=> "+hs.put("sndKey3",3));
        mapMapPut.put("key2",hs);

        System.out.println("mapMapPut = "+mapMapPut);



        /**
         * 맵 배멸
         *  3. MAP<Stirng, int[]>
         *
        */
        mapArr.put("key1",new int[]{1,2});
        System.out.println(mapArr.get("key1")[0]);

        mapArr.get("key1")[0]= mapArr.get("key1")[0]+10;

        System.out.println("mapArr change value => "+mapArr.get("key1")[0]);

'IT > Java' 카테고리의 다른 글

String to Date , Date to String, Time add  (0) 2020.10.19
Eclipse java Working Set 설정  (0) 2020.03.07
interface와 상속 차이 (미완)  (0) 2019.12.22
Java 다중 반복문 빠져나가기  (0) 2019.10.05
02. 상속  (0) 2019.01.15

+ Recent posts