@@ -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
271271List<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))
0 commit comments