Skip to content

Commit 98ffbc1

Browse files
author
Grzegorz Piwowarek
committed
Refactor ArrayList examples
1 parent 6bb724d commit 98ffbc1

2 files changed

Lines changed: 11 additions & 16 deletions

File tree

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
package org.baeldung.java.collections;
22

3+
import com.google.common.collect.Sets;
34
import org.junit.Before;
45
import org.junit.Test;
56

67
import java.util.*;
78
import java.util.stream.*;
89

10+
import static java.util.stream.Collectors.toList;
11+
import static java.util.stream.Collectors.toSet;
912
import static org.hamcrest.CoreMatchers.equalTo;
1013
import static org.hamcrest.core.IsNot.not;
1114
import static org.junit.Assert.*;
1215

1316
public class ArrayListTest {
1417

15-
List<String> stringsToSearch;
18+
private List<String> stringsToSearch;
1619

1720
@Before
1821
public void setUp() {
1922
List<String> xs = LongStream.range(0, 16)
2023
.boxed()
2124
.map(Long::toHexString)
22-
.collect(Collectors.toList());
25+
.collect(toList());
2326
stringsToSearch = new ArrayList<>(xs);
2427
stringsToSearch.addAll(xs);
2528
}
@@ -33,7 +36,7 @@ public void givenNewArrayList_whenCheckCapacity_thenDefaultValue() {
3336
@Test
3437
public void givenCollection_whenProvideItToArrayListCtor_thenArrayListIsPopulatedWithItsElements() {
3538
Collection<Integer> numbers =
36-
IntStream.range(0, 10).boxed().collect(Collectors.toSet());
39+
IntStream.range(0, 10).boxed().collect(toSet());
3740

3841
List<Integer> xs = new ArrayList<>(numbers);
3942
assertEquals(10, xs.size());
@@ -54,7 +57,7 @@ public void givenElement_whenAddToArrayList_thenIsAdded() {
5457
@Test
5558
public void givenCollection_whenAddToArrayList_thenIsAdded() {
5659
List<Long> xs = new ArrayList<>(Arrays.asList(1L, 2L, 3L));
57-
Collection<Long> ys = LongStream.range(4, 10).boxed().collect(Collectors.toList());
60+
Collection<Long> ys = LongStream.range(4, 10).boxed().collect(toList());
5861
xs.addAll(0, ys);
5962

6063
assertThat(Arrays.asList(4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L), equalTo(xs));
@@ -89,7 +92,7 @@ public void givenPredicate_whenIterateArrayList_thenFindAllElementsSatisfyingPre
8992
List<String> result = stringsToSearch
9093
.stream()
9194
.filter(matchingStrings::contains)
92-
.collect(Collectors.toList());
95+
.collect(toList());
9396

9497
assertEquals(6, result.size());
9598
}
@@ -104,9 +107,7 @@ public void givenSortedArray_whenUseBinarySearch_thenFindElement() {
104107

105108
@Test
106109
public void givenIndex_whenRemove_thenCorrectElementRemoved() {
107-
List<Integer> xs = new ArrayList<>(
108-
IntStream.range(0, 10).boxed().collect(Collectors.toList())
109-
);
110+
List<Integer> xs = IntStream.range(0, 10).boxed().collect(toList());
110111
Collections.reverse(xs);
111112

112113
xs.remove(0);
@@ -118,9 +119,7 @@ public void givenIndex_whenRemove_thenCorrectElementRemoved() {
118119

119120
@Test
120121
public void givenListIterator_whenReverseTraversal_thenRetrieveElementsInOppositeOrder() {
121-
List<Integer> xs = new ArrayList<>(
122-
IntStream.range(0, 10).boxed().collect(Collectors.toList())
123-
);
122+
List<Integer> xs = IntStream.range(0, 10).boxed().collect(toList());
124123
ListIterator<Integer> it = xs.listIterator(xs.size());
125124
List<Integer> result = new ArrayList<>(xs.size());
126125
while (it.hasPrevious()) {
@@ -134,7 +133,7 @@ public void givenListIterator_whenReverseTraversal_thenRetrieveElementsInOpposit
134133
@Test
135134
public void givenCondition_whenIterateArrayList_thenRemoveAllElementsSatisfyingCondition() {
136135
Set<String> matchingStrings
137-
= new HashSet<>(Arrays.asList("a", "b", "c", "d", "e", "f"));
136+
= Sets.newHashSet("a", "b", "c", "d", "e", "f");
138137

139138
Iterator<String> it = stringsToSearch.iterator();
140139
while (it.hasNext()) {

‎reflection/src/main/java/com/baeldung/reflection/Animal.java‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ public void setName(String name) {
1818
this.name = name;
1919
}
2020

21-
public String makeSound() {
22-
return getSound();
23-
}
24-
2521
protected abstract String getSound();
2622

2723
}

0 commit comments

Comments
 (0)