Skip to content

Commit 314c211

Browse files
zinch84pivovarit
authored andcommitted
Array list demo (eugenp#679)
* Create ArrayList demo * Update code samples according to the article * Use toCollection(ArrayList::new) instead of toList()
1 parent 7ba9a12 commit 314c211

1 file changed

Lines changed: 5 additions & 6 deletions

File tree

‎core-java/src/test/java/org/baeldung/java/collections/ArrayListTest.java‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
import java.util.*;
88
import java.util.stream.*;
99

10-
import static java.util.stream.Collectors.toList;
11-
import static java.util.stream.Collectors.toSet;
10+
import static java.util.stream.Collectors.*;
1211
import static org.hamcrest.CoreMatchers.equalTo;
1312
import static org.hamcrest.core.IsNot.not;
1413
import static org.junit.Assert.*;
@@ -22,7 +21,7 @@ public void setUp() {
2221
List<String> xs = LongStream.range(0, 16)
2322
.boxed()
2423
.map(Long::toHexString)
25-
.collect(toList());
24+
.collect(toCollection(ArrayList::new));
2625
stringsToSearch = new ArrayList<>(xs);
2726
stringsToSearch.addAll(xs);
2827
}
@@ -92,7 +91,7 @@ public void givenPredicate_whenIterateArrayList_thenFindAllElementsSatisfyingPre
9291
List<String> result = stringsToSearch
9392
.stream()
9493
.filter(matchingStrings::contains)
95-
.collect(toList());
94+
.collect(toCollection(ArrayList::new));
9695

9796
assertEquals(6, result.size());
9897
}
@@ -107,7 +106,7 @@ public void givenSortedArray_whenUseBinarySearch_thenFindElement() {
107106

108107
@Test
109108
public void givenIndex_whenRemove_thenCorrectElementRemoved() {
110-
List<Integer> xs = IntStream.range(0, 10).boxed().collect(toList());
109+
List<Integer> xs = IntStream.range(0, 10).boxed().collect(toCollection(ArrayList::new));
111110
Collections.reverse(xs);
112111

113112
xs.remove(0);
@@ -119,7 +118,7 @@ public void givenIndex_whenRemove_thenCorrectElementRemoved() {
119118

120119
@Test
121120
public void givenListIterator_whenReverseTraversal_thenRetrieveElementsInOppositeOrder() {
122-
List<Integer> xs = IntStream.range(0, 10).boxed().collect(toList());
121+
List<Integer> xs = IntStream.range(0, 10).boxed().collect(toCollection(ArrayList::new));
123122
ListIterator<Integer> it = xs.listIterator(xs.size());
124123
List<Integer> result = new ArrayList<>(xs.size());
125124
while (it.hasPrevious()) {

0 commit comments

Comments
 (0)