Sub topic 10
Wrappers in Generics and Collections, Including `List.remove`
Prerequisites and the central rule
Assume you know Java primitive types, classes, objects, ==, equals, and basic List and Map usage with generics.
A primitive type stores a value directly. A wrapper class stores the same kind of value inside an object. For example, int is primitive, while Integer is its wrapper class.
Java collections use generic type parameters, and generic type parameters must be reference types. Therefore, this declaration is invalid:
// List<int> values; // Does not compileUse the wrapper instead:
List<Integer> values = new ArrayList<>();When you add an int to this list, Java automatically boxes it. Boxing converts a primitive value into a wrapper object. When you read an element into an int, Java unboxes it. Unboxing extracts the primitive value from the wrapper.
Boxing in collection operations
The compiler inserts these conversions at the boundary between primitive code and collection APIs:
import java.util.ArrayList;
import java.util.List;
public class IntegerCollectionDemo {
public static void main(String[] args) {
List<Integer> scores = new ArrayList<>();
scores.add(90); // Integer.valueOf(90)
scores.add(85); // Integer.valueOf(85)
Integer objectScore = scores.get(0);
int primitiveScore = scores.get(1); // objectScore.intValue() conceptually
System.out.println(objectScore);
System.out.println(primitiveScore);
}
}The declared type matters. scores.get(0) returns Integer, even though assigning it to int causes unboxing.
A wrapper can also represent the absence of a value with null. A primitive cannot be null. This makes wrappers useful when a collection needs to distinguish “zero” from “no value,” but it also creates a failure risk:
Use an explicit check or a default value when the collection may contain null:
Integer stored = null;
int safe = stored == null ? 0 : stored;
System.out.println(safe);The `List.remove` overload
List<Integer> has two relevant overloads:
| Call | Selected method | Meaning |
|---|---|---|
| `list.remove(1)` | `remove(int index)` | Remove the element at index 1 |
| `list.remove(Integer.valueOf(1))` | `remove(Object value)` | Remove the element whose value is 1 |
This is a common source of bugs because the list’s element type does not force remove(1) to mean “remove the value 1.” The argument is an int, so Java selects the index overload.
import java.util.ArrayList;
import java.util.List;
public class RemoveExample {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>(List.of(10, 20, 30));
numbers.remove(1);
System.out.println(numbers); // [10, 30]
numbers.remove(Integer.valueOf(10));
System.out.println(numbers); // [30]
}
}To remove by value, pass an Integer explicitly:
Integer value = 20;
numbers.remove(value);The variable’s static type, which is the type known by the compiler, selects remove(Object). If you need to remove by index, use an int variable or an index expression.
Other collection consequences
The same wrapper rules apply to maps and sets:
Map<String, Integer> quantities = new HashMap<>();
quantities.put("apples", 3); // boxes 3
int count = quantities.get("apples"); // unboxes the resultIf the key is absent, get returns null, so the final assignment can fail. Prefer getOrDefault when a missing key should have a known value.
Collections also store object references, not primitive values. Repeated boxing can create extra objects, although common wrapper values may come from a cache. For ordinary collection use, this cost is usually acceptable. For very large numeric data sets or performance-sensitive loops, consider primitive arrays or primitive streams.
Exercise
Given List<Integer> items = new ArrayList<>(List.of(4, 8, 12));, which call removes the value 8: items.remove(1) or items.remove(Integer.valueOf(8))?
Show answer
items.remove(Integer.valueOf(8)) removes the value 8. items.remove(1) removes the element at index 1, which is also 8 in this particular list, but it uses a different overload and meaning.