Skip to content

Commit 720079c

Browse files
brunoborgesCopilot
andcommitted
Extract snippets.json into individual JSON files per category
Each snippet is now stored as category/slug.json alongside its HTML file. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 9857e9a commit 720079c

File tree

86 files changed

+2924
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+2924
-0
lines changed

collections/collectors-teeing.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"id": 26,
3+
"slug": "collectors-teeing",
4+
"title": "Collectors.teeing()",
5+
"category": "collections",
6+
"difficulty": "intermediate",
7+
"jdkVersion": "12",
8+
"oldLabel": "Java 8",
9+
"modernLabel": "Java 12+",
10+
"oldApproach": "Two Passes",
11+
"modernApproach": "teeing()",
12+
"oldCode": "long count = items.stream().count();\ndouble sum = items.stream()\n .mapToDouble(Item::price)\n .sum();\nvar result = new Stats(count, sum);",
13+
"modernCode": "var result = items.stream().collect(\n Collectors.teeing(\n Collectors.counting(),\n Collectors.summingDouble(Item::price),\n Stats::new\n )\n);",
14+
"summary": "Compute two aggregations in a single stream pass.",
15+
"explanation": "Collectors.teeing() sends each element to two downstream collectors and merges the results. This avoids streaming the data twice or using a mutable accumulator.",
16+
"whyModernWins": [
17+
{
18+
"icon": "",
19+
"title": "Single pass",
20+
"desc": "Process the stream once instead of twice."
21+
},
22+
{
23+
"icon": "🧩",
24+
"title": "Composable",
25+
"desc": "Combine any two collectors with a merger function."
26+
},
27+
{
28+
"icon": "🔒",
29+
"title": "Immutable result",
30+
"desc": "Merge into a record or value object directly."
31+
}
32+
],
33+
"support": "Widely available since JDK 12 (March 2019)"
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"id": 22,
3+
"slug": "copying-collections-immutably",
4+
"title": "Copying collections immutably",
5+
"category": "collections",
6+
"difficulty": "beginner",
7+
"jdkVersion": "10",
8+
"oldLabel": "Java 8",
9+
"modernLabel": "Java 10+",
10+
"oldApproach": "Manual Copy + Wrap",
11+
"modernApproach": "List.copyOf()",
12+
"oldCode": "List<String> copy =\n Collections.unmodifiableList(\n new ArrayList<>(original)\n );",
13+
"modernCode": "List<String> copy =\n List.copyOf(original);",
14+
"summary": "Create an immutable copy of any collection in one call.",
15+
"explanation": "List.copyOf(), Set.copyOf(), and Map.copyOf() create immutable snapshots of existing collections. If the source is already an immutable collection, no copy is made.",
16+
"whyModernWins": [
17+
{
18+
"icon": "",
19+
"title": "Smart copy",
20+
"desc": "Skips the copy if the source is already immutable."
21+
},
22+
{
23+
"icon": "📏",
24+
"title": "One call",
25+
"desc": "No manual ArrayList construction + wrapping."
26+
},
27+
{
28+
"icon": "🛡️",
29+
"title": "Defensive copy",
30+
"desc": "Changes to the original don't affect the copy."
31+
}
32+
],
33+
"support": "Widely available since JDK 10 (March 2018)"
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"id": 19,
3+
"slug": "immutable-list-creation",
4+
"title": "Immutable list creation",
5+
"category": "collections",
6+
"difficulty": "beginner",
7+
"jdkVersion": "9",
8+
"oldLabel": "Java 8",
9+
"modernLabel": "Java 9+",
10+
"oldApproach": "Verbose Wrapping",
11+
"modernApproach": "List.of()",
12+
"oldCode": "List<String> list =\n Collections.unmodifiableList(\n new ArrayList<>(\n Arrays.asList(\"a\", \"b\", \"c\")\n )\n );",
13+
"modernCode": "List<String> list =\n List.of(\"a\", \"b\", \"c\");",
14+
"summary": "Create immutable lists in one clean expression.",
15+
"explanation": "List.of() creates a truly immutable list — no wrapping, no defensive copy. It's null-hostile (rejects null elements) and structurally immutable. The old way required three nested calls.",
16+
"whyModernWins": [
17+
{
18+
"icon": "📏",
19+
"title": "One call",
20+
"desc": "Replace three nested calls with a single factory method."
21+
},
22+
{
23+
"icon": "🔒",
24+
"title": "Truly immutable",
25+
"desc": "Not just a wrapper — the list itself is immutable."
26+
},
27+
{
28+
"icon": "🛡️",
29+
"title": "Null-safe",
30+
"desc": "Rejects null elements at creation time, failing fast."
31+
}
32+
],
33+
"support": "Widely available since JDK 9 (Sept 2017)"
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"id": 20,
3+
"slug": "immutable-map-creation",
4+
"title": "Immutable map creation",
5+
"category": "collections",
6+
"difficulty": "beginner",
7+
"jdkVersion": "9",
8+
"oldLabel": "Java 8",
9+
"modernLabel": "Java 9+",
10+
"oldApproach": "Map Builder Pattern",
11+
"modernApproach": "Map.of()",
12+
"oldCode": "Map<String, Integer> map = new HashMap<>();\nmap.put(\"a\", 1);\nmap.put(\"b\", 2);\nmap.put(\"c\", 3);\nmap = Collections.unmodifiableMap(map);",
13+
"modernCode": "Map<String, Integer> map =\n Map.of(\"a\", 1, \"b\", 2, \"c\", 3);",
14+
"summary": "Create immutable maps inline without a builder.",
15+
"explanation": "Map.of() accepts key-value pairs inline and returns an immutable map. For more than 10 entries, use Map.ofEntries() with Map.entry() pairs.",
16+
"whyModernWins": [
17+
{
18+
"icon": "📏",
19+
"title": "Inline creation",
20+
"desc": "No temporary mutable map needed."
21+
},
22+
{
23+
"icon": "🔒",
24+
"title": "Immutable result",
25+
"desc": "The map cannot be modified after creation."
26+
},
27+
{
28+
"icon": "🚫",
29+
"title": "No null keys/values",
30+
"desc": "Null entries are rejected immediately."
31+
}
32+
],
33+
"support": "Widely available since JDK 9 (Sept 2017)"
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"id": 21,
3+
"slug": "immutable-set-creation",
4+
"title": "Immutable set creation",
5+
"category": "collections",
6+
"difficulty": "beginner",
7+
"jdkVersion": "9",
8+
"oldLabel": "Java 8",
9+
"modernLabel": "Java 9+",
10+
"oldApproach": "Verbose Wrapping",
11+
"modernApproach": "Set.of()",
12+
"oldCode": "Set<String> set =\n Collections.unmodifiableSet(\n new HashSet<>(\n Arrays.asList(\"a\", \"b\", \"c\")\n )\n );",
13+
"modernCode": "Set<String> set =\n Set.of(\"a\", \"b\", \"c\");",
14+
"summary": "Create immutable sets with a single factory call.",
15+
"explanation": "Set.of() creates a truly immutable set that rejects nulls and duplicate elements at creation time. No more wrapping mutable sets.",
16+
"whyModernWins": [
17+
{
18+
"icon": "📏",
19+
"title": "Concise",
20+
"desc": "One line instead of three nested calls."
21+
},
22+
{
23+
"icon": "🚫",
24+
"title": "Detects duplicates",
25+
"desc": "Throws if you accidentally pass duplicate elements."
26+
},
27+
{
28+
"icon": "🔒",
29+
"title": "Immutable",
30+
"desc": "No add/remove possible after creation."
31+
}
32+
],
33+
"support": "Widely available since JDK 9 (Sept 2017)"
34+
}

collections/map-entry-factory.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"id": 23,
3+
"slug": "map-entry-factory",
4+
"title": "Map.entry() factory",
5+
"category": "collections",
6+
"difficulty": "beginner",
7+
"jdkVersion": "9",
8+
"oldLabel": "Java 8",
9+
"modernLabel": "Java 9+",
10+
"oldApproach": "SimpleEntry",
11+
"modernApproach": "Map.entry()",
12+
"oldCode": "Map.Entry<String, Integer> e =\n new AbstractMap.SimpleEntry<>(\n \"key\", 42\n );",
13+
"modernCode": "var e = Map.entry(\"key\", 42);",
14+
"summary": "Create map entries with a clean factory method.",
15+
"explanation": "Map.entry() replaces the verbose AbstractMap.SimpleEntry constructor. It returns an immutable entry, making it ideal for Map.ofEntries() and stream operations.",
16+
"whyModernWins": [
17+
{
18+
"icon": "📏",
19+
"title": "Concise",
20+
"desc": "One line instead of three with a clearer intent."
21+
},
22+
{
23+
"icon": "🔒",
24+
"title": "Immutable",
25+
"desc": "The returned entry cannot be modified."
26+
},
27+
{
28+
"icon": "🧩",
29+
"title": "Composable",
30+
"desc": "Works perfectly with Map.ofEntries() for large maps."
31+
}
32+
],
33+
"support": "Widely available since JDK 9 (Sept 2017)"
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"id": 24,
3+
"slug": "sequenced-collections",
4+
"title": "Sequenced collections",
5+
"category": "collections",
6+
"difficulty": "beginner",
7+
"jdkVersion": "21",
8+
"oldLabel": "Java 8",
9+
"modernLabel": "Java 21+",
10+
"oldApproach": "Index Arithmetic",
11+
"modernApproach": "getFirst/getLast",
12+
"oldCode": "// Get last element\nvar last = list.get(list.size() - 1);\n// Get first\nvar first = list.get(0);\n// Reverse iteration: manual",
13+
"modernCode": "var last = list.getLast();\nvar first = list.getFirst();\nvar reversed = list.reversed();",
14+
"summary": "Access first/last elements and reverse views with clean API methods.",
15+
"explanation": "SequencedCollection adds getFirst(), getLast(), reversed(), addFirst(), addLast() to List, Deque, SortedSet, and LinkedHashSet. No more size-1 arithmetic or manual reverse iteration.",
16+
"whyModernWins": [
17+
{
18+
"icon": "📖",
19+
"title": "Self-documenting",
20+
"desc": "getLast() is clearer than get(size()-1)."
21+
},
22+
{
23+
"icon": "🔄",
24+
"title": "Reversed view",
25+
"desc": "reversed() gives a view — no copying needed."
26+
},
27+
{
28+
"icon": "🧩",
29+
"title": "Uniform API",
30+
"desc": "Works the same on List, Deque, SortedSet."
31+
}
32+
],
33+
"support": "Widely available since JDK 21 LTS (Sept 2023)"
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"id": 27,
3+
"slug": "stream-toarray-typed",
4+
"title": "Typed stream toArray",
5+
"category": "collections",
6+
"difficulty": "beginner",
7+
"jdkVersion": "8",
8+
"oldLabel": "Pre-Streams",
9+
"modernLabel": "Java 8+",
10+
"oldApproach": "Manual Array Copy",
11+
"modernApproach": "toArray(generator)",
12+
"oldCode": "List<String> list = getNames();\nString[] arr = new String[list.size()];\nfor (int i = 0; i < list.size(); i++) {\n arr[i] = list.get(i);\n}",
13+
"modernCode": "String[] arr = getNames().stream()\n .filter(n -> n.length() > 3)\n .toArray(String[]::new);",
14+
"summary": "Convert streams to typed arrays with a method reference.",
15+
"explanation": "The toArray(IntFunction) method creates a properly typed array from a stream. The generator (String[]::new) tells the stream what type of array to create.",
16+
"whyModernWins": [
17+
{
18+
"icon": "🎯",
19+
"title": "Type-safe",
20+
"desc": "No Object[] cast — the array type is correct."
21+
},
22+
{
23+
"icon": "🔗",
24+
"title": "Chainable",
25+
"desc": "Works at the end of any stream pipeline."
26+
},
27+
{
28+
"icon": "📏",
29+
"title": "Concise",
30+
"desc": "One expression replaces the manual loop."
31+
}
32+
],
33+
"support": "Widely available since JDK 8 (March 2014)"
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"id": 25,
3+
"slug": "stream-tolist-shorthand",
4+
"title": "Stream toList() shorthand",
5+
"category": "collections",
6+
"difficulty": "beginner",
7+
"jdkVersion": "16",
8+
"oldLabel": "Java 8",
9+
"modernLabel": "Java 16+",
10+
"oldApproach": "Collectors.toList()",
11+
"modernApproach": ".toList()",
12+
"oldCode": "List<String> names = people.stream()\n .map(Person::name)\n .collect(Collectors.toList());",
13+
"modernCode": "List<String> names = people.stream()\n .map(Person::name)\n .toList();",
14+
"summary": "Collect a stream to an unmodifiable list without Collectors.",
15+
"explanation": "Stream.toList() is a terminal operation that returns an unmodifiable list. It's shorter than .collect(Collectors.toList()) and the result is immutable by default.",
16+
"whyModernWins": [
17+
{
18+
"icon": "📏",
19+
"title": "Shorter",
20+
"desc": ".toList() vs .collect(Collectors.toList())."
21+
},
22+
{
23+
"icon": "🔒",
24+
"title": "Immutable result",
25+
"desc": "Returns an unmodifiable list by default."
26+
},
27+
{
28+
"icon": "📖",
29+
"title": "Reads naturally",
30+
"desc": "stream → map → toList flows like English."
31+
}
32+
],
33+
"support": "Widely available since JDK 16 (March 2021)"
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"id": 28,
3+
"slug": "unmodifiable-collectors",
4+
"title": "Unmodifiable collectors",
5+
"category": "collections",
6+
"difficulty": "intermediate",
7+
"jdkVersion": "10",
8+
"oldLabel": "Java 8",
9+
"modernLabel": "Java 10+",
10+
"oldApproach": "collectingAndThen",
11+
"modernApproach": "toUnmodifiable*()",
12+
"oldCode": "List<String> list = stream.collect(\n Collectors.collectingAndThen(\n Collectors.toList(),\n Collections::unmodifiableList\n )\n);",
13+
"modernCode": "List<String> list = stream.collect(\n Collectors.toUnmodifiableList()\n);",
14+
"summary": "Collect directly to unmodifiable collections.",
15+
"explanation": "Java 10 added toUnmodifiableList(), toUnmodifiableSet(), and toUnmodifiableMap() collectors. No more wrapping with collectingAndThen.",
16+
"whyModernWins": [
17+
{
18+
"icon": "📏",
19+
"title": "Direct",
20+
"desc": "One collector instead of wrapping with collectingAndThen."
21+
},
22+
{
23+
"icon": "🔒",
24+
"title": "Immutable",
25+
"desc": "Result cannot be modified — no accidental mutations."
26+
},
27+
{
28+
"icon": "🚫",
29+
"title": "Null-safe",
30+
"desc": "Rejects null elements during collection."
31+
}
32+
],
33+
"support": "Widely available since JDK 10 (March 2018)"
34+
}

0 commit comments

Comments
 (0)