A report combines selection, ordering and presentation. Describe those decisions in plain language before writing a pipeline. This checkpoint also checks ownership: a report must not reorder its caller's original data. Use the comparator tie case you practiced earlier.
Work through the example
A report can be expressed as a loop plus a sort or as a stream. Here the stream filters before sorting, so discarded values do not need ordering. The result contains only accepted labels. sorted() uses the strings' natural case-sensitive order. sorted(comparator) instead uses your supplied ordering policy. Both return a stream stage; unlike List.sort, neither rearranges the source list. No mutable accumulator is captured by a lambda.
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.
import java.util.List;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
List<String> source = new ArrayList<>();
source.add("Test");
source.add("Go");
source.add("Read");
List<String> result = source.stream()
.filter(title -> title.length() >= 4)
.sorted()
.toList();
System.out.println(result);
System.out.println(source);
}
}Check your prediction
[Read, Test]
[Test, Go, Read]Compare the behavior
Counting and listing are different terminal results. count returns long, Java's 64-bit whole-number type, rather than the 32-bit int. It holds larger counts; use a long variable even for a tiny example. Empty data is a valid report. The same source list can create another fresh stream, but a consumed stream itself cannot be used twice.
import java.util.List;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
List<String> source = new ArrayList<>();
source.add("Go");
long count = source.stream()
.filter(title -> title.length() >= 4)
.count();
System.out.println(count);
}
}Check your prediction
0Plan your acceptance cases
Before writing the complete implementation, predict these cases without running the solution. Keep the requirements above as the authority; this table highlights distinct promises rather than prescribing a particular class layout.
| Case | Expected behavior | Skill it checks |
|---|---|---|
| A longer title and a shorter accepted title | Longer first | Primary descending order |
| Read and Test | Read first | Ascending tie-breaker |
| Length 2, length 3 and empty input | Exclude 2, include 3; empty gives empty | Filter boundary |
After your first version works, choose one row and introduce a plausible defect that it should expose. Restore the correct version, then recreate the central operation without the worked example. If one skill is missing, revisit that lesson before combining more features.
Practice independently
Checkpoint: Ranked Report: practice
Implement rank(List<String>) for nonnull strings: retain titles of at least three characters, sort by descending length then ascending text, and return an unmodifiable list. For Go, Test, Read, Java return [Java, Read, Test]. Check the source's first entry is still Go and an empty list returns [].
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.
import java.util.List;
import java.util.ArrayList;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
System.out.println("Implement and check rank");
}
}Hint 1: the idea
Separate the comparator policy from the pipeline so the two ordering directions are visible.
Hint 2: a first step
Build byLength once, reverse it, add the text tie-breaker, and pass that comparator to sorted.
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?
import java.util.List;
import java.util.ArrayList;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
List<String> source = new ArrayList<>();
source.add("Go");
source.add("Test");
source.add("Read");
source.add("Java");
System.out.println(rank(source));
System.out.println(source.get(0));
System.out.println(rank(new ArrayList<String>()));
}
static List<String> rank(List<String> source) {
Comparator<String> byLength = Comparator.comparingInt(String::length);
return source.stream()
.filter(title -> title.length() >= 3)
.sorted(byLength.reversed().thenComparing(String::compareTo))
.toList();
}
}Expected output:
[Java, Read, Test]
Go
[]Change it and test again
The supplied accepted values all tie on length. Add a longer title and an exactly-three-character title so the tests exercise both primary order and the threshold. Replace sorted with sorting the source directly: which ownership assertion should fail?
Explain before moving on
Can you explain this pipeline as an ordinary loop? When would you keep the loop instead? Do not move on until you can predict empty results and ties without running the code.
Completion is saved in this browser only. It does not measure mastery.