Skip to content

Commit d735465

Browse files
authored
Merge pull request #144 from javaevolved/copilot/fix-stream-toarray-typed-issue
Fix stream-toarray-typed: old code missing filter(length > 3)
2 parents f0470ed + 5716f4f commit d735465

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

content/collections/stream-toarray-typed.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ oldApproach: "Manual Array Copy"
1111
modernApproach: "toArray(generator)"
1212
oldCode: |-
1313
List<String> list = getNames();
14-
String[] arr = new String[list.size()];
15-
for (int i = 0; i < list.size(); i++) {
16-
arr[i] = list.get(i);
14+
List<String> filtered = new ArrayList<>();
15+
for (String n : list) {
16+
if (n.length() > 3) {
17+
filtered.add(n);
18+
}
1719
}
20+
String[] arr = filtered.toArray(new String[0]);
1821
modernCode: |-
1922
String[] arr = getNames().stream()
2023
.filter(n -> n.length() > 3)

0 commit comments

Comments
 (0)