Sub topic 1
What the Collections Framework Is
Most programs need to keep track of groups of things: names in a class, items in a cart, or scores in a game. Arrays can hold a group, but an array has a fixed size. Once you create it, it cannot grow or shrink. The Java Collections Framework solves this problem.
What It Is
A collection is an object that holds a group of other objects, called elements. The Java Collections Framework is a set of ready-made interfaces and classes in the java.util package for storing and working with groups of data.
An interface is a list of methods that a class promises to provide. An implementation is a class that actually does that work. The framework gives you a small number of interfaces and several implementations of each.
Why It Matters
Collections help you in three ways:
- They grow and shrink as you add or remove elements.
- They come with useful methods for adding, removing, searching, and counting.
- They are tested and fast, so you do not have to write these tools yourself.
The Main Interfaces
The framework has four interfaces you will use most:
| Interface | What it holds | Duplicates allowed? | Example use |
|---|---|---|---|
| `Collection` | Any group of elements | Depends on the type | The shared parent of `List` and `Set` |
| `List` | Elements in order, reachable by position | Yes | A to-do list |
| `Set` | Unique elements | No | Tags on a blog post |
| `Map` | Pairs of keys and values | Keys: no. Values: yes | A phone book |
Collection is the top-level interface. List and Set both extend it, which means they get all of its methods and add their own. Map is part of the framework, but it does not extend Collection, because it stores pairs instead of single elements.
Methods Every Collection Has
Since List and Set both extend Collection, they share these methods:
add(e)puts an element in.remove(e)takes an element out.contains(e)returnstrueif the element is there.size()returns how many elements there are.isEmpty()returnstrueif there are none.
A Worked Example
The program below creates a list and a set and treats both as plain Collection objects. The <String> part says the collection holds strings. The next chapter explains it.
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
public class CollectionDemo {
public static void main(String[] args) {
Collection<String> list = new ArrayList<>();
list.add("red");
list.add("blue");
list.add("red");
Collection<String> set = new HashSet<>();
set.add("red");
set.add("blue");
set.add("red");
System.out.println(list.size()); // 3
System.out.println(set.size()); // 2
System.out.println(list.contains("blue")); // true
list.remove("red"); // removes the first "red"
System.out.println(list); // [blue, red]
}
}The same three add calls give different results. The list keeps both copies of "red". The set keeps only one. The shared interface gives you the same methods, and each implementation decides how they behave.
Exercises
Exercise
You need to store the usernames of everyone signed up for a website, and no username may appear twice. Which interface fits best: List, Set, or Map?
Show answer
Set, because a Set holds only unique elements and ignores duplicates.
Exercise
Why can't you call add(e) with a single element on a Map the way you can on a List?
Show answer
A Map does not extend Collection. It stores key-value pairs, so you add entries with put(key, value) instead of add(e).