Skip to content

Commit

Permalink
toImmutable() method on FastList avoids creating a redundant array copy
Browse files Browse the repository at this point in the history
Signed-off-by: vmzakharov <zakharov.vladimir.m@gmail.com>
  • Loading branch information
vmzakharov committed May 14, 2020
1 parent cc17ca5 commit 753dae6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ private <T> ImmutableList<T> withList(List<T> items)
return this.of(items.get(0), items.get(1), items.get(2), items.get(3), items.get(4), items.get(5), items.get(6), items.get(7), items.get(8), items.get(9));

default:
return ImmutableArrayList.newListWith((T[]) items.toArray());
return ImmutableArrayList.newList(items);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.collections.api.block.function.Function0;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.tuple.Twin;
Expand Down Expand Up @@ -1203,6 +1204,15 @@ public void testWith()
FastList.newListWith(42).withAll(Interval.from(10).to(12).toList()));
}

@Test
public void unoptimizedListToImmutable()
{
FastList<String> list = FastList.newListWith(
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15");
ImmutableList<String> immutableList = list.toImmutable();
Verify.assertIterablesEqual(immutableList, list);
}

@Test(expected = NoSuchElementException.class)
public void min_empty_throws_without_comparator()
{
Expand Down

0 comments on commit 753dae6

Please sign in to comment.