diff --git a/eclipse-collections-code-generator/src/main/resources/impl/set/mutable/primitiveHashSet.stg b/eclipse-collections-code-generator/src/main/resources/impl/set/mutable/primitiveHashSet.stg index dea93ec9a4..dbfd4c27fe 100644 --- a/eclipse-collections-code-generator/src/main/resources/impl/set/mutable/primitiveHashSet.stg +++ b/eclipse-collections-code-generator/src/main/resources/impl/set/mutable/primitiveHashSet.stg @@ -463,7 +463,17 @@ public class HashSet extends AbstractSet implements MutableSet { return Sets.immutable.with(this.Iterator().next()); } - HashSet mutableSet = HashSet.newSetWith(this.toArray()); + HashSet mutableSet; + if (this.table.length == smallestPowerOfTwoGreaterThan(this.size() \<\< 1)) // table is not too big + { + mutableSet = this; + this.copyOnWrite = true; + } + else + { + mutableSet = new HashSet(this.size()); + mutableSet.addAll(this); + } return new ImmutableHashSet(mutableSet.table, mutableSet.occupiedWithData, mutableSet.zeroToThirtyOne, mutableSet.zeroToThirtyOneOccupied); }