Skip to content

Commit 1f1630a

Browse files
Jose CarvajalJose Carvajal
authored andcommitted
Merge remote-tracking branch 'upstream/master'
2 parents 059e66d + 7f23c45 commit 1f1630a

43 files changed

Lines changed: 2748 additions & 761 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎core-java-8/pom.xml‎

Lines changed: 275 additions & 257 deletions
Large diffs are not rendered by default.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.counter;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.openjdk.jmh.annotations.Benchmark;
7+
import org.openjdk.jmh.annotations.BenchmarkMode;
8+
import org.openjdk.jmh.annotations.Fork;
9+
import org.openjdk.jmh.annotations.Mode;
10+
11+
import com.baeldung.counter.CounterUtil.MutableInteger;
12+
13+
@Fork(value = 1, warmups = 3)
14+
@BenchmarkMode(Mode.All)
15+
public class CounterStatistics {
16+
17+
private static final Map<String, Integer> counterMap = new HashMap<>();
18+
private static final Map<String, MutableInteger> counterWithMutableIntMap = new HashMap<>();
19+
private static final Map<String, int[]> counterWithIntArrayMap = new HashMap<>();
20+
private static final Map<String, Long> counterWithLongWrapperMap = new HashMap<>();
21+
22+
@Benchmark
23+
public void wrapperAsCounter() {
24+
CounterUtil.counterWithWrapperObject(counterMap);
25+
}
26+
27+
@Benchmark
28+
public void lambdaExpressionWithWrapper() {
29+
CounterUtil.counterWithLambdaAndWrapper(counterWithLongWrapperMap);
30+
}
31+
32+
@Benchmark
33+
public void mutableIntegerAsCounter() {
34+
CounterUtil.counterWithMutableInteger(counterWithMutableIntMap);
35+
}
36+
37+
@Benchmark
38+
public void primitiveArrayAsCounter() {
39+
CounterUtil.counterWithPrimitiveArray(counterWithIntArrayMap);
40+
}
41+
42+
public static void main(String[] args) throws Exception {
43+
org.openjdk.jmh.Main.main(args);
44+
}
45+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.counter;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import static org.junit.Assert.*;
7+
8+
import org.junit.Test;
9+
10+
import com.baeldung.counter.CounterUtil.MutableInteger;
11+
12+
public class CounterTest {
13+
14+
@Test
15+
public void whenMapWithWrapperAsCounter_runsSuccessfully() {
16+
Map<String, Integer> counterMap = new HashMap<>();
17+
CounterUtil.counterWithWrapperObject(counterMap);
18+
19+
assertEquals(3, counterMap.get("China")
20+
.intValue());
21+
assertEquals(2, counterMap.get("India")
22+
.intValue());
23+
}
24+
25+
@Test
26+
public void whenMapWithLambdaAndWrapperCounter_runsSuccessfully() {
27+
Map<String, Long> counterMap = new HashMap<>();
28+
CounterUtil.counterWithLambdaAndWrapper(counterMap);
29+
30+
assertEquals(3l, counterMap.get("China")
31+
.longValue());
32+
assertEquals(2l, counterMap.get("India")
33+
.longValue());
34+
}
35+
36+
@Test
37+
public void whenMapWithMutableIntegerCounter_runsSuccessfully() {
38+
Map<String, MutableInteger> counterMap = new HashMap<>();
39+
CounterUtil.counterWithMutableInteger(counterMap);
40+
assertEquals(3, counterMap.get("China")
41+
.getCount());
42+
assertEquals(2, counterMap.get("India")
43+
.getCount());
44+
}
45+
46+
@Test
47+
public void whenMapWithPrimitiveArray_runsSuccessfully() {
48+
Map<String, int[]> counterMap = new HashMap<>();
49+
CounterUtil.counterWithPrimitiveArray(counterMap);
50+
assertEquals(3, counterMap.get("China")[0]);
51+
assertEquals(2, counterMap.get("India")[0]);
52+
}
53+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.counter;
2+
3+
import java.util.Map;
4+
import java.util.stream.Collectors;
5+
import java.util.stream.Stream;
6+
7+
public class CounterUtil {
8+
9+
private final static String[] COUNTRY_NAMES = { "China", "Australia", "India", "USA", "USSR", "UK", "China", "France", "Poland", "Austria", "India", "USA", "Egypt", "China" };
10+
11+
public static void counterWithWrapperObject(Map<String, Integer> counterMap) {
12+
for (String country : COUNTRY_NAMES) {
13+
counterMap.compute(country, (k, v) -> v == null ? 1 : v + 1);
14+
}
15+
}
16+
17+
public static void counterWithLambdaAndWrapper(Map<String, Long> counterMap) {
18+
counterMap.putAll(Stream.of(COUNTRY_NAMES)
19+
.parallel()
20+
.collect(Collectors.groupingBy(k -> k, Collectors.counting())));
21+
}
22+
23+
public static class MutableInteger {
24+
int count;
25+
26+
public MutableInteger(int count) {
27+
this.count = count;
28+
}
29+
30+
public void increment() {
31+
this.count++;
32+
}
33+
34+
public int getCount() {
35+
return this.count;
36+
}
37+
}
38+
39+
public static void counterWithMutableInteger(Map<String, MutableInteger> counterMap) {
40+
for (String country : COUNTRY_NAMES) {
41+
MutableInteger oldValue = counterMap.get(country);
42+
if (oldValue != null) {
43+
oldValue.increment();
44+
} else {
45+
counterMap.put(country, new MutableInteger(1));
46+
}
47+
}
48+
}
49+
50+
public static void counterWithPrimitiveArray(Map<String, int[]> counterMap) {
51+
for (String country : COUNTRY_NAMES) {
52+
int[] oldCounter = counterMap.get(country);
53+
if (oldCounter != null) {
54+
oldCounter[0] += 1;
55+
} else {
56+
counterMap.put(country, new int[] { 1 });
57+
}
58+
}
59+
}
60+
61+
}

‎core-java/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,5 @@
120120
- [Guide to Java String Pool](http://www.baeldung.com/java-string-pool)
121121
- [Copy a File with Java](http://www.baeldung.com/java-copy-file)
122122
- [Introduction to Creational Design Patterns](http://www.baeldung.com/creational-design-patterns)
123+
- [Quick Example - Comparator vs Comparable in Java](http://www.baeldung.com/java-comparator-comparable)
123124

‎core-java/customers.xml‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?xml version="1.0"?>
2+
<webRowSet xmlns="http://java.sun.com/xml/ns/jdbc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://java.sun.com/xml/ns/jdbc http://java.sun.com/xml/ns/jdbc/webrowset.xsd">
4+
<properties>
5+
<command>SELECT * FROM customers</command>
6+
<concurrency>1008</concurrency>
7+
<datasource><null/></datasource>
8+
<escape-processing>true</escape-processing>
9+
<fetch-direction>1000</fetch-direction>
10+
<fetch-size>0</fetch-size>
11+
<isolation-level>2</isolation-level>
12+
<key-columns>
13+
</key-columns>
14+
<map>
15+
</map>
16+
<max-field-size>0</max-field-size>
17+
<max-rows>0</max-rows>
18+
<query-timeout>0</query-timeout>
19+
<read-only>true</read-only>
20+
<rowset-type>ResultSet.TYPE_SCROLL_INSENSITIVE</rowset-type>
21+
<show-deleted>false</show-deleted>
22+
<table-name>customers</table-name>
23+
<url>jdbc:h2:mem:testdb</url>
24+
<sync-provider>
25+
<sync-provider-name>com.sun.rowset.providers.RIOptimisticProvider</sync-provider-name>
26+
<sync-provider-vendor>Oracle Corporation</sync-provider-vendor>
27+
<sync-provider-version>1.0</sync-provider-version>
28+
<sync-provider-grade>2</sync-provider-grade>
29+
<data-source-lock>1</data-source-lock>
30+
</sync-provider>
31+
</properties>
32+
<metadata>
33+
<column-count>2</column-count>
34+
<column-definition>
35+
<column-index>1</column-index>
36+
<auto-increment>false</auto-increment>
37+
<case-sensitive>true</case-sensitive>
38+
<currency>false</currency>
39+
<nullable>0</nullable>
40+
<signed>true</signed>
41+
<searchable>true</searchable>
42+
<column-display-size>11</column-display-size>
43+
<column-label>ID</column-label>
44+
<column-name>ID</column-name>
45+
<schema-name>PUBLIC</schema-name>
46+
<column-precision>10</column-precision>
47+
<column-scale>0</column-scale>
48+
<table-name>CUSTOMERS</table-name>
49+
<catalog-name>TESTDB</catalog-name>
50+
<column-type>4</column-type>
51+
<column-type-name>INTEGER</column-type-name>
52+
</column-definition>
53+
<column-definition>
54+
<column-index>2</column-index>
55+
<auto-increment>false</auto-increment>
56+
<case-sensitive>true</case-sensitive>
57+
<currency>false</currency>
58+
<nullable>0</nullable>
59+
<signed>true</signed>
60+
<searchable>true</searchable>
61+
<column-display-size>50</column-display-size>
62+
<column-label>NAME</column-label>
63+
<column-name>NAME</column-name>
64+
<schema-name>PUBLIC</schema-name>
65+
<column-precision>50</column-precision>
66+
<column-scale>0</column-scale>
67+
<table-name>CUSTOMERS</table-name>
68+
<catalog-name>TESTDB</catalog-name>
69+
<column-type>12</column-type>
70+
<column-type-name>VARCHAR</column-type-name>
71+
</column-definition>
72+
</metadata>
73+
<data>
74+
<currentRow>
75+
<columnValue>1</columnValue>
76+
<columnValue>Customer1</columnValue>
77+
</currentRow>
78+
<currentRow>
79+
<columnValue>2</columnValue>
80+
<columnValue>Customer2</columnValue>
81+
</currentRow>
82+
<currentRow>
83+
<columnValue>3</columnValue>
84+
<columnValue>Customer3</columnValue>
85+
</currentRow>
86+
<currentRow>
87+
<columnValue>4</columnValue>
88+
<columnValue>Customer4</columnValue>
89+
</currentRow>
90+
<currentRow>
91+
<columnValue>5</columnValue>
92+
<columnValue>Customer5</columnValue>
93+
</currentRow>
94+
</data>
95+
</webRowSet>

0 commit comments

Comments
 (0)