java-collections-framework-list-set-map

Java Collections Framework: Overview

  • 4 min

The Collections Framework is the set of interfaces, implementations, and algorithms for working with groups of objects.

Arrays have a fixed size. If you create one with five positions and need a sixth, you must create another and copy the elements.

In the real world, data is dynamic. Shopping carts grow, users register and unsubscribe. We need structures that can grow and shrink on their own.

For that, Java offers the Java Collections Framework (JCF). A set of Interfaces and Classes designed and optimized to manipulate groups of objects.

Today, we will look at the overall picture so you never hesitate about which structure to use.

The Collection Hierarchy

The framework is divided into two main groups. It is important to understand that Maps do not implement Collection.

The Collection Interface (The Root)

It is the parent interface for groups of individual elements. It defines the basic operations that all share:

  • add(E e): Add.
  • remove(Object o): Delete.
  • size(): How many there are.
  • contains(Object o): Is this inside?
  • iterator(): To traverse them.

From here, two main branches emerge: List and Set.

The Map Interface

Maps do NOT inherit from Collection. Why? Because Collection stores things one by one (objects), and Map stores pairs (Key -> Value). They have different methods (put, get…).

The 3 main structures

When you need to store data, ask yourself these questions to choose the correct interface:

“Order matters and I allow duplicates”

It’s like a shopping list or a to-do list.

  • Ordered: The first element is 0, the second is 1… They maintain insertion order.
  • Duplicates: You can have “Milk” twice on the list.
  • Access: You can ask for the element at position X (get(3)).

Famous implementations: ArrayList, LinkedList.

“Uniqueness is what matters”

It’s like a bag of marbles or a list of VIP guests.

  • No duplicates: If you try to add something that is already there, Java ignores it.
  • No order (generally): There is no concept of “position 3”. It’s either in the bag or it isn’t.

Famous implementations: HashSet, TreeSet.

“Key-Value Dictionary”

It’s like a phone book or a real dictionary.

  • Unique Keys: There cannot be two people with the same ID (Key).
  • Duplicate Values: Several people can be named “Pepe” (Value).
  • Fast access: Give me the ID and I’ll give you the Person instantly.

Famous implementations: HashMap, TreeMap.

Wrappers: The Toll of Collections

There is a technical rule in Java you must remember: Collections ONLY store Objects.

You cannot create an ArrayList<int>. Primitive types (int, double, boolean) are not objects, so they don’t go into the collection.

To solve this, Java has Wrapper Classes:

PrimitiveWrapper (Object)
intInteger
doubleDouble
charCharacter
booleanBoolean

Thanks to Autoboxing (Java 5+), the conversion is automatic, but you must declare the collection correctly:

// WRONG ❌
// List<int> numeros = new ArrayList<>();

// CORRECT ✅
List<Integer> numeros = new ArrayList<>();

numeros.add(10); // Java internally converts the int 10 to new Integer(10)
int n = numeros.get(0); // Java automatically "unboxes" it to int
Copied!

Iterators: Traversing Collections

Although we usually use the for-each loop (which works because all collections implement Iterable), sometimes we need the Iterator.

The Iterator is an object that allows us to traverse the collection and, this is important, delete elements safely while traversing.

List<String> nombres = new ArrayList<>();
nombres.add("Ana");
nombres.add("Pedro");

// FOR-EACH (Only for comfortable reading)
for (String nombre : nombres) {
    System.out.println(nombre);
    // nombres.remove(nombre); // ERROR! ConcurrentModificationException
}

// ITERATOR (To delete while traversing)
Iterator<String> it = nombres.iterator();
while(it.hasNext()) {
    String nombre = it.next();
    if (nombre.equals("Ana")) {
        it.remove(); // ✅ Safe way to delete
    }
}
Copied!