Junior — Middle
What specific implementations of collection interfaces have you used in your projects?
sobes.tech AI
Answer from AI
In Java projects, I often used the following implementations of collection interfaces:
- List:
ArrayListfor dynamic arrays with quick access by index,LinkedListwhen fast insertion/deletion in the middle of the list is needed. - Set:
HashSetfor storing unique elements with quick access,LinkedHashSetif insertion order is important,TreeSetfor sorted sets. - Map:
HashMapfor associative arrays with quick access,LinkedHashMapto preserve insertion order,TreeMapfor sorted key-value mappings.
Example of using HashMap:
Map<String, Integer> wordCount = new HashMap<>();
wordCount.put("apple", 3);
wordCount.put("banana", 2);
int count = wordCount.getOrDefault("apple", 0);