Refactor ArrayList examples

This commit is contained in:
Grzegorz Piwowarek 2016-09-04 10:02:09 +02:00
parent 6bb724d828
commit 98ffbc1f37
2 changed files with 11 additions and 16 deletions

View File

@ -1,25 +1,28 @@
package org.baeldung.java.collections;
import com.google.common.collect.Sets;
import org.junit.Before;
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 org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.*;
public class ArrayListTest {
List<String> stringsToSearch;
private List<String> stringsToSearch;
@Before
public void setUp() {
List<String> xs = LongStream.range(0, 16)
.boxed()
.map(Long::toHexString)
.collect(Collectors.toList());
.collect(toList());
stringsToSearch = new ArrayList<>(xs);
stringsToSearch.addAll(xs);
}
@ -33,7 +36,7 @@ public class ArrayListTest {
@Test
public void givenCollection_whenProvideItToArrayListCtor_thenArrayListIsPopulatedWithItsElements() {
Collection<Integer> numbers =
IntStream.range(0, 10).boxed().collect(Collectors.toSet());
IntStream.range(0, 10).boxed().collect(toSet());
List<Integer> xs = new ArrayList<>(numbers);
assertEquals(10, xs.size());
@ -54,7 +57,7 @@ public class ArrayListTest {
@Test
public void givenCollection_whenAddToArrayList_thenIsAdded() {
List<Long> xs = new ArrayList<>(Arrays.asList(1L, 2L, 3L));
Collection<Long> ys = LongStream.range(4, 10).boxed().collect(Collectors.toList());
Collection<Long> ys = LongStream.range(4, 10).boxed().collect(toList());
xs.addAll(0, ys);
assertThat(Arrays.asList(4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L), equalTo(xs));
@ -89,7 +92,7 @@ public class ArrayListTest {
List<String> result = stringsToSearch
.stream()
.filter(matchingStrings::contains)
.collect(Collectors.toList());
.collect(toList());
assertEquals(6, result.size());
}
@ -104,9 +107,7 @@ public class ArrayListTest {
@Test
public void givenIndex_whenRemove_thenCorrectElementRemoved() {
List<Integer> xs = new ArrayList<>(
IntStream.range(0, 10).boxed().collect(Collectors.toList())
);
List<Integer> xs = IntStream.range(0, 10).boxed().collect(toList());
Collections.reverse(xs);
xs.remove(0);
@ -118,9 +119,7 @@ public class ArrayListTest {
@Test
public void givenListIterator_whenReverseTraversal_thenRetrieveElementsInOppositeOrder() {
List<Integer> xs = new ArrayList<>(
IntStream.range(0, 10).boxed().collect(Collectors.toList())
);
List<Integer> xs = IntStream.range(0, 10).boxed().collect(toList());
ListIterator<Integer> it = xs.listIterator(xs.size());
List<Integer> result = new ArrayList<>(xs.size());
while (it.hasPrevious()) {
@ -134,7 +133,7 @@ public class ArrayListTest {
@Test
public void givenCondition_whenIterateArrayList_thenRemoveAllElementsSatisfyingCondition() {
Set<String> matchingStrings
= new HashSet<>(Arrays.asList("a", "b", "c", "d", "e", "f"));
= Sets.newHashSet("a", "b", "c", "d", "e", "f");
Iterator<String> it = stringsToSearch.iterator();
while (it.hasNext()) {

View File

@ -18,10 +18,6 @@ public abstract class Animal implements Eating {
this.name = name;
}
public String makeSound() {
return getSound();
}
protected abstract String getSound();
}