//맵 자료형에는 HashMap, LinkedMap, TreeMap 등이 있다.
 

import java.util.HashMap;

public class Sample5 {
    public static void main(String[] args){
        HashMap<String, String>map = new HashMap<>();
        map.put("people", "사람");
        map.put("baseball", "야구");
 

        System.out.println(map.get("people")); //"people"겟하라고 하면 "사람" 출력됨.
        System.out.println(map.getOrDefault("java", "없음"));
        //"java"가 없으면 디폴트값인 "없음"을 출력하도록 함.
 
       
        System.out.println(map.containsKey("people")); // key 값 people 있으면 true 출력
 

        System.out.println(map.remove("people")); // key, value를 모두 삭제한 후에 value값인 "사람" 출력
 
        System.out.println(map.size());
 
        System.out.println(map.keySet()); //map의 모든 키를 모아서 집합으로 리턴


        //LinkedHashMap: 입력한 순서대로 데이터를 저장, TreeMap: 입력된 key의 오름차순으로 데이터 저장.
        //리스트는 순서가 중요하지만 맵은 순서에 의존하지 않는다.
 
 

+ Recent posts