![]() |
VOOZH | about |
In JavaCollections Framework, List, Set, and Map are the most commonly used collection types for storing and managing data. Each serves a different purpose based on how elements are stored, accessed, and organized. Understanding their differences helps in selecting the most suitable collection for a particular requirement.
The Set interface is a part of the java.util package and is used to store a collection of unique elements. It does not allow duplicate values and is commonly used when data uniqueness is required.
Syntax:
Set<DataType> setName = new HashSet<>();
Elements in Set: [Apple, Mango, Orange, Banana] Contains Mango? true After removing Orange: [Apple, Mango, Banana] Total Elements: 3
The List interface is a part of the java.util package and is used to store an ordered collection of elements. It allows duplicate values and preserves the insertion order of elements.
Syntax:
List<DataType> listName = new ArrayList<>();
Output :
mango
orange
Grapes
The Map interface is a part of the java.util package and is used to store data in the form of key-value pairs. Each key must be unique, while values can be duplicated.
Syntax:
Map<KeyType, ValueType> mapName = new HashMap<>();
Output :
100 Amit
101 Vijay
102 Rahul
| Feature | List | Set | Map |
|---|---|---|---|
| Interface Package | java.util.List | java.util.Set | java.util.Map |
| Data Storage | Stores elements | Stores unique elements | Stores key-value pairs |
| Duplicate Values | Allowed | Not Allowed | Duplicate keys not allowed, values allowed |
| Insertion Order | Maintains insertion order | Depends on implementation | Depends on implementation |
| Index-Based Access | Supported | Not Supported | Not Supported |
| Key-Value Pair | No | No | Yes |
| Null Values | Multiple null values allowed | Usually one null element allowed | One null key (HashMap) and multiple null values allowed |
| Retrieval Method | By index | By element | By key |
| Primary Use | Ordered collection of data | Storing unique data | Associating keys with values |
| Common Implementations | ArrayList,LinkedList LinkedList, Vector | HashSet, LinkedHashSet, TreeSet | HashMap, LinkedHashMap,TreeMap |