티스토리 뷰

반응형

 

벌써 8일이다.

Day 8 챌린지를 시작해보자!

 

강의 코드부터 확인하자. 

 

package hackerrank.day8;

import java.util.HashMap;
import java.util.Map;

public class DictionaryPractice {
    public static void main(String[] args) {
        Map<String, String> engKorDictionary = new HashMap<String, String>();
        engKorDictionary.put("Monday", "월요일");
        engKorDictionary.put("Tuesday", "화요일");
        engKorDictionary.put("Wednesday", "수요일");
        engKorDictionary.put("Thursday", "목요일");
        engKorDictionary.put("Friday", "금요일");

        System.out.println(engKorDictionary.get("Monday"));
        System.out.println(engKorDictionary.get("Tuesday"));
        System.out.println(engKorDictionary.get("Wednesday"));
        System.out.println(engKorDictionary.get("Thursday"));
        System.out.println(engKorDictionary.get("Friday"));

        // Print out all keys
        System.out.println(engKorDictionary.keySet());

        // Print out all keys
        System.out.println(engKorDictionary.values());

        // Print out size
        System.out.println("The Size of out Dictionary is " + engKorDictionary.size());


        // Shopping List
        Map<String, Boolean> shoppingList = new HashMap<String, Boolean>();
        // Put some stuff in dictionary
        shoppingList.put("Ham", true);
        shoppingList.put("Bread", Boolean.TRUE);
        shoppingList.put("Oreos", Boolean.TRUE);
        shoppingList.put("Eggs", Boolean.FALSE);
        shoppingList.put("Sugar", false);

        // Retrieve items
        System.out.println(shoppingList.get("Ham"));
        System.out.println(shoppingList.get("Sugar"));
        // Key-Value Pairs Print Out
        System.out.println(shoppingList.toString());
        // Clear out dictionary
        shoppingList.clear();
        System.out.println(shoppingList.toString());

        // Is Empty?
        System.out.println(shoppingList.isEmpty());

        // Remove things
        shoppingList.remove("Eggs");
        // Replace values for a certain key
        shoppingList.replace("Bread", Boolean.FALSE);
        // Key-Value Pairs Print Out
        System.out.println(shoppingList.toString());
    }
}

 

 

오늘은 Map, HashMap에 대해 공부해보았다.

Map은 자주 쓰이는 중요한 개념인데

어렵고 헷갈린다는 이유로 잘 공부하지 않았다.

 

오늘을 기회로 Map을 접하게 되었고

여러 종류의 Map 사용방법에 대해 궁금해져서 공부하려한다.

조만간 Map 관련 포스팅도 작성해야겠다.

 

HashSet에 값을 넣고 지우고 바꾸고 조회하는 방법에 대해 알아보았다.

 


 

그럼 다음 포스팅에서 문제 풀이로 만나도록 하자. 😊 

반응형