ConcurrentHashMap — fine-grained locking, better concurrency. synchronizedMap — single lock for all operations.
1// ConcurrentHashMap — concurrent reads and writes2ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();3map.put("key", 1); // Lock on bucket only4map.compute("key", (k, v) -> v + 1); // Atomic operation56// Collections.synchronizedMap — blocks entire map7Map<String, Integer> syncMap = Collections.synchronizedMap(new HashMap<>());8syncMap.put("key", 1); // Lock on entire map
Key differences: