[1] List
- ArrayList
Uses an array to store elements, allowing for fast access and traversal through index-based operations.
However, when elements are inserted or removed in the middle, it requires shifting subsequent elements to accommodate the change.
This shifting operation can lead to inefficiencies for frequent insertions/removals.
- LinkedList
Stores elements as nodes, where each node points to the next and/or previous node.
This structure makes inserting and removing elements efficient, especially in the middle, as it only requires updating references.
However, random access to elements is slower compared to ArrayList because you need to traverse the list from the beginning or end to reach a specific index.
[2] Set
- HashSet
Uses hash codes to store and retrieve elements, offering constant-time complexity for common operations like adding, removing and checking for containment.
However, it doesn't guarantee any specific order of elements and doesn't maintain insertion order.
- LinkedHashSet
It is similar to a HashSet, but it maintains the insertion order of elements.
This means that when you iterate through a LinkedHashSet, the elements are returned in the order they were added.
Additionally, it offers faster iteration compared to a regular HashSet.
- TreeSet
Uses a self-balancing binary search tree to store elements in sorted order.
This enables efficient operations like adding, removing and checking for containment in logarithmic time complexity.
The sorted nature of a TreeSet is useful when you need elements to be ordered according to their natural ordering or a custom comparator.
[3] Map
- HashMap
Uses hash codes to store and retrieve key-value pairs, providing constant-time complexity for common operations like adding, removing and retrieving.
However, it doesn't guarantee any specific order of elements and doesn't maintain insertion order.
Hash collisions (when different keys produce the same hash code) can impact performance by slowing down access in such cases.
- LinkedHashMap
Similar to a HashMap, but it maintains the insertion order of key-value pairs.
When iterating through a LinkedHashMap, the entries are returned in the order they were added.
Additionally, it offers faster iteration compared to a regular HashMap.
- TreeMap
Uses a self-balancing binary search tree to store key-value pairs in sorted order based on the keys.
This allows for efficient operations like adding, removing, and checking for containment in logarithmic time complexity.
The sorted nature of a TreeMap is beneficial when you need keys to be ordered according to their natural ordering or a custom comparator.