Refactor ArrayList examples
This commit is contained in:
parent
6bb724d828
commit
98ffbc1f37
@ -1,25 +1,28 @@
|
|||||||
package org.baeldung.java.collections;
|
package org.baeldung.java.collections;
|
||||||
|
|
||||||
|
import com.google.common.collect.Sets;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
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.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.*;
|
||||||
|
|
||||||
public class ArrayListTest {
|
public class ArrayListTest {
|
||||||
|
|
||||||
List<String> stringsToSearch;
|
private List<String> stringsToSearch;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
List<String> xs = LongStream.range(0, 16)
|
List<String> xs = LongStream.range(0, 16)
|
||||||
.boxed()
|
.boxed()
|
||||||
.map(Long::toHexString)
|
.map(Long::toHexString)
|
||||||
.collect(Collectors.toList());
|
.collect(toList());
|
||||||
stringsToSearch = new ArrayList<>(xs);
|
stringsToSearch = new ArrayList<>(xs);
|
||||||
stringsToSearch.addAll(xs);
|
stringsToSearch.addAll(xs);
|
||||||
}
|
}
|
||||||
@ -33,7 +36,7 @@ public class ArrayListTest {
|
|||||||
@Test
|
@Test
|
||||||
public void givenCollection_whenProvideItToArrayListCtor_thenArrayListIsPopulatedWithItsElements() {
|
public void givenCollection_whenProvideItToArrayListCtor_thenArrayListIsPopulatedWithItsElements() {
|
||||||
Collection<Integer> numbers =
|
Collection<Integer> numbers =
|
||||||
IntStream.range(0, 10).boxed().collect(Collectors.toSet());
|
IntStream.range(0, 10).boxed().collect(toSet());
|
||||||
|
|
||||||
List<Integer> xs = new ArrayList<>(numbers);
|
List<Integer> xs = new ArrayList<>(numbers);
|
||||||
assertEquals(10, xs.size());
|
assertEquals(10, xs.size());
|
||||||
@ -54,7 +57,7 @@ public class ArrayListTest {
|
|||||||
@Test
|
@Test
|
||||||
public void givenCollection_whenAddToArrayList_thenIsAdded() {
|
public void givenCollection_whenAddToArrayList_thenIsAdded() {
|
||||||
List<Long> xs = new ArrayList<>(Arrays.asList(1L, 2L, 3L));
|
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);
|
xs.addAll(0, ys);
|
||||||
|
|
||||||
assertThat(Arrays.asList(4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L), equalTo(xs));
|
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
|
List<String> result = stringsToSearch
|
||||||
.stream()
|
.stream()
|
||||||
.filter(matchingStrings::contains)
|
.filter(matchingStrings::contains)
|
||||||
.collect(Collectors.toList());
|
.collect(toList());
|
||||||
|
|
||||||
assertEquals(6, result.size());
|
assertEquals(6, result.size());
|
||||||
}
|
}
|
||||||
@ -104,9 +107,7 @@ public class ArrayListTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenIndex_whenRemove_thenCorrectElementRemoved() {
|
public void givenIndex_whenRemove_thenCorrectElementRemoved() {
|
||||||
List<Integer> xs = new ArrayList<>(
|
List<Integer> xs = IntStream.range(0, 10).boxed().collect(toList());
|
||||||
IntStream.range(0, 10).boxed().collect(Collectors.toList())
|
|
||||||
);
|
|
||||||
Collections.reverse(xs);
|
Collections.reverse(xs);
|
||||||
|
|
||||||
xs.remove(0);
|
xs.remove(0);
|
||||||
@ -118,9 +119,7 @@ public class ArrayListTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenListIterator_whenReverseTraversal_thenRetrieveElementsInOppositeOrder() {
|
public void givenListIterator_whenReverseTraversal_thenRetrieveElementsInOppositeOrder() {
|
||||||
List<Integer> xs = new ArrayList<>(
|
List<Integer> xs = IntStream.range(0, 10).boxed().collect(toList());
|
||||||
IntStream.range(0, 10).boxed().collect(Collectors.toList())
|
|
||||||
);
|
|
||||||
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()) {
|
||||||
@ -134,7 +133,7 @@ public class ArrayListTest {
|
|||||||
@Test
|
@Test
|
||||||
public void givenCondition_whenIterateArrayList_thenRemoveAllElementsSatisfyingCondition() {
|
public void givenCondition_whenIterateArrayList_thenRemoveAllElementsSatisfyingCondition() {
|
||||||
Set<String> matchingStrings
|
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();
|
Iterator<String> it = stringsToSearch.iterator();
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
|
@ -18,10 +18,6 @@ public abstract class Animal implements Eating {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String makeSound() {
|
|
||||||
return getSound();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected abstract String getSound();
|
protected abstract String getSound();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user