Sobes.tech
Junior — Middle

Can you name different approaches to implementing the Map interface in Java?

sobes.tech AI

Answer from AI

In Java, the Map interface is implemented by different classes, each suitable for different tasks:

  • HashMap — the most popular implementation, stores key-value pairs without order, provides fast access (O(1) on average).
  • LinkedHashMap — extends HashMap, maintains insertion order.
  • TreeMap — implements NavigableMap interface, stores elements in sorted order by key (uses a red-black tree).
  • Hashtable — outdated synchronized implementation, now often replaced by ConcurrentHashMap.
  • ConcurrentHashMap — thread-safe implementation for multi-threaded environments, without locking the entire map.

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
Can you name different approaches to implementing the… - sobes.tech