[ZB]

4. HashMap

혜리노베이션 2023. 5. 11. 17:17

//      2. Maps - 쌍을 이루는 자료형
        System.out.println("== Maps ==");
        HashMap map = new HashMap();


//      2-1. put - 데이터 넣기
        map.put("hyeri", 28);
        map.put("youngchan", 36);
        map.put("daye", 32);
        map.put("sungwoo", 35);
        System.out.println("map = " + map); //순서대로 출력X, 순서는 중요하지 않음


//      2-2. get - 데이터 꺼내기
        System.out.println(map.get("hyeri")); // 있는 데이터 ==> 28
        System.out.println(map.get("hyungee")); //없는 데이터 ==> null

//      2-3. size
        System.out.println(map.size());


//      2-4. remove
        System.out.println(map.remove("hyeri")); // 있는 데이터 ==> 지우면서 값을 되돌림 28
        System.out.println(map.remove("hyungee")); // 없는 데이터 ==> null
        System.out.println(map); // 지워진 key "hyeri' 삭제됨

//      2-5. containsKey
        System.out.println(map.containsKey("hyeri")); // 지워진 데이터 ==> false
        System.out.println(map.containsKey("daye")); // 있는 데이터 ==> true

 


1. HashMap 사용하여 자료에 데이터 추가, 꺼내기, 삭제 가능

2. HashMap의 사이즈, 데이터의 유무 확인 가능