Junior — Middle
Can you list classes that implement the Map interface in Java?
sobes.tech AI
Answer from AI
The Java Map interface is implemented by several main classes from the standard library:
HashMap— the most commonly used implementation, stores key-value pairs without order.LinkedHashMap— extendsHashMap, maintains insertion order.TreeMap— implements a sorted map, elements are ordered by key (uses a red-black tree).Hashtable— an outdated synchronized implementation, nowConcurrentHashMapis more commonly used.ConcurrentHashMap— a thread-safe implementation for multi-threaded applications.
Example of using HashMap:
Map<String, Integer> map = new HashMap<>();
map.put("apple", 3);
map.put("banana", 5);
System.out.println(map.get("apple")); // 3