티스토리 뷰

HashMap은 기존 컬렉션 아래에, ConcurrentHashMap은 동시성 컬렉션 아래에 있는 클래스이다.
이 외에도 다음과 같은 다양한 차이점들이 존재한다.


✔️ HashMap은 기본적으로 동기화되지 않는다. 즉, HashMap은 Thread-safe하지 않다.

반면 ConcurrentHashMap은 Thread-safe하다.
(Thread-safe한 코드는 많은 쓰레드가 동시에 실행하는 경우에도 작동하는 코드이다.)


✔️ HashMap은 기본적으로 동기화되지 않고 여러 쓰레드가 동시에 수행될 수 있기 때문에 상대적으로 성능이 좋다.

그러나 ConcurrentHashMap은 때때로 대기하는 쓰레드가 발생하기 때문에 ConcurrentHashMap의 성능이 낮은 경우도 있다.


✔️ 하나의 쓰레드가 HashMap 객체를 iterate하는 동안, 다른 쓰레드가 객체의 내용을 추가/수정하려고 하면 ConcurrentModificationException이라는 런타임 예외가 발생한다.

반면 ConcurrentHashMap에서는 iterate할 때 객체 수정이 발생하더라도 예외가 발생하지 않는다.


✔️ HashMap에서는 키와 값에 대해 null 값이 허용된다.

ConcurrentHashMap에서는 키와 값에 대해 null 값이 허용되지 않는다.
null 값을 사용하는 경우 NullPointerException이라는 런타임 예외가 발생한다.


참고: 

 

Difference between HashMap and ConcurrentHashMap - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

www.geeksforgeeks.org

 

728x90
댓글