From 314c211fa2c91a5c6f0eac1964701897d2a9cd19 Mon Sep 17 00:00:00 2001 From: zinch84 Date: Sat, 10 Sep 2016 16:50:35 +0300 Subject: [PATCH] Array list demo (#679) * Create ArrayList demo * Update code samples according to the article * Use toCollection(ArrayList::new) instead of toList() --- .../org/baeldung/java/collections/ArrayListTest.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/core-java/src/test/java/org/baeldung/java/collections/ArrayListTest.java b/core-java/src/test/java/org/baeldung/java/collections/ArrayListTest.java index e93acf9a32..7a80d802e9 100644 --- a/core-java/src/test/java/org/baeldung/java/collections/ArrayListTest.java +++ b/core-java/src/test/java/org/baeldung/java/collections/ArrayListTest.java @@ -7,8 +7,7 @@ import org.junit.Test; import java.util.*; import java.util.stream.*; -import static java.util.stream.Collectors.toList; -import static java.util.stream.Collectors.toSet; +import static java.util.stream.Collectors.*; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.core.IsNot.not; import static org.junit.Assert.*; @@ -22,7 +21,7 @@ public class ArrayListTest { List xs = LongStream.range(0, 16) .boxed() .map(Long::toHexString) - .collect(toList()); + .collect(toCollection(ArrayList::new)); stringsToSearch = new ArrayList<>(xs); stringsToSearch.addAll(xs); } @@ -92,7 +91,7 @@ public class ArrayListTest { List result = stringsToSearch .stream() .filter(matchingStrings::contains) - .collect(toList()); + .collect(toCollection(ArrayList::new)); assertEquals(6, result.size()); } @@ -107,7 +106,7 @@ public class ArrayListTest { @Test public void givenIndex_whenRemove_thenCorrectElementRemoved() { - List xs = IntStream.range(0, 10).boxed().collect(toList()); + List xs = IntStream.range(0, 10).boxed().collect(toCollection(ArrayList::new)); Collections.reverse(xs); xs.remove(0); @@ -119,7 +118,7 @@ public class ArrayListTest { @Test public void givenListIterator_whenReverseTraversal_thenRetrieveElementsInOppositeOrder() { - List xs = IntStream.range(0, 10).boxed().collect(toList()); + List xs = IntStream.range(0, 10).boxed().collect(toCollection(ArrayList::new)); ListIterator it = xs.listIterator(xs.size()); List result = new ArrayList<>(xs.size()); while (it.hasPrevious()) {