Junior — Middle
Can you list and describe the main implementations of collections in Java?
sobes.tech AI
Answer from AI
In Java, the main implementations of collections are located in the java.util package and are divided into several groups:
-
List — ordered collections that allow duplicates.
ArrayList— dynamic array, fast access by index, slow insertions/deletions in the middle.LinkedList— doubly linked list, fast insertions/deletions, slow access by index.Vector— outdated thread-safe analogue ofArrayList.
-
Set — collections without duplicates.
HashSet— based on hash table, does not guarantee order.LinkedHashSet— preserves insertion order.TreeSet— sorted set, based on a red-black tree.
-
Queue — collections for FIFO operation.
LinkedListimplementsQueue.PriorityQueue— queue with priority.
-
Map — key-value mappings.
HashMap— hash table, does not guarantee order.LinkedHashMap— preserves insertion order.TreeMap— sorted map by key.
Each implementation has its own features regarding performance and behavior, and the choice depends on specific tasks.