Skip to content

Come Back Later

The Course
•
20 min study

Return after two or three days, then again a week later. Work from memory for the first attempt. Delayed practice reveals which ideas you can retrieve independently and which you only recognized while reading. This review is extra time beyond the seven daily study budgets.

Work through the example

Begin by predicting, not running. This code checks whether repeated values survive a list-to-set conversion. Explain both the size and the order. If you cannot explain them, reread the collections lesson after your attempt rather than copying its example immediately.

Before Run, write the output and identify the exact line responsible for each result. Each editor is a separate program; names and edits in one box do not carry into the next.

Predict, then run Java
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.LinkedHashSet;

public class Main {
    public static void main(String[] args) {
        List<String> values = new ArrayList<>();
        values.add("Read");
        values.add("Test");
        values.add("Read");
        Set<String> unique = new LinkedHashSet<>(values);
        System.out.println(values.size());
        System.out.println(unique);
    }
}
Check your prediction
Text
3
[Read, Test]

Compare the behavior

Now trace a pipeline with an inclusive boundary and an empty-result possibility. Write the selected intermediate values before computing the sum. This separates understanding the filter from remembering the terminal method's name.

Trace the changed case Java
import java.util.List;
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        List<Integer> values = new ArrayList<>();
        values.add(10);
        values.add(25);
        values.add(40);
        System.out.println(values.stream()
            .filter(value -> value >= 25)
            .mapToInt(value -> value)
            .sum());
    }
}
Check your prediction
Text
65

Practice independently

Exercise

Come Back Later: practice

Without reopening yesterday's solutions, write generic copyNonNull(List<T>). Preserve order and duplicates, skip null elements and return a separate mutable list. For [Read, null, Read], print [Read, Read]. Clear the returned list and show the original still has size 3. Then explain why mutable elements would still be shared.

Spend a few minutes on your own first. Compile after one small change, read the first error, and check the result against the requirements rather than matching the solution's layout.

Your workspace Java
import java.util.List;
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        System.out.println("Retrieve generics and ownership from memory");
    }
}
Hint 1: the idea

Keep filtering separate from copying. The type parameter connects input elements to output elements.

Hint 2: a first step

Declare <T> before List<T>, create a new ArrayList<T>, and add only nonnull values.

Show one solution

One possible implementation follows. Compare its decisions with yours: which values can change, what inputs are accepted, and what would expose a mistake?

Java
import java.util.List;
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        List<String> source = new ArrayList<>();
        source.add("Read");
        source.add(null);
        source.add("Read");
        List<String> copy = copyNonNull(source);
        System.out.println(copy);
        copy.clear();
        System.out.println(source.size());
    }

    static <T> List<T> copyNonNull(List<T> source) {
        List<T> result = new ArrayList<>();
        for (T value : source) {
            if (value != null) {
                result.add(value);
            }
        }
        return result;
    }
}

Expected output:

Text
[Read, Read]
3

Change it and test again

Choose two retrieval challenges: rebuild the repository's duplicate rejection; parse a line with a trailing empty field; implement a comparator with a tie-breaker; or inject two policies into the same consumer. For each, add one test that catches a deliberate defect. Return to the task tracker and explain one source file aloud before running it.

Explain before moving on

Record what you completed unaided, which hint you needed, and the next retrieval date. You are ready for larger local projects when you can protect state, choose collections, define a useful boundary and demonstrate recovery with tests. Frameworks, persistence, concurrency and performance deserve their own study beyond this course.

Ready for advanced?

Before moving on, rebuild a small repository from memory: reject duplicate IDs without changing the original entry, return an independent list, and compare a simple loop report with a stream report. Then inject a second policy without editing its consumer. Revisit ownership, interfaces, generic methods or streams for the missing skill.

Java advanced adds records, Optional, sealed result types, exact decimals and atomic batches. Those are new lessons, not assumed prerequisites. Its ledger deliberately changes the recovery policy from this course's keep-valid-lines importer to all-or-nothing imports; compare that requirement before copying code across projects.

Was this article helpful?

Your response is saved on this device.