R
Rishtaara
Java Fundamentals
Lesson 28 of 36Article17 min

Collections, List, ArrayList & LinkedList

java.util collections hold groups of objects — List, Set, Map, Queue. Use interfaces for flexibility: List<String> list = new ArrayList<>();

Collections Framework — overview

java.util collections hold groups of objects — List, Set, Map, Queue. Use interfaces for flexibility: List<String> list = new ArrayList<>();

Generics <Type> keep type safety — no casting from raw types.

Real-life example: Collections are different containers — list = numbered queue, set = unique badge set, map = name-to-desk lookup.

  • List — ordered, allows duplicates
  • Set — unique elements only
  • Map — key → value pairs

List, ArrayList & LinkedList

List interface — add, get, remove by index. ArrayList — fast random access. LinkedList — fast insert/remove at ends.

Pick ArrayList by default unless you insert/delete heavily in the middle.

Real-life example: ArrayList is a theater row — jump to seat 5 quickly. LinkedList is a chain of people holding hands — easy to add in middle if you know neighbors.

ArrayList and LinkedList
import java.util.*;

List<String> array = new ArrayList<>();
array.add("Java");
array.add("Spring");
System.out.println(array.get(0));

List<Integer> linked = new LinkedList<>();
linked.add(10);
linked.add(20);