Java’s Collections Framework is one of its most powerful tools — but it’s confusing at first.
Should you use a List
, a Set
, or a Map
? 🤔
Here’s a beginner-friendly guide that clears it all up 👇
1. List
– Ordered and Allows Duplicates
-
Think of it like an array, but more flexible.
-
Maintains insertion order
-
Duplicates are allowed
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Apple"); // allowed!
Popular types:
-
ArrayList
-
LinkedList
2. Set
– Unique Values Only
-
No duplicates allowed
-
Doesn’t guarantee order (unless you use
LinkedHashSet
orTreeSet
)
Set<String> names = new HashSet<>();
names.add("Aarav");
names.add("Aarav"); // ignored!
Popular types:
-
HashSet
-
TreeSet
-
LinkedHashSet
No comments:
Post a Comment