Collections Framework
Why Collections?
Arrays are great, but they have a big limitation: fixed size. What if you don't know how many items you'll need? What if you want to quickly look up a value by name instead of by number? That's where the Collections Framework comes in.
Think of it as a toolbox of advanced containers:
- ArrayList — a resizable array. Like a backpack that magically gets bigger as you add more stuff.
- LinkedList — a chain of nodes. Great for adding/removing from the beginning or end.
- HashSet — a bag that doesn't allow duplicates. Like a guest list where each name can only appear once.
- HashMap — a dictionary built on hash functions. Look up values by key, like finding a phone number by name.
All collections use generics (the <Type> in angle brackets) to specify what type they hold.
ArrayList — Your Go-To List
HashSet & HashMap
A HashSet is perfect when you care about uniqueness. Adding a duplicate? It just ignores it. Think of it like a stamp collection — you don't want two of the same stamp.
A HashMap stores key-value pairs. It's like a real dictionary: the word is the key, the definition is the value. You look things up by key, and it's crazy fast — nearly instant, no matter how many entries you have.
HashSet — No Duplicates Allowed
HashMap — The Power of Key-Value Pairs
int or double directly. Instead, Java uses wrapper classes: Integer, Double, Boolean, etc. Java auto-converts between them (called autoboxing), so ArrayList<Integer> works seamlessly with int values.Quick check
Continue reading