java
47 lines · 8 steps
Diffing two collections with set operations
A small immutable value type computes what was added, removed, and unchanged between two collections using plain set algebra.
Explained by
highlit
1import java.util.HashSet;
2import java.util.Set;
3
4public final class CollectionDiff<T> {
5
6 private final Set<T> added;
7 private final Set<T> removed;
8 private final Set<T> unchanged;
9
10 private CollectionDiff(Set<T> added, Set<T> removed, Set<T> unchanged) {
11 this.added = added;
12 this.removed = removed;
13 this.unchanged = unchanged;
14 }
15
16 public static <T> CollectionDiff<T> between(Collection<? extends T> before, Collection<? extends T> after) {
17 Set<T> source = new HashSet<>(before);
18 Set<T> target = new HashSet<>(after);
19
20 Set<T> added = new HashSet<>(target);
21 added.removeAll(source);
22
23 Set<T> removed = new HashSet<>(source);
24 removed.removeAll(target);
25
26 Set<T> unchanged = new HashSet<>(source);
27 unchanged.retainAll(target);
28
29 return new CollectionDiff<>(added, removed, unchanged);
30 }
31
32 public Set<T> added() {
33 return Set.copyOf(added);
34 }
35
36 public Set<T> removed() {
37 return Set.copyOf(removed);
38 }
39
40 public Set<T> unchanged() {
41 return Set.copyOf(unchanged);
42 }
43
44 public boolean hasChanges() {
45 return !added.isEmpty() || !removed.isEmpty();
46 }
47}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Set difference and intersection give you added, removed, and unchanged in three cheap operations.
- 2A private constructor plus a static factory keeps construction logic in one clear entry point.
- 3Copying sets on the way out prevents callers from mutating internal state.
Related explainers
java
@Component @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> {
Transparent column encryption in Spring & JPA
encryption
aes-gcm
jpa-converter
Advanced
10 steps
java
package com.acme.billing.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties;
Feature-flagged beans with Spring @ConditionalOnProperty
feature-flags
conditional-beans
strategy-pattern
Intermediate
5 steps
java
public static Map<String, String> parseCookieHeader(String header) { Map<String, String> cookies = new LinkedHashMap<>(); if (header == null || header.isBlank()) { return cookies;
Parsing an HTTP Cookie header in Java
string-parsing
http
url-decoding
Intermediate
6 steps
go
package logging import ( "context"
Deduplicating log attributes in Go's slog
decorator-pattern
structured-logging
immutability
Intermediate
8 steps
java
public class TimedSocketReader { private static final int READ_TIMEOUT_MS = 5_000; private static final int CONNECT_TIMEOUT_MS = 3_000;
Reading a socket with connect and read timeouts
sockets
timeouts
io
Intermediate
8 steps
rust
use axum::{ async_trait, extract::{rejection::JsonRejection, FromRequest, Request}, http::StatusCode,
A validated JSON extractor in Axum
extractors
validation
error-handling
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/diffing-two-collections-with-set-operations-explained-java-856a/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.