BAEL-2480 Java Map to String conversion (#6075)

* BAEL-2480 Java Map to String conversion

* BAEL-2480 Java Map to String conversion

* BAEL-2480 Java Map to String conversion

* BAEL-2480 Java Map to String conversion
This commit is contained in:
Laurentiu Delcea 2019-01-10 23:21:06 +02:00 committed by maibin
parent 9e71023f98
commit b8dbc9d053
4 changed files with 126 additions and 0 deletions

View File

@ -0,0 +1,34 @@
package com.baeldung.convert;
import com.google.common.base.Joiner;
import org.apache.commons.lang3.StringUtils;
import java.util.Map;
import java.util.stream.Collectors;
public class MapToString {
public static String convertWithIteration(Map<Integer, ?> map) {
StringBuilder mapAsString = new StringBuilder("{");
for (Integer key : map.keySet()) {
mapAsString.append(key + "=" + map.get(key) + ", ");
}
mapAsString.delete(mapAsString.length()-2, mapAsString.length()).append("}");
return mapAsString.toString();
}
public static String convertWithStream(Map<Integer, ?> map) {
String mapAsString = map.keySet().stream()
.map(key -> key + "=" + map.get(key))
.collect(Collectors.joining(", ", "{", "}"));
return mapAsString;
}
public static String convertWithGuava(Map<Integer, ?> map) {
return Joiner.on(",").withKeyValueSeparator("=").join(map);
}
public static String convertWithApache(Map map) {
return StringUtils.join(map);
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.convert;
import com.google.common.base.Splitter;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
public class StringToMap {
public static Map<String, String> convertWithStream(String mapAsString) {
Map<String, String> map = Arrays.stream(mapAsString.split(","))
.map(entry -> entry.split("="))
.collect(Collectors.toMap(entry -> entry[0], entry -> entry[1]));
return map;
}
public static Map<String, String> convertWithGuava(String mapAsString) {
return Splitter.on(',').withKeyValueSeparator('=').split(mapAsString);
}
}

View File

@ -0,0 +1,48 @@
package com.baeldung.convert;
import org.apache.commons.collections4.MapUtils;
import org.junit.Assert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.Map;
public class MapToStringUnitTest {
private Map<Integer, String> wordsByKey = new HashMap<>();
@BeforeEach
public void setup() {
wordsByKey.clear();
wordsByKey.put(1, "one");
wordsByKey.put(2, "two");
wordsByKey.put(3, "three");
wordsByKey.put(4, "four");
}
@Test
public void givenMap_WhenUsingIteration_ThenResultingMapIsCorrect() {
String mapAsString = MapToString.convertWithIteration(wordsByKey);
Assert.assertEquals("{1=one, 2=two, 3=three, 4=four}", mapAsString);
}
@Test
public void givenMap_WhenUsingStream_ThenResultingMapIsCorrect() {
String mapAsString = MapToString.convertWithStream(wordsByKey);
Assert.assertEquals("{1=one, 2=two, 3=three, 4=four}", mapAsString);
}
@Test
public void givenMap_WhenUsingGuava_ThenResultingMapIsCorrect() {
String mapAsString = MapToString.convertWithGuava(wordsByKey);
Assert.assertEquals("1=one,2=two,3=three,4=four", mapAsString);
}
@Test
public void givenMap_WhenUsingApache_ThenResultingMapIsCorrect() {
String mapAsString = MapToString.convertWithApache(wordsByKey);
Assert.assertEquals("{1=one, 2=two, 3=three, 4=four}", mapAsString);
MapUtils.debugPrint(System.out, "Map as String", wordsByKey);
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.convert;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import java.util.Map;
public class StringToMapUnitTest {
@Test
public void givenString_WhenUsingStream_ThenResultingStringIsCorrect() {
Map<String, String> wordsByKey = StringToMap.convertWithStream("1=one,2=two,3=three,4=four");
Assert.assertEquals(4, wordsByKey.size());
Assert.assertEquals("one", wordsByKey.get("1"));
}
@Test
void givenString_WhenUsingGuava_ThenResultingStringIsCorrect() {
Map<String, String> wordsByKey = StringToMap.convertWithGuava("1=one,2=two,3=three,4=four");
Assert.assertEquals(4, wordsByKey.size());
Assert.assertEquals("one", wordsByKey.get("1"));
}
}