Sobes.tech
Junior — Middle

Milliseid viise teate Java keeles List-liidese rakendamiseks?

sobes.tech AI

Vastus AI-lt

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 — thread-safe implementation based on copying the array during modifications, suitable for scenarios with many reads and few modifications.

  • Stack — a class that 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 print "First"

The choice of implementation depends on specific tasks and performance requirements.