Exploring Map in Java: Your Ultimate Core Java Interview Guide - Part 2
Dinuka Ekanayake
Senior Software Engineer | Java Development, React , Angular , AWS , Azure , CICD , Kubernetes
Map Implementations in Java
Java provides several implementations of the Map interface, each with its own characteristics and use cases:
Key Features of Java Map:
Internal Behaviour of HashMap
Concurrent Collections for Maps in Java
In Java, concurrent collections offer thread-safe alternatives to their non-concurrent counterparts, providing efficient and scalable solutions for concurrent programming. One of the key motivations behind using concurrent collections is to address the limitations of legacy thread-safe collections, such as Hashtable, which are not as performant or flexible for modern multithreaded applications.
Why Concurrent Collections?
Legacy Collections: Hashtable was among the first collections introduced in Java and provided thread safety by synchronising all methods, ensuring that only one thread could access the collection at a time. However, this global locking mechanism led to performance bottlenecks and limited scalability, making it less suitable for modern applications.
ConcurrentHashMap: With the introduction of Java 1.5, ConcurrentHashMap was introduced to provide efficient thread safety without sacrificing performance. ConcurrentHashMap uses a fine-grained locking mechanism that allows multiple threads to access different segments of the map concurrently, minimizing contention and improving overall performance.
Performance of ConcurrentHashMap
ConcurrentHashMap offers high performance compared to Hashtable due to its efficient locking strategy:
Commonly Used Methods of Java Map:
Map<String, Integer> map = new HashMap<>();
map.put("apple", 10);
map.put("banana", 5);
2. get(Object key): Retrieves the value associated with the specified key.
int appleCount = map.get("apple"); // Retrieves the value associated with the key "apple"
3. containsKey(Object key): Checks if the map contains the specified key.
boolean containsKey = map.containsKey("apple");
4. containsValue(Object value): Checks if the map contains the specified value.
boolean containsValue = map.containsValue(10);
5. size(): Returns the number of key-value mappings in the map.
int size = map.size();
6. remove(Object key): Removes the key-value pair associated with the specified key from the map.
map.remove("apple");
7. keySet(): Returns a Set view of the keys contained in the map.
Set<String> keys = map.keySet();
8. values(): Returns a Collection view of the values contained in the map.
Collection<Integer> values = map.values();
9. entrySet(): Returns a Set view of the key-value mappings contained in the map.
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
10. clear(): Removes all key-value pairs from the map.
map.clear();
11. getOrDefault(Object key, V defaultValue): Retrieves the value associated with the specified key, or a default value if the key is not present in the map.
getOrDefault(Object key, V defaultValue)
// Retrieves the value associated with the specified key, or a default value if the key is not present in the map.
Conclusion:
Java Map is a fundamental data structure that offers efficient key-value mapping, making it indispensable in various Java applications. By mastering its key features and commonly used methods, developers can effectively utilize maps to solve a wide range of programming problems and excel in Java interviews.