Junior — Middle
What types of Map interface implementations have you used in your projects?
sobes.tech AI
Answer from AI
In Java projects, I used several main implementations of the Map interface:
-
HashMap — the most common implementation, provides fast insertion and lookup of elements by key (average O(1)). Used when the order of elements is not important.
-
LinkedHashMap — maintains the insertion order of elements, which is useful if you need to iterate over elements in the same order they were added.
-
TreeMap — implements the SortedMap interface, stores keys in sorted order (by natural order or using a Comparator). Used when ordered access to elements is important.
Example of using HashMap:
Map<String, Integer> map = new HashMap<>();
map.put("apple", 3);
map.put("banana", 5);
int count = map.get("apple");