본문 바로가기
Java

Java ConcurrentHashMap 생성 및 초기화하는 방법 6가지

by Status Code 2023. 4. 23.

Java의 ConcurrentHashMap은 동시성을 지원하는 Map 인터페이스의 구현체입니다. 이 글에서는 여러 가지 방법으로 ConcurrentHashMap을 생성하고 초기화하는 예제들을 소개하겠습니다.

 

예제 1. 기본 생성 및 초기화

import java.util.concurrent.ConcurrentHashMap;
import java.util.Map;

public class ConcurrentHashMapExample {
    public static void main(String[] args) {
        Map<String, Integer> concurrentHashMap = new ConcurrentHashMap<>();

        concurrentHashMap.put("one", 1);
        concurrentHashMap.put("two", 2);
        concurrentHashMap.put("three", 3);

        System.out.println(concurrentHashMap);
    }
}

 

 

예제 2. 생성 시 초기 용량 지정

import java.util.concurrent.ConcurrentHashMap;
import java.util.Map;

public class ConcurrentHashMapExample {
    public static void main(String[] args) {
        Map<String, Integer> concurrentHashMap = new ConcurrentHashMap<>(5);

        concurrentHashMap.put("one", 1);
        concurrentHashMap.put("two", 2);
        concurrentHashMap.put("three", 3);

        System.out.println(concurrentHashMap);
    }
}

 

 

 

예제 3. 생성 시 초기 용량 및 로드 팩터 지정

import java.util.concurrent.ConcurrentHashMap;
import java.util.Map;

public class ConcurrentHashMapExample {
    public static void main(String[] args) {
        Map<String, Integer> concurrentHashMap = new ConcurrentHashMap<>(5, 0.75f);

        concurrentHashMap.put("one", 1);
        concurrentHashMap.put("two", 2);
        concurrentHashMap.put("three", 3);

        System.out.println(concurrentHashMap);
    }
}

 

 

 

예제 4. 생성 시 초기 용량, 로드 팩터 및 동시성 수준 지정

import java.util.concurrent.ConcurrentHashMap;
import java.util.Map;

public class ConcurrentHashMapExample {
    public static void main(String[] args) {
        Map<String, Integer> concurrentHashMap = new ConcurrentHashMap<>(5, 0.75f, 4);

        concurrentHashMap.put("one", 1);
        concurrentHashMap.put("two", 2);
        concurrentHashMap.put("three", 3);

        System.out.println(concurrentHashMap);
    }
}

 

 

 

예제 5. 익명 클래스를 사용한 초기화

import java.util.concurrent.ConcurrentHashMap;
import java.util.Map;

public class ConcurrentHashMapExample {
    public static void main(String[] args) {
        Map<String, Integer> concurrentHashMap = new ConcurrentHashMap<String, Integer>() {{
            put("one", 1);
            put("two", 2);
            put("three", 3);
        }};

        System.out.println(concurrentHashMap);
    }
}

 

 

 

예제 6. Java 8의 Stream API를 사용한 초기화

import java.util.concurrent.ConcurrentHashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ConcurrentHashMapExample {
    public static void main(String[] args) {
        Map<String, Integer> concurrentHashMap = Stream.of(new Object[][]{
                {"one", 1},
                {"two", 2},
                {"three", 3},
        }).collect(Collectors.toMap(data -> (String) data[0], data -> (Integer) data[1], (v1, v2) -> v2, ConcurrentHashMap::new));

        System.out.println(concurrentHashMap);
}
}

 

 

이 글에서 소개한 예제들은 다양한 방법으로 ConcurrentHashMap을 생성하고 초기화하는 방법을 보여줍니다. 상황에 따라 적절한 방법을 선택하여 사용할 수 있습니다.

 

댓글