Skip to content

Commit 22c9996

Browse files
committed
Merge remote-tracking branch 'eugenp/master'
2 parents 998d02b + f046857 commit 22c9996

22 files changed

Lines changed: 554 additions & 2 deletions

File tree

‎core-java-collections/pom.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@
6868
<artifactId>commons-exec</artifactId>
6969
<version>1.3</version>
7070
</dependency>
71+
<dependency>
72+
<groupId>one.util</groupId>
73+
<artifactId>streamex</artifactId>
74+
<version>0.6.5</version>
75+
</dependency>
7176

7277
</dependencies>
7378

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.baeldung.map.java_8;
2+
3+
import com.baeldung.sort.Employee;
4+
import one.util.streamex.EntryStream;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
import java.util.stream.Collectors;
8+
import java.util.stream.Stream;
9+
10+
public class MergeMaps {
11+
12+
private static Map<String, Employee> map1 = new HashMap<>();
13+
private static Map<String, Employee> map2 = new HashMap<>();
14+
15+
public static void main(String[] args) {
16+
17+
initialize();
18+
19+
mergeFunction();
20+
21+
streamConcat();
22+
23+
streamOf();
24+
25+
streamEx();
26+
27+
streamMerge();
28+
}
29+
30+
private static void streamMerge() {
31+
32+
Map<String, Employee> map3 = map2.entrySet()
33+
.stream()
34+
.collect(
35+
Collectors.toMap(
36+
Map.Entry::getKey,
37+
Map.Entry::getValue,
38+
(v1, v2) -> new Employee(v1.getId(), v2.getName()),
39+
() -> new HashMap<>(map1)
40+
)
41+
);
42+
43+
System.out.println(map3);
44+
}
45+
46+
private static void streamEx() {
47+
Map<String, Employee> map3 = EntryStream.of(map1)
48+
.append(EntryStream.of(map2))
49+
.toMap((e1, e2) -> e1);
50+
51+
System.out.println(map3);
52+
53+
}
54+
55+
private static void streamOf() {
56+
Map<String, Employee> map3 = Stream.of(map1, map2)
57+
.flatMap(map -> map.entrySet().stream())
58+
.collect(
59+
Collectors.toMap(
60+
Map.Entry::getKey,
61+
Map.Entry::getValue,
62+
(v1, v2) -> new Employee(v1.getId(), v2.getName())
63+
)
64+
);
65+
66+
map3.entrySet().forEach(System.out::println);
67+
}
68+
69+
private static void streamConcat() {
70+
Map<String, Employee> result = Stream.concat(map1.entrySet().stream(), map2.entrySet().stream()).collect(Collectors.toMap(
71+
Map.Entry::getKey,
72+
Map.Entry::getValue,
73+
(value1, value2) -> new Employee(value2.getId(), value1.getName())
74+
));
75+
76+
result.entrySet().forEach(System.out::println);
77+
}
78+
79+
private static void mergeFunction() {
80+
Map<String, Employee> map3 = new HashMap<>(map1);
81+
82+
map2.forEach(
83+
(key, value) -> map3.merge(key, value, (v1, v2) ->
84+
new Employee(v1.getId(), v2.getName()))
85+
);
86+
87+
map3.entrySet().forEach(System.out::println);
88+
}
89+
90+
91+
private static void initialize() {
92+
Employee employee1 = new Employee(1L, "Henry");
93+
map1.put(employee1.getName(), employee1);
94+
Employee employee2 = new Employee(22L, "Annie");
95+
map1.put(employee2.getName(), employee2);
96+
Employee employee3 = new Employee(8L, "John");
97+
map1.put(employee3.getName(), employee3);
98+
99+
Employee employee4 = new Employee(2L, "George");
100+
map2.put(employee4.getName(), employee4);
101+
Employee employee5 = new Employee(3L, "Henry");
102+
map2.put(employee5.getName(), employee5);
103+
}
104+
105+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.java.map;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.ArrayList;
6+
import java.util.Collection;
7+
import java.util.HashMap;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
import org.apache.commons.collections4.MultiMap;
12+
import org.apache.commons.collections4.MultiMapUtils;
13+
import org.apache.commons.collections4.MultiValuedMap;
14+
import org.apache.commons.collections4.map.MultiValueMap;
15+
import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
16+
import org.apache.commons.collections4.multimap.HashSetValuedHashMap;
17+
import org.junit.Test;
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
21+
import com.google.common.collect.ArrayListMultimap;
22+
import com.google.common.collect.LinkedHashMultimap;
23+
import com.google.common.collect.Multimap;
24+
import com.google.common.collect.TreeMultimap;
25+
26+
27+
public class KeyCheckTest {
28+
29+
@Test
30+
public void whenKeyIsPresent_thenContainsKeyReturnsTrue() {
31+
Map<String, String> map = Collections.singletonMap("key", "value");
32+
33+
assertTrue(map.containsKey("key"));
34+
assertFalse(map.containsKey("missing"));
35+
}
36+
37+
@Test
38+
public void whenKeyHasNullValue_thenGetStillWorks() {
39+
Map<String, String> map = Collections.singletonMap("nothing", null);
40+
41+
assertTrue(map.containsKey("nothing"));
42+
assertNull(map.get("nothing"));
43+
}
44+
}

‎core-java/README.md‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
- [Guide to sun.misc.Unsafe](http://www.baeldung.com/java-unsafe)
3737
- [How to Perform a Simple HTTP Request in Java](http://www.baeldung.com/java-http-request)
3838
- [Call Methods at Runtime Using Java Reflection](http://www.baeldung.com/java-method-reflection)
39-
- [Guide to UUID in JAVA](http://www.baeldung.com/guide-to-uuid-in-java)
4039
- [How to Add a Single Element to a Stream](http://www.baeldung.com/java-stream-append-prepend)
4140
- [Iterating Over Enum Values in Java](http://www.baeldung.com/java-enum-iteration)
4241
- [Kotlin Java Interoperability](http://www.baeldung.com/kotlin-java-interoperability)

‎jackson/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118

119119
<properties>
120120
<!-- marshalling -->
121-
<jackson.version>2.9.6</jackson.version>
121+
<jackson.version>2.9.7</jackson.version>
122122
<!-- util -->
123123
<commons-lang3.version>3.8</commons-lang3.version>
124124
<joda-time.version>2.10</joda-time.version>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.jackson.xmlToJson;
2+
3+
public enum Color {
4+
PINK, BLUE, YELLOW, RED;
5+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.jackson.xmlToJson;
2+
3+
public class Flower {
4+
5+
private String name;
6+
7+
private Color color;
8+
9+
private Integer petals;
10+
11+
public Flower() { }
12+
13+
public Flower(String name, Color color, Integer petals) {
14+
this.name = name;
15+
this.color = color;
16+
this.petals = petals;
17+
}
18+
19+
public String getName() {
20+
return name;
21+
}
22+
23+
public void setName(String name) {
24+
this.name = name;
25+
}
26+
27+
public Color getColor() {
28+
return color;
29+
}
30+
31+
public void setColor(Color color) {
32+
this.color = color;
33+
}
34+
35+
public Integer getPetals() {
36+
return petals;
37+
}
38+
39+
public void setPetals(Integer petals) {
40+
this.petals = petals;
41+
}
42+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.jackson.xmlToJson;
2+
3+
4+
import com.fasterxml.jackson.core.JsonProcessingException;
5+
import com.fasterxml.jackson.databind.JsonNode;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
8+
import org.junit.Test;
9+
10+
import static org.junit.Assert.*;
11+
12+
import java.io.IOException;
13+
14+
public class XmlToJsonUnitTest {
15+
16+
@Test
17+
public void givenAnXML_whenUseDataBidingToConvertToJSON_thenReturnDataOK() {
18+
String flowerXML = "<Flower><name>Poppy</name><color>RED</color><petals>9</petals></Flower>";
19+
20+
try {
21+
XmlMapper xmlMapper = new XmlMapper();
22+
Flower poppy = xmlMapper.readValue(flowerXML, Flower.class);
23+
24+
assertEquals(poppy.getName(), "Poppy");
25+
assertEquals(poppy.getColor(), Color.RED);
26+
assertEquals(poppy.getPetals(), new Integer(9));
27+
28+
ObjectMapper mapper = new ObjectMapper();
29+
String json = mapper.writeValueAsString(poppy);
30+
31+
assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":9}");
32+
System.out.println(json);
33+
} catch(IOException e) {
34+
e.printStackTrace();
35+
}
36+
}
37+
38+
@Test
39+
public void givenAnXML_whenUseATreeConvertToJSON_thenReturnDataOK() {
40+
String flowerXML = "<Flower><name>Poppy</name><color>RED</color><petals>9</petals></Flower>";
41+
42+
try {
43+
XmlMapper xmlMapper = new XmlMapper();
44+
JsonNode node = xmlMapper.readTree(flowerXML.getBytes());
45+
46+
ObjectMapper jsonMapper = new ObjectMapper();
47+
String json = jsonMapper.writeValueAsString(node);
48+
49+
System.out.println(json);
50+
51+
assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":\"9\"}");
52+
} catch(IOException e) {
53+
e.printStackTrace();
54+
}
55+
}
56+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Overview
2+
This is an example of a ETL stream pipeline, mixing a starter application with custom transform and sink.
3+
4+
# Applications
5+
JDBC Source - Application Starter distributed by default
6+
7+
customer-transform - Custom application to transform the data
8+
9+
customer-mongodb-sink - Custom application to sink the data
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.customer</groupId>
8+
<artifactId>customer-mongodb-sink</artifactId>
9+
<packaging>jar</packaging>
10+
11+
<name>customer-mongodb-sink</name>
12+
<description>Example ETL Load Project</description>
13+
14+
<parent>
15+
<artifactId>parent-boot-2</artifactId>
16+
<groupId>com.baeldung</groupId>
17+
<version>0.0.1-SNAPSHOT</version>
18+
<relativePath>../../../parent-boot-2</relativePath>
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.cloud</groupId>
31+
<artifactId>spring-cloud-stream</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.cloud</groupId>
35+
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-data-mongodb</artifactId>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-test</artifactId>
44+
<scope>test</scope>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.springframework.cloud</groupId>
48+
<artifactId>spring-cloud-stream-test-support</artifactId>
49+
<scope>test</scope>
50+
</dependency>
51+
</dependencies>
52+
53+
<dependencyManagement>
54+
<dependencies>
55+
<dependency>
56+
<groupId>org.springframework.cloud</groupId>
57+
<artifactId>spring-cloud-dependencies</artifactId>
58+
<version>${spring-cloud.version}</version>
59+
<type>pom</type>
60+
<scope>import</scope>
61+
</dependency>
62+
</dependencies>
63+
</dependencyManagement>
64+
65+
<build>
66+
<plugins>
67+
<plugin>
68+
<groupId>org.springframework.boot</groupId>
69+
<artifactId>spring-boot-maven-plugin</artifactId>
70+
</plugin>
71+
</plugins>
72+
</build>
73+
74+
75+
</project>

0 commit comments

Comments
 (0)