11package org .baeldung .java .collections ;
22
3+ import com .google .common .collect .Sets ;
34import org .junit .Before ;
45import org .junit .Test ;
56
67import java .util .*;
78import java .util .stream .*;
89
10+ import static java .util .stream .Collectors .toList ;
11+ import static java .util .stream .Collectors .toSet ;
912import static org .hamcrest .CoreMatchers .equalTo ;
1013import static org .hamcrest .core .IsNot .not ;
1114import static org .junit .Assert .*;
1215
1316public 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 ()) {
0 commit comments