Skip to content

Commit 7285447

Browse files
eralmas7pivovarit
authored andcommitted
Counter (eugenp#3223)
* BAEL-1375 Counter in Java. * BAEL-1375 Frequency counter in java. * BAEL-1375 Refactored method names. * BAEL-1375 refactored code. * BAEL-1375 Fixed the test * BAEL-1375 updated code to include parallel stream.
1 parent 284eeb8 commit 7285447

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

‎core-java-8/src/test/java/com/baeldung/counter/CounterStatistics.java‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.HashMap;
44
import java.util.Map;
5+
import java.util.Random;
56

67
import org.openjdk.jmh.annotations.Benchmark;
78
import org.openjdk.jmh.annotations.BenchmarkMode;
@@ -18,8 +19,18 @@ public class CounterStatistics {
1819
private static final Map<String, MutableInteger> counterWithMutableIntMap = new HashMap<>();
1920
private static final Map<String, int[]> counterWithIntArrayMap = new HashMap<>();
2021
private static final Map<String, Long> counterWithLongWrapperMap = new HashMap<>();
22+
private static final Map<String, Long> counterWithLongWrapperStreamMap = new HashMap<>();
23+
24+
static {
25+
CounterUtil.COUNTRY_NAMES = new String[10000];
26+
final String prefix = "NewString";
27+
Random random = new Random();
28+
for (int i=0; i<10000; i++) {
29+
CounterUtil.COUNTRY_NAMES[i] = new String(prefix + random.nextInt(1000));
30+
}
31+
}
2132

22-
@Benchmark
33+
@Benchmark
2334
public void wrapperAsCounter() {
2435
CounterUtil.counterWithWrapperObject(counterMap);
2536
}
@@ -28,6 +39,11 @@ public void wrapperAsCounter() {
2839
public void lambdaExpressionWithWrapper() {
2940
CounterUtil.counterWithLambdaAndWrapper(counterWithLongWrapperMap);
3041
}
42+
43+
@Benchmark
44+
public void parallelStreamWithWrapper() {
45+
CounterUtil.counterWithParallelStreamAndWrapper(counterWithLongWrapperStreamMap);
46+
}
3147

3248
@Benchmark
3349
public void mutableIntegerAsCounter() {

‎core-java-8/src/test/java/com/baeldung/counter/CounterUtil.java‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
public class CounterUtil {
88

9-
private final static String[] COUNTRY_NAMES = { "China", "Australia", "India", "USA", "USSR", "UK", "China", "France", "Poland", "Austria", "India", "USA", "Egypt", "China" };
9+
public static String[] COUNTRY_NAMES = { "China", "Australia", "India", "USA", "USSR", "UK", "China", "France", "Poland", "Austria", "India", "USA", "Egypt", "China" };
1010

1111
public static void counterWithWrapperObject(Map<String, Integer> counterMap) {
1212
for (String country : COUNTRY_NAMES) {
@@ -15,9 +15,14 @@ public static void counterWithWrapperObject(Map<String, Integer> counterMap) {
1515
}
1616

1717
public static void counterWithLambdaAndWrapper(Map<String, Long> counterMap) {
18-
counterMap.putAll(Stream.of(COUNTRY_NAMES)
18+
Stream.of(COUNTRY_NAMES)
19+
.collect(Collectors.groupingBy(k -> k, () -> counterMap, Collectors.counting()));
20+
}
21+
22+
public static void counterWithParallelStreamAndWrapper(Map<String, Long> counterMap) {
23+
Stream.of(COUNTRY_NAMES)
1924
.parallel()
20-
.collect(Collectors.groupingBy(k -> k, Collectors.counting())));
25+
.collect(Collectors.groupingBy(k -> k, () -> counterMap, Collectors.counting()));
2126
}
2227

2328
public static class MutableInteger {
@@ -49,4 +54,4 @@ public static void counterWithPrimitiveArray(Map<String, int[]> counterMap) {
4954
}
5055
}
5156

52-
}
57+
}

0 commit comments

Comments
 (0)