Skip to content

Commit e41501e

Browse files
authored
Merge pull request #1 from darshitpp/sync/upstream-update
Sync: Update patterns from java.evolved (2026-03-25)
2 parents bc564e6 + 506b245 commit e41501e

3 files changed

Lines changed: 13 additions & 9 deletions

File tree

references/collections.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ List<String> copy =
5959
### Why modern wins
6060
- **Smart copy:** Skips the copy if the source is already immutable.
6161
- **One call:** No manual ArrayList construction + wrapping.
62-
- **Defensive copy:** Changes to the original don't affect the copy.
62+
- **Any Collection:** Accepts any Collection as input—no intermediate ArrayList conversion needed.
6363

6464
### References
6565
- [List.copyOf()](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/List.html#copyOf(java.util.Collection))
@@ -262,17 +262,20 @@ var reversed = list.reversed();
262262
263263
## Typed stream toArray
264264
- **Since:** Java 8
265-
- **Old approach:** Manual Array Copy (Pre-Streams)
265+
- **Old approach:** Manual Filter + Copy (Pre-Streams)
266266
- **Modern approach:** toArray(generator) (Java 8+)
267-
- **Summary:** Convert streams to typed arrays with a method reference.
267+
- **Summary:** Filter a collection and collect the results to a typed array using a single stream expression.
268268
269269
### Before
270270
```java
271271
List<String> list = getNames();
272-
String[] arr = new String[list.size()];
273-
for (int i = 0; i < list.size(); i++) {
274-
arr[i] = list.get(i);
272+
List<String> filtered = new ArrayList<>();
273+
for (String n : list) {
274+
if (n.length() > 3) {
275+
filtered.add(n);
276+
}
275277
}
278+
String[] arr = filtered.toArray(new String[0]);
276279
```
277280
278281
### After
@@ -285,7 +288,7 @@ String[] arr = getNames().stream()
285288
### Why modern wins
286289
- **Type-safe:** No Object[] cast — the array type is correct.
287290
- **Chainable:** Works at the end of any stream pipeline.
288-
- **Concise:** One expression replaces the manual loop.
291+
- **Concise:** No intermediate list — one expression replaces the manual loop and copy.
289292
290293
### References
291294
- [Stream.toArray()](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/stream/Stream.html#toArray(java.util.function.IntFunction))

references/detection-patterns.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ _Auto-generated from upstream YAML data. Do not edit manually._
1515
- **map-entry-factory** (Java 9+): Old=`SimpleEntry` | Detect: `SimpleEntry`
1616
- **reverse-list-iteration** (Java 21+): Old=`Manual ListIterator` | Detect: `System.out.println(`, `list.listIterator(list.size())`, `it.hasPrevious()`, `it.previous()`, `out.println(element)`
1717
- **sequenced-collections** (Java 21+): Old=`Index Arithmetic` | Detect: `list.get(list.size() - 1)`, `list.get(0)`
18-
- **stream-toarray-typed** (Java 8+): Old=`Manual Array Copy` | Detect: `list.size()`, `list.get(i)`
18+
- **stream-toarray-typed** (Java 8+): Old=`Manual Filter + Copy` | Detect: `new ArrayList(`, `n.length()`, `filtered.add(n)`
1919
- **unmodifiable-collectors** (Java 16+): Old=`collectingAndThen` | Detect: `Collectors.collectingAndThen(`, `Collectors.toList(`
2020

2121
## Concurrency

references/language.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,8 @@ class Square extends Shape {
312312

313313
### Before
314314
```java
315-
if (shape instanceof Circle c) {
315+
if (shape instanceof Circle) {
316+
Circle c = (Circle) shape;
316317
if (c.radius() > 10) {
317318
return "large circle";
318319
} else {

0 commit comments

Comments
 (0)