Skip to content

Commit 5f89c09

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents abb541b + 9f38b7c commit 5f89c09

25 files changed

Lines changed: 630 additions & 37 deletions

File tree

‎core-java-9/src/test/java/com/baeldung/java9/language/stream/CollectorImprovementTest.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public void addComment(String comment) {
3131
}
3232
}
3333

34-
@Test
35-
public void testFiltering() {
34+
@Test
35+
public void givenList_whenSatifyPredicate_thenMapValueWithOccurences() {
3636
List<Integer> numbers = List.of(1, 2, 3, 5, 5);
3737

3838
Map<Integer, Long> result = numbers.stream()
@@ -49,7 +49,7 @@ public void testFiltering() {
4949
}
5050

5151
@Test
52-
public void testFlatMapping() {
52+
public void givenListOfBlogs_whenAuthorName_thenMapAuthorWithComments() {
5353
Blog blog1 = new CollectorImprovementTest.Blog("1");
5454
blog1.addComment("Nice");
5555
blog1.addComment("Very Nice");
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.concurrent.blockingqueue;
2+
3+
import java.util.concurrent.BlockingQueue;
4+
import java.util.concurrent.LinkedBlockingQueue;
5+
6+
public class BlockingQueueUsage {
7+
public static void main(String[] args) {
8+
int BOUND = 10;
9+
int N_PRODUCERS = 4;
10+
int N_CONSUMERS = Runtime.getRuntime().availableProcessors();
11+
12+
BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(BOUND);
13+
14+
for (int i = 0; i < N_PRODUCERS; i++) {
15+
new Thread(new NumbersProducer(queue)).start();
16+
}
17+
18+
for (int j = 0; j < N_CONSUMERS; j++) {
19+
new Thread(new NumbersConsumer(queue)).start();
20+
}
21+
}
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.concurrent.blockingqueue;
2+
3+
import java.util.concurrent.BlockingQueue;
4+
5+
public class NumbersConsumer implements Runnable {
6+
private final BlockingQueue<Integer> queue;
7+
8+
public NumbersConsumer(BlockingQueue<Integer> queue) {
9+
this.queue = queue;
10+
}
11+
12+
public void run() {
13+
try {
14+
while (true) {
15+
Integer number = queue.take();
16+
String result = number.toString();
17+
System.out.println(Thread.currentThread().getName() + " result: " + result);
18+
}
19+
} catch (InterruptedException e) {
20+
Thread.currentThread().interrupt();
21+
}
22+
}
23+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.concurrent.blockingqueue;
2+
3+
import java.util.concurrent.BlockingQueue;
4+
import java.util.concurrent.ThreadLocalRandom;
5+
6+
public class NumbersProducer implements Runnable {
7+
8+
private final BlockingQueue<Integer> numbersQueue;
9+
10+
public NumbersProducer(BlockingQueue<Integer> numbersQueue) {
11+
this.numbersQueue = numbersQueue;
12+
}
13+
14+
public void run() {
15+
try {
16+
generateNumbers();
17+
} catch (InterruptedException e) {
18+
Thread.currentThread().interrupt();
19+
}
20+
}
21+
22+
private void generateNumbers() throws InterruptedException {
23+
for (int i = 0; i < 100; i++) {
24+
numbersQueue.put(ThreadLocalRandom.current().nextInt(100));
25+
}
26+
}
27+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.baeldung.java.concurrentmap;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.concurrent.ConcurrentHashMap;
10+
import java.util.concurrent.ExecutorService;
11+
import java.util.concurrent.Executors;
12+
import java.util.concurrent.TimeUnit;
13+
14+
import static org.junit.Assert.assertEquals;
15+
import static org.junit.Assert.assertNotEquals;
16+
17+
public class ConcurrentMapAggregateStatusTest {
18+
19+
private ExecutorService executorService;
20+
private Map<String, Integer> concurrentMap;
21+
private List<Integer> mapSizes;
22+
private int MAX_SIZE = 100000;
23+
24+
@Before
25+
public void init() {
26+
executorService = Executors.newFixedThreadPool(2);
27+
concurrentMap = new ConcurrentHashMap<>();
28+
mapSizes = new ArrayList<>(MAX_SIZE);
29+
}
30+
31+
@Test
32+
public void givenConcurrentMap_whenSizeWithoutConcurrentUpdates_thenCorrect() throws InterruptedException {
33+
Runnable collectMapSizes = () -> {
34+
for (int i = 0; i < MAX_SIZE; i++) {
35+
concurrentMap.put(String.valueOf(i), i);
36+
mapSizes.add(concurrentMap.size());
37+
}
38+
};
39+
Runnable retrieveMapData = () -> {
40+
for (int i = 0; i < MAX_SIZE; i++) {
41+
concurrentMap.get(String.valueOf(i));
42+
}
43+
};
44+
executorService.execute(retrieveMapData);
45+
executorService.execute(collectMapSizes);
46+
executorService.shutdown();
47+
executorService.awaitTermination(1, TimeUnit.MINUTES);
48+
49+
for (int i = 1; i <= MAX_SIZE; i++) {
50+
assertEquals("map size should be consistently reliable", i, mapSizes
51+
.get(i - 1)
52+
.intValue());
53+
}
54+
assertEquals(MAX_SIZE, concurrentMap.size());
55+
}
56+
57+
@Test
58+
public void givenConcurrentMap_whenUpdatingAndGetSize_thenError() throws InterruptedException {
59+
Runnable collectMapSizes = () -> {
60+
for (int i = 0; i < MAX_SIZE; i++) {
61+
mapSizes.add(concurrentMap.size());
62+
}
63+
};
64+
Runnable updateMapData = () -> {
65+
for (int i = 0; i < MAX_SIZE; i++) {
66+
concurrentMap.put(String.valueOf(i), i);
67+
}
68+
};
69+
executorService.execute(updateMapData);
70+
executorService.execute(collectMapSizes);
71+
executorService.shutdown();
72+
executorService.awaitTermination(1, TimeUnit.MINUTES);
73+
74+
assertNotEquals("map size collected with concurrent updates not reliable", MAX_SIZE, mapSizes
75+
.get(MAX_SIZE - 1)
76+
.intValue());
77+
assertEquals(MAX_SIZE, concurrentMap.size());
78+
}
79+
80+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package com.baeldung.java.concurrentmap;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
import java.util.concurrent.ConcurrentHashMap;
9+
import java.util.concurrent.ConcurrentMap;
10+
11+
import static org.junit.Assert.assertNull;
12+
13+
public class ConcurrentMapNullKeyValueTest {
14+
15+
ConcurrentMap<String, Object> concurrentMap;
16+
17+
@Before
18+
public void setup() {
19+
concurrentMap = new ConcurrentHashMap<>();
20+
}
21+
22+
@Test(expected = NullPointerException.class)
23+
public void givenConcurrentHashMap_whenGetWithNullKey_thenThrowsNPE() {
24+
concurrentMap.get(null);
25+
}
26+
27+
@Test(expected = NullPointerException.class)
28+
public void givenConcurrentHashMap_whenGetOrDefaultWithNullKey_thenThrowsNPE() {
29+
concurrentMap.getOrDefault(null, new Object());
30+
}
31+
32+
@Test(expected = NullPointerException.class)
33+
public void givenConcurrentHashMap_whenPutWithNullKey_thenThrowsNPE() {
34+
concurrentMap.put(null, new Object());
35+
}
36+
37+
@Test(expected = NullPointerException.class)
38+
public void givenConcurrentHashMap_whenPutNullValue_thenThrowsNPE() {
39+
concurrentMap.put("test", null);
40+
}
41+
42+
@Test(expected = NullPointerException.class)
43+
public void givenConcurrentHashMapAndKeyAbsent_whenPutWithNullKey_thenThrowsNPE() {
44+
concurrentMap.putIfAbsent(null, new Object());
45+
}
46+
47+
@Test(expected = NullPointerException.class)
48+
public void givenConcurrentHashMapAndMapWithNullKey_whenPutNullKeyMap_thenThrowsNPE() {
49+
Map<String, Object> nullKeyMap = new HashMap<>();
50+
nullKeyMap.put(null, new Object());
51+
concurrentMap.putAll(nullKeyMap);
52+
}
53+
54+
@Test(expected = NullPointerException.class)
55+
public void givenConcurrentHashMapAndMapWithNullValue_whenPutNullValueMap_thenThrowsNPE() {
56+
Map<String, Object> nullValueMap = new HashMap<>();
57+
nullValueMap.put("test", null);
58+
concurrentMap.putAll(nullValueMap);
59+
}
60+
61+
@Test(expected = NullPointerException.class)
62+
public void givenConcurrentHashMap_whenReplaceNullKeyWithValues_thenThrowsNPE() {
63+
concurrentMap.replace(null, new Object(), new Object());
64+
}
65+
66+
@Test(expected = NullPointerException.class)
67+
public void givenConcurrentHashMap_whenReplaceWithNullNewValue_thenThrowsNPE() {
68+
Object o = new Object();
69+
concurrentMap.replace("test", o, null);
70+
}
71+
72+
@Test(expected = NullPointerException.class)
73+
public void givenConcurrentHashMap_whenReplaceOldNullValue_thenThrowsNPE() {
74+
Object o = new Object();
75+
concurrentMap.replace("test", null, o);
76+
}
77+
78+
@Test(expected = NullPointerException.class)
79+
public void givenConcurrentHashMap_whenReplaceWithNullValue_thenThrowsNPE() {
80+
concurrentMap.replace("test", null);
81+
}
82+
83+
@Test(expected = NullPointerException.class)
84+
public void givenConcurrentHashMap_whenReplaceNullKey_thenThrowsNPE() {
85+
concurrentMap.replace(null, "test");
86+
}
87+
88+
@Test(expected = NullPointerException.class)
89+
public void givenConcurrentHashMap_whenReplaceAllMappingNull_thenThrowsNPE() {
90+
concurrentMap.put("test", new Object());
91+
concurrentMap.replaceAll((s, o) -> null);
92+
}
93+
94+
@Test(expected = NullPointerException.class)
95+
public void givenConcurrentHashMap_whenRemoveNullKey_thenThrowsNPE() {
96+
concurrentMap.remove(null);
97+
}
98+
99+
@Test(expected = NullPointerException.class)
100+
public void givenConcurrentHashMap_whenRemoveNullKeyWithValue_thenThrowsNPE() {
101+
concurrentMap.remove(null, new Object());
102+
}
103+
104+
@Test(expected = NullPointerException.class)
105+
public void givenConcurrentHashMap_whenMergeNullKeyWithValue_thenThrowsNPE() {
106+
concurrentMap.merge(null, new Object(), (o, o2) -> o2);
107+
}
108+
109+
@Test(expected = NullPointerException.class)
110+
public void givenConcurrentHashMap_whenMergeKeyWithNullValue_thenThrowsNPE() {
111+
concurrentMap.put("test", new Object());
112+
concurrentMap.merge("test", null, (o, o2) -> o2);
113+
}
114+
115+
@Test(expected = NullPointerException.class)
116+
public void givenConcurrentHashMapAndAssumeKeyAbsent_whenComputeWithNullKey_thenThrowsNPE() {
117+
concurrentMap.computeIfAbsent(null, s -> s);
118+
}
119+
120+
@Test(expected = NullPointerException.class)
121+
public void givenConcurrentHashMapAndAssumeKeyPresent_whenComputeWithNullKey_thenThrowsNPE() {
122+
concurrentMap.computeIfPresent(null, (s, o) -> o);
123+
}
124+
125+
@Test(expected = NullPointerException.class)
126+
public void givenConcurrentHashMap_whenComputeWithNullKey_thenThrowsNPE() {
127+
concurrentMap.compute(null, (s, o) -> o);
128+
}
129+
130+
@Test
131+
public void givenConcurrentHashMap_whenMergeKeyRemappingNull_thenRemovesMapping() {
132+
Object oldValue = new Object();
133+
concurrentMap.put("test", oldValue);
134+
concurrentMap.merge("test", new Object(), (o, o2) -> null);
135+
assertNull(concurrentMap.get("test"));
136+
}
137+
138+
@Test
139+
public void givenConcurrentHashMapAndKeyAbsent_whenComputeWithKeyRemappingNull_thenRemainsAbsent() {
140+
concurrentMap.computeIfPresent("test", (s, o) -> null);
141+
assertNull(concurrentMap.get("test"));
142+
}
143+
144+
@Test
145+
public void givenKeyPresent_whenComputeIfPresentRemappingNull_thenMappingRemoved() {
146+
Object oldValue = new Object();
147+
concurrentMap.put("test", oldValue);
148+
concurrentMap.computeIfPresent("test", (s, o) -> null);
149+
assertNull(concurrentMap.get("test"));
150+
}
151+
152+
@Test
153+
public void givenKeyPresent_whenComputeRemappingNull_thenMappingRemoved() {
154+
Object oldValue = new Object();
155+
concurrentMap.put("test", oldValue);
156+
concurrentMap.compute("test", (s, o) -> null);
157+
assertNull(concurrentMap.get("test"));
158+
}
159+
160+
}

0 commit comments

Comments
 (0)