Card 07/ 08

ExerciseDifficulty: Advanced1 min

Six Declarations: Which Survive Swapping the Implementation

Six signatures. For each, decide whether it still compiles when the caller switches from an ArrayList to a LinkedList, or from a HashSet to a TreeSet. Write all six down first.

java
void a(ArrayList<String> refs) { }
void b(List<String> refs) { }
void c(Collection<String> refs) { }
void d(HashSet<String> refs) { }
List<String> e() { return new ArrayList<>(); }
ArrayList<String> f() { return new ArrayList<>(); }
Which of the six survive the swap, and what each one costs?
Each signature, whether it survives, and what it commits to
SignatureSurvives?What it says
a(ArrayList<String>)NoDemands one class; refuses List.of too
b(List<String>)YesNeeds order and positions — accepts every list
c(Collection<String>)YesNeeds only to walk it — the widest honest type
d(HashSet<String>)NoDemands one class; a TreeSet is refused
e() returning ListYesCallers cannot depend on which list it is
f() returning ArrayListCompiles, and traps youEvery caller may now depend on it being an ArrayList

The last row is the one worth arguing about. Returning the class does not break today — it publishes the choice, so changing it later becomes a change to everyone who called you.

The rule underneath all six: declare the least you need. A method that only walks its argument should say Collection, and a method that indexes it should say List.