4 ms·
What's the point of this part of the generated Java code? (I split it out onto multiple lines for legibility) favoriteFoods = new java.util.LinkedHashSet<S
by koolba 8y ago
What's the point of this part of the generated Java code? (I split it out onto multiple lines for legibility)
favoriteFoods = new java.util.LinkedHashSet<String>(
this.favoriteFoods.size() < 1073741824
? 1 + this.favoriteFoods.size() + (this.favoriteFoods.size() - 3) / 3
: Integer.MAX_VALUE
);
favoriteFoods.addAll(this.favoriteFoods);
Why not just?
this.favoriteFoods = new java.util.LinkedHashSet<>(this.favoriteFoods);
- notamy 8y ago1073741824 is maximum (signed) integer size divided by 2. This is trying to optimize for the initial size of the set so that it doesn't have to allocate too many elements, since iirc Java's hashsets are backed by a hashmap implementation which has to be sized appropriately.
- koolba 8y agoI understand what it’s doing. I’m curious why it’s necessary to do it explicitly. Wouldn’t the optimal sizing be the default behavior when passing in the collection that will populate the set?