Skip to content

Commit 827e4b4

Browse files
committed
Java ArrayList
1 parent 910cb74 commit 827e4b4

4 files changed

Lines changed: 171 additions & 2 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.hellokoding.java.collections;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.Iterator;
7+
import java.util.List;
8+
9+
public class ArrayListIterateTest {
10+
@Test
11+
public void forIndex() {
12+
List<Integer> lst = new ArrayList<>();
13+
lst.add(1);
14+
lst.add(2);
15+
lst.add(3);
16+
17+
for(int i=0; i<lst.size(); i++){
18+
int ele = lst.get(i);
19+
System.out.println(ele);
20+
}
21+
}
22+
23+
@Test
24+
public void forEach() {
25+
List<Integer> lst = new ArrayList<>();
26+
lst.add(1);
27+
lst.add(2);
28+
lst.add(3);
29+
30+
for(int ele : lst){
31+
System.out.println(ele);
32+
}
33+
}
34+
35+
@Test
36+
public void forEachConsumer() {
37+
List<Integer> lst = new ArrayList<>();
38+
lst.add(1);
39+
lst.add(2);
40+
lst.add(3);
41+
42+
lst.forEach(System.out::println);
43+
}
44+
45+
@Test
46+
public void iterator() {
47+
List<Integer> lst = new ArrayList<>();
48+
lst.add(1);
49+
lst.add(2);
50+
lst.add(3);
51+
52+
Iterator<Integer> iterator = lst.iterator();
53+
while (iterator.hasNext()){
54+
System.out.println(iterator.next());
55+
}
56+
}
57+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.hellokoding.java.collections;
2+
3+
import org.junit.Test;
4+
5+
import java.util.*;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
public class ArrayListOverviewTest {
10+
@Test
11+
public void declare() {
12+
List<Integer> lst1 = new ArrayList<>();
13+
assertThat(lst1).isInstanceOf(ArrayList.class);
14+
15+
Collection<Integer> lst2 = new ArrayList<>();
16+
assertThat(lst2).isInstanceOf(ArrayList.class);
17+
18+
Iterable<Integer> lst3 = new ArrayList<>();
19+
assertThat(lst3).isInstanceOf(ArrayList.class);
20+
21+
ArrayList<Integer> lst4 = new ArrayList<>();
22+
}
23+
24+
@Test
25+
public void initialize() {
26+
// Create a new ArrayList
27+
List<Integer> lst = new ArrayList<>();
28+
29+
// Add elements to ArrayList
30+
lst.add(3);
31+
lst.add(1);
32+
lst.add(2);
33+
34+
// Can add null element
35+
lst.add(null);
36+
37+
// Can add duplicate element
38+
lst.add(2);
39+
assertThat(lst).hasSize(5);
40+
41+
// The output ordering will be same as the insertion oder
42+
System.out.println(lst);
43+
}
44+
}

‎java-examples/java-core/src/test/java/com/hellokoding/java/collections/ArrayListQueryTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void filter() {
2020
}
2121

2222
@Test
23-
public void get_OK() {
23+
public void get_thenOK() {
2424
List<Integer> lst = new ArrayList<>();
2525
lst.add(3);
2626
lst.add(1);
@@ -31,7 +31,7 @@ public void get_OK() {
3131
}
3232

3333
@Test(expected = IndexOutOfBoundsException.class)
34-
public void get_Exception() {
34+
public void get_thenException() {
3535
List<Integer> lst = new ArrayList<>();
3636
lst.add(3);
3737
lst.add(1);
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.hellokoding.java.concurrent;
2+
3+
import org.junit.Test;
4+
5+
import java.util.concurrent.CompletableFuture;
6+
import java.util.concurrent.ExecutorService;
7+
import java.util.concurrent.Executors;
8+
import java.util.concurrent.ForkJoinPool;
9+
10+
import static com.hellokoding.java.concurrent.ConcurrentUtils.stop;
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
13+
public class SynchronizedTest {
14+
private ExecutorService executorService = Executors.newFixedThreadPool(3);
15+
16+
@Test
17+
public void runAsync() {
18+
Counter counter = new Counter();
19+
20+
for (int i = 0; i < 10000; i++) {
21+
CompletableFuture.runAsync(counter::increaseRunnable);
22+
}
23+
24+
stop(ForkJoinPool.commonPool());
25+
26+
assertThat(counter.count).isEqualTo(10000);
27+
}
28+
29+
@Test
30+
public void runAsyncWithExecutorService() {
31+
Counter counter = new Counter();
32+
33+
for (int i = 0; i < 10000; i++) {
34+
CompletableFuture.runAsync(counter::increaseRunnable, executorService);
35+
}
36+
37+
stop(executorService);
38+
39+
assertThat(counter.count).isEqualTo(10000);
40+
}
41+
42+
@Test
43+
public void supplyAsync() {
44+
Counter counter = new Counter();
45+
46+
for (int i = 0; i < 10000; i++) {
47+
CompletableFuture.supplyAsync(counter::increaseSupplier, executorService);
48+
}
49+
50+
stop(executorService);
51+
52+
assertThat(counter.count).isEqualTo(10000);
53+
}
54+
55+
class Counter {
56+
int count = 0;
57+
58+
synchronized void increaseRunnable() {
59+
count++;
60+
}
61+
62+
synchronized int increaseSupplier() {
63+
count++;
64+
65+
return count;
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)