파이썬 딕셔너리, 자바 해시맵

BlackHan·2024년 5월 3일
0
public class Main {
    public static void main(String[] args) {
        // 딕셔너리 생성
        Map<String, Integer> dictionary = new HashMap<>();

        // 요소 추가
        dictionary.put("apple", 5);
        dictionary.put("banana", 3);
        dictionary.put("orange", 7);

        // items() 대신 entrySet() 사용하여 엔트리(키-값 쌍)의 집합 얻기
        for (Map.Entry<String, Integer> entry : dictionary.entrySet()) {
            System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
        }

        // keys() 대신 keySet() 사용하여 키의 집합 얻기
        for (String key : dictionary.keySet()) {
            System.out.println("Key: " + key);
        }

        // values() 사용하여 값의 집합 얻기
        for (int value : dictionary.values()) {
            System.out.println("Value: " + value);
        }

        // get(key) 사용하여 값 얻기
        int appleCount = dictionary.get("apple");
        System.out.println("Apple Count: " + appleCount);
    }
}
// HashMap 선언
Map map = new HashMap();
map.put("name", "a");
map.put("age", 100);
map.put("name", "b"); // 처음 대임된 a -> b로 변경 됩니다.

// 결과
// key : name, value : b
// key : age, value : 100
for (Object o : map.keySet()) {
    System.out.println("key : " + o.toString() + ", value : " + map.get(o));
}

map.keySet();                             // key 값들을 Set 형태로 모두 불러옵니다.
map.values();                             // value에 해당하는 값들을 Collection 형태로 불러옵니다.
map.get("name");                          // key에 해당하는 값을 읽기
map.getOrDefault("address", "Not Found"); // key에 해당하는 값이 없을 경우 default 값을 참조
map.containsKey("email");                 // key 값이 map에 포함하는지 확인
map.remove("name1");                      // key에 해당하는 값을 제거
map.clear();                              // 모든 map 내부 데이터 제거

Map map2 = new HashMap();
map.put("name", "aa");
map.put("age", 200);

// 결과
// {name=aa, age=200}
map.putAll(map2);                         // map에 map2의 값을 대입합니다.

도넛과 막대 그래프

import java.util.*;

class Solution {
    public int[] solution(int[][] edges) {
        Map<Integer, Integer> out = new HashMap<>();
        Map<Integer, Integer> in = new HashMap<>();
        int[] answer = new int[4];

        for (int[] edge : edges) { // (1)
            out.put(edge[0], out.getOrDefault(edge[0], 0) + 1);
            in.put(edge[1], in.getOrDefault(edge[1], 0) + 1);
        }

        for (int node : out.keySet()) {
            if (out.get(node) > 1) { // (2)
                if (!in.containsKey(node)) {
                    answer[0] = node;
                } else {
                    answer[3] += 1;
                }
            }
        }

        for (int node : in.keySet()) {
            if (!out.containsKey(node)) { // (3)
                answer[2] += 1;
            }
        }
        answer[1] = out.get(answer[0]) - answer[2] - answer[3]; // (4)
        return answer;
    }
}

다른 풀이

public class HashEx {
    public int[] solution(int[][] edges) {
        int[] answer = new int[4];
        Map<Integer, int[]> map = new HashMap<>();
        for (int[] edge : edges) {
            map.put(edge[0],map.getOrDefault(edge[0],new int[]{0,0}));
            map.put(edge[1], map.getOrDefault(edge[1], new int[]{0, 0}));
            map.get(edge[0])[0]++;
            map.get(edge[1])[1]++;
        }
        for(Map.Entry<Integer,int[]> entry : map.entrySet()){
            int Key = entry.getKey();
            int[] val = entry.getValue();
            if (val[0] >= 2 && val[1] == 0) {
                answer[0] = Key;
            }
            // 막대 모양 그래프의 수 확인
            else if (val[0] == 0 && val[1] > 0) {
                answer[2]++;
            }
            // 8자 모양 그래프의 수 확인
            else if (val[0] >= 2 && val[1] >= 2) {
                answer[3]++;
            }
        }
        // 도넛 모양 그래프의 수 확인
        answer[1] = map.get(answer[0])[0] - answer[2] - answer[3];
        return answer;
    }
profile
Slow-starter

0개의 댓글