Skip to content

Commit b4e0135

Browse files
zinch84pivovarit
authored andcommitted
Create ArrayList demo (eugenp#643)
1 parent e318452 commit b4e0135

1 file changed

Lines changed: 148 additions & 0 deletions

File tree

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package org.baeldung.java.collections;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import java.util.*;
7+
import java.util.stream.*;
8+
9+
import static org.hamcrest.CoreMatchers.equalTo;
10+
import static org.hamcrest.core.IsNot.not;
11+
import static org.junit.Assert.*;
12+
13+
public class ArrayListTest {
14+
15+
List<String> stringsToSearch;
16+
17+
@Before
18+
public void setUp() {
19+
List<String> xs = LongStream.range(0, 16)
20+
.boxed()
21+
.map(Long::toHexString)
22+
.collect(Collectors.toList());
23+
stringsToSearch = new ArrayList<>(xs);
24+
stringsToSearch.addAll(xs);
25+
}
26+
27+
@Test
28+
public void givenNewArrayList_whenCheckCapacity_thenDefaultValue() {
29+
List<String> xs = new ArrayList<>();
30+
assertTrue(xs.isEmpty());
31+
}
32+
33+
@Test
34+
public void givenCollection_whenProvideItToArrayListCtor_thenArrayListIsPopulatedWithItsElements() {
35+
Collection<Integer> numbers =
36+
IntStream.range(0, 10).boxed().collect(Collectors.toSet());
37+
38+
List<Integer> xs = new ArrayList<>(numbers);
39+
assertEquals(10, xs.size());
40+
assertTrue(numbers.containsAll(xs));
41+
}
42+
43+
@Test
44+
public void givenElement_whenAddToArrayList_thenIsAdded() {
45+
List<Long> xs = new ArrayList<>();
46+
47+
xs.add(1L);
48+
xs.add(2L);
49+
xs.add(1, 3L);
50+
51+
assertThat(Arrays.asList(1L, 3L, 2L), equalTo(xs));
52+
}
53+
54+
@Test
55+
public void givenCollection_whenAddToArrayList_thenIsAdded() {
56+
List<Long> xs = new ArrayList<>(Arrays.asList(1L, 2L, 3L));
57+
Collection<Long> ys = LongStream.range(4, 10).boxed().collect(Collectors.toList());
58+
xs.addAll(0, ys);
59+
60+
assertThat(Arrays.asList(4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L), equalTo(xs));
61+
}
62+
63+
@Test
64+
public void givenExistingElement_whenCallIndexOf_thenReturnCorrectIndex() {
65+
assertEquals(10, stringsToSearch.indexOf("a"));
66+
assertEquals(26, stringsToSearch.lastIndexOf("a"));
67+
}
68+
69+
@Test
70+
public void givenCondition_whenIterateArrayList_thenFindAllElementsSatisfyingCondition() {
71+
Iterator<String> it = stringsToSearch.iterator();
72+
Set<String> matchingStrings = new HashSet<>(Arrays.asList("a", "c", "9"));
73+
74+
List<String> result = new ArrayList<>();
75+
while (it.hasNext()) {
76+
String s = it.next();
77+
if (matchingStrings.contains(s)) {
78+
result.add(s);
79+
}
80+
}
81+
82+
assertEquals(6, result.size());
83+
}
84+
85+
@Test
86+
public void givenPredicate_whenIterateArrayList_thenFindAllElementsSatisfyingPredicate() {
87+
Set<String> matchingStrings = new HashSet<>(Arrays.asList("a", "c", "9"));
88+
89+
List<String> result = stringsToSearch
90+
.stream()
91+
.filter(matchingStrings::contains)
92+
.collect(Collectors.toList());
93+
94+
assertEquals(6, result.size());
95+
}
96+
97+
@Test
98+
public void givenSortedArray_whenUseBinarySearch_thenFindElement() {
99+
List<String> copy = new ArrayList<>(stringsToSearch);
100+
Collections.sort(copy);
101+
int index = Collections.binarySearch(copy, "f");
102+
assertThat(index, not(equalTo(-1)));
103+
}
104+
105+
@Test
106+
public void givenIndex_whenRemove_thenCorrectElementRemoved() {
107+
List<Integer> xs = new ArrayList<>(
108+
IntStream.range(0, 10).boxed().collect(Collectors.toList())
109+
);
110+
Collections.reverse(xs);
111+
112+
xs.remove(0);
113+
assertThat(xs.get(0), equalTo(8));
114+
115+
xs.remove(Integer.valueOf(0));
116+
assertFalse(xs.contains(0));
117+
}
118+
119+
@Test
120+
public void givenListIterator_whenReverseTraversal_thenRetrieveElementsInOppositeOrder() {
121+
List<Integer> xs = new ArrayList<>(
122+
IntStream.range(0, 10).boxed().collect(Collectors.toList())
123+
);
124+
ListIterator<Integer> it = xs.listIterator(xs.size());
125+
List<Integer> result = new ArrayList<>(xs.size());
126+
while (it.hasPrevious()) {
127+
result.add(it.previous());
128+
}
129+
130+
Collections.reverse(xs);
131+
assertThat(result, equalTo(xs));
132+
}
133+
134+
@Test
135+
public void givenCondition_whenIterateArrayList_thenRemoveAllElementsSatisfyingCondition() {
136+
Set<String> matchingStrings
137+
= new HashSet<>(Arrays.asList("a", "b", "c", "d", "e", "f"));
138+
139+
Iterator<String> it = stringsToSearch.iterator();
140+
while (it.hasNext()) {
141+
if (matchingStrings.contains(it.next())) {
142+
it.remove();
143+
}
144+
}
145+
146+
assertEquals(20, stringsToSearch.size());
147+
}
148+
}

0 commit comments

Comments
 (0)