Comparing yesterday's list with today's is three filters over two sets, not a diff library. Sets make each membership test O(1) and the three results partition the union exactly.
var before = List.of("S1", "S2", "S3");
var after = List.of("S2", "S3", "S4");
var was = new TreeSet<>(before);
var now = new TreeSet<>(after);
var added = now.stream().filter(s -> !was.contains(s)).toList();
var removed = was.stream().filter(s -> !now.contains(s)).toList();
var kept = now.stream().filter(was::contains).toList();
System.out.println("added : " + added);
System.out.println("removed: " + removed);
System.out.println("kept : " + kept);
added : [S4]
removed: [S1]
kept : [S2, S3]
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-09-20