Junior — Middle
What methods of implementing the List interface do you know in Java?
sobes.tech AI
Answer from AI
In Java, the List interface can be implemented in various ways depending on performance and functionality requirements:
-
ArrayList — implements a dynamic array. Fast access by index, but slow insertions and deletions in the middle of the list.
-
LinkedList — implements a doubly linked list. Fast insertions and deletions anywhere, but slow access by index.
-
CopyOnWriteArrayList — a thread-safe implementation based on copying the array during modifications, suitable for scenarios with many reads and infrequent modifications.
-
Stack — extends Vector, implements a stack data structure (LIFO).
Example of using ArrayList:
List<String> list = new ArrayList<>();
list.add("First");
list.add("Second");
System.out.println(list.get(0)); // Will output "First"
The choice of implementation depends on specific tasks and performance requirements.