This commit related to the article BAEL-6877 (#14899)
This commit aims to add a new Java module "core-java-collections-maps-7"
This commit is contained in:
parent
ed2f3234e6
commit
e62aba145b
|
@ -0,0 +1 @@
|
|||
## Relevant Articles
|
|
@ -0,0 +1,78 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-collections-maps-7</artifactId>
|
||||
<name>core-java-collections-maps-7</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<spring.version>5.2.5.RELEASE</spring.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.12.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>1.36</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.8.9</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.json</groupId>
|
||||
<artifactId>json</artifactId>
|
||||
<version>20230227</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.13.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>5.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>5.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.13.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>5.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.13.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,60 @@
|
|||
package com.baeldung.map;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ConvertHashMapStringToHashMapObjectUsingtoString {
|
||||
public String name;
|
||||
public int age;
|
||||
|
||||
public ConvertHashMapStringToHashMapObjectUsingtoString(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public static ConvertHashMapStringToHashMapObjectUsingtoString deserializeCustomObject(String valueString) {
|
||||
if (valueString.startsWith("{") && valueString.endsWith("}")) {
|
||||
valueString = valueString.substring(1, valueString.length() - 1);
|
||||
String[] parts = valueString.split(",");
|
||||
String name = null;
|
||||
int age = -1;
|
||||
for (String part : parts) {
|
||||
String[] keyValue = part.split("=");
|
||||
if (keyValue.length == 2) {
|
||||
String key = keyValue[0].trim();
|
||||
String val = keyValue[1].trim();
|
||||
if (key.equals("name")) {
|
||||
name = val;
|
||||
} else if (key.equals("age")) {
|
||||
age = Integer.parseInt(val);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (name != null && age >= 0) {
|
||||
return new ConvertHashMapStringToHashMapObjectUsingtoString(name, age);
|
||||
}
|
||||
}
|
||||
return new ConvertHashMapStringToHashMapObjectUsingtoString("", -1);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String hashMapString = "{key1={name=John, age=30}, key2={name=Alice, age=25}}";
|
||||
String keyValuePairs = hashMapString.replaceAll("[{}\\s]", "");
|
||||
String[] pairs = keyValuePairs.split(",");
|
||||
Map<String, ConvertHashMapStringToHashMapObjectUsingtoString> actualHashMap = new HashMap<>();
|
||||
for (String pair : pairs) {
|
||||
String[] keyValue = pair.split("=");
|
||||
if (keyValue.length == 2) {
|
||||
String key = keyValue[0];
|
||||
ConvertHashMapStringToHashMapObjectUsingtoString value = deserializeCustomObject(keyValue[1]);
|
||||
actualHashMap.put(key, value);
|
||||
}
|
||||
}
|
||||
System.out.println(actualHashMap);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{name=" + name + ", age=" + age + "}";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class ConvertHashMapStringToHashMapObjectUsingtoStringUnitTest {
|
||||
|
||||
@Test
|
||||
void givenValidCustomObject_whenSerializing_thenSerializedStringIsCorrect() {
|
||||
ConvertHashMapStringToHashMapObjectUsingtoString customObject = new ConvertHashMapStringToHashMapObjectUsingtoString("John", 30);
|
||||
String expectedSerializedString = "{name=John, age=30}";
|
||||
assertEquals(expectedSerializedString, customObject.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenValidSerializedString_whenDeserializing_thenCustomObjectIsCorrect() {
|
||||
String serializedString = "{name=Alice, age=25}";
|
||||
ConvertHashMapStringToHashMapObjectUsingtoString customObject = ConvertHashMapStringToHashMapObjectUsingtoString.deserializeCustomObject(serializedString);
|
||||
assertEquals("Alice", customObject.name);
|
||||
assertEquals(25, customObject.age);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenInvalidSerializedString_whenDeserializing_thenDefaultCustomObjectIsCreated() {
|
||||
String invalidSerializedString = "{invalidString}";
|
||||
ConvertHashMapStringToHashMapObjectUsingtoString customObject = ConvertHashMapStringToHashMapObjectUsingtoString.deserializeCustomObject(invalidSerializedString);
|
||||
assertEquals("", customObject.name);
|
||||
assertEquals(-1, customObject.age);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue