
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Difference between Concurrent hash map and Synchronized hashmap in Java
Concurrent Hashmap is a class that was introduced in jdk1.5. Concurrent hash map applies locks only at bucket level called fragment while adding or updating the map. So, a concurrent hash map allows concurrent read and write operation to the map.
Synchronized hashmap(Collection.syncronizedHashMap()) is a method of Collection framework. This method applies a lock on the entire collection. So, if one thread is accessing the map then no other thread can access the same map.
Sr. No. | Key | Concurrent hash map | Synchronized hashmap |
---|---|---|---|
1 | Implementation | It is a class that implements a Concurrent hash map and serializable interface. | It is a method in Collection class. |
2 | Lock mechanism | Locks the portion | Locks the whole map. |
3 | Performance | Concurrent hashmap allows concurrent read and write. So performance is relatively better than a synchronized map. | Multiple threads can't access the map concurrently. So, performance is relatively less than a concurrent hash map. |
4 | Null key | It doesn't allow null as a key or value. | It allows null as a key. |
5 | Concurrent modification exception | It doesn't throw concurrent modification exception. | Iterator return by synchronized map throws concurrent modification exception |
Example of SynchronizedMap
public class SynchronizedMapExample { public static void main(String[] args) { Map<Integer,String> laptopmap = new HashMap<Integer,String>(); laptopmap.put(1,"IBM"); laptopmap.put(2,"Dell"); laptopmap.put(3,"HCL"); // create a synchronized map Map<Integer,String> syncmap = Collections.synchronizedMap(laptopmap); System.out.println("Synchronized map is : "+syncmap); } }
Example of ConcurrentHashMap
public class ConcurrentHashMapExample { public static void main(String[] args) { //ConcurrentHashMap Map<Integer,String> laptopmap = new ConcurrentHashMap<Integer,String>(); laptopmap.put(1,"IBM"); laptopmap.put(2,"Dell"); laptopmap.put(3,"HCL"); System.out.println("ConcurrentHashMap is: "+laptopmap); } }
- Related Articles
- Difference Between ReentrantLock and Synchronized in Java
- Difference between Synchronized ArrayList and CopyOnWriteArrayList in Java
- Difference between Traditional Collections and Concurrent Collections in java
- Difference between HashMap and HashSet in Java.
- Difference between EnumMap and HashMap in Java
- Difference between HashMap and HashTable in Java.
- Difference between HashMap and ConcurrentHashMap in Java
- Difference between HashTable and HashMap in Java
- Difference Between HashMap and TreeMap in Java
- Difference Between HashMap and LinkedHashMap in Java
- Difference between TreeMap, HashMap, and LinkedHashMap in Java
- Difference between TreeMap, HashMap and LinkedHashMap in Java programming
- Difference between Tree Set and Hash Set in Java
- Difference between HashMap and ConcurrentHashMap
- Traversing contents of a hash map in Java

Advertisements