What is a collection in Java?
A collection is a container that holds a group of objects. The collection provides a way to manage objects easily. Collections manage groups of objects as a single unit.
Read also: What is Vector Java?
Common Collection Types
- List:
- ArrayList: Best for general-purpose storage and fast random access.
- LinkedList: Good for frequent insertions and deletions.
- Set:
- HashSet: Use when you need a set with no duplicates and order doesn’t matter.
- LinkedHashSet: Maintains insertion order.
- TreeSet: Keeps elements sorted.
- Map:
- HashMap: Best for general-purpose key-value storage.
- LinkedHashMap: Maintains insertion order.
- TreeMap: Stores keys in sorted order.
Here are a few basic operations we do on collections :
1) Adding objects to the collection.
2) Removing or deleting objects from the collection.
3) Retrieving objects from the collection.
4) Iterating collection