r/java • • Jan 22 '26

Java 26: what’s new?

https://www.loicmathieu.fr/wordpress/informatique/java-26-whats-new/

What's new in Java 26 for us, developers

(Bot in English and French)

161 Upvotes

43 comments sorted by

View all comments

Show parent comment

3

u/cogman10 Jan 22 '26

Easily fixed with something like this.

if (o.size() > (elementData.length - size)) {
  resize(o.size());
}

var iter = o.iterator();
int i = size;
for (var e : o) {
  elementData[i] = e;
  ++i;
  if (i == elementData.length)
    grow();
}

Also array copy performs well

That's not so much the problem. The bigger issue is that you are iterating over every element in the collection twice while also allocating an array for those elements.

The array copy is very fast, but it's still a second iteration.