Array list demo (#679)
* Create ArrayList demo * Update code samples according to the article * Use toCollection(ArrayList::new) instead of toList()
This commit is contained in:
parent
7ba9a1288b
commit
314c211fa2
@ -7,8 +7,7 @@ import org.junit.Test;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.*;
|
import java.util.stream.*;
|
||||||
|
|
||||||
import static java.util.stream.Collectors.toList;
|
import static java.util.stream.Collectors.*;
|
||||||
import static java.util.stream.Collectors.toSet;
|
|
||||||
import static org.hamcrest.CoreMatchers.equalTo;
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
import static org.hamcrest.core.IsNot.not;
|
import static org.hamcrest.core.IsNot.not;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
@ -22,7 +21,7 @@ public class ArrayListTest {
|
|||||||
List<String> xs = LongStream.range(0, 16)
|
List<String> xs = LongStream.range(0, 16)
|
||||||
.boxed()
|
.boxed()
|
||||||
.map(Long::toHexString)
|
.map(Long::toHexString)
|
||||||
.collect(toList());
|
.collect(toCollection(ArrayList::new));
|
||||||
stringsToSearch = new ArrayList<>(xs);
|
stringsToSearch = new ArrayList<>(xs);
|
||||||
stringsToSearch.addAll(xs);
|
stringsToSearch.addAll(xs);
|
||||||
}
|
}
|
||||||
@ -92,7 +91,7 @@ public class ArrayListTest {
|
|||||||
List<String> result = stringsToSearch
|
List<String> result = stringsToSearch
|
||||||
.stream()
|
.stream()
|
||||||
.filter(matchingStrings::contains)
|
.filter(matchingStrings::contains)
|
||||||
.collect(toList());
|
.collect(toCollection(ArrayList::new));
|
||||||
|
|
||||||
assertEquals(6, result.size());
|
assertEquals(6, result.size());
|
||||||
}
|
}
|
||||||
@ -107,7 +106,7 @@ public class ArrayListTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenIndex_whenRemove_thenCorrectElementRemoved() {
|
public void givenIndex_whenRemove_thenCorrectElementRemoved() {
|
||||||
List<Integer> xs = IntStream.range(0, 10).boxed().collect(toList());
|
List<Integer> xs = IntStream.range(0, 10).boxed().collect(toCollection(ArrayList::new));
|
||||||
Collections.reverse(xs);
|
Collections.reverse(xs);
|
||||||
|
|
||||||
xs.remove(0);
|
xs.remove(0);
|
||||||
@ -119,7 +118,7 @@ public class ArrayListTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenListIterator_whenReverseTraversal_thenRetrieveElementsInOppositeOrder() {
|
public void givenListIterator_whenReverseTraversal_thenRetrieveElementsInOppositeOrder() {
|
||||||
List<Integer> xs = IntStream.range(0, 10).boxed().collect(toList());
|
List<Integer> xs = IntStream.range(0, 10).boxed().collect(toCollection(ArrayList::new));
|
||||||
ListIterator<Integer> it = xs.listIterator(xs.size());
|
ListIterator<Integer> it = xs.listIterator(xs.size());
|
||||||
List<Integer> result = new ArrayList<>(xs.size());
|
List<Integer> result = new ArrayList<>(xs.size());
|
||||||
while (it.hasPrevious()) {
|
while (it.hasPrevious()) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user