Split the string by , to get individual map entries. Then split them by = to get the key and the value.

Map<String, String> reconstructedUtilMap = Arrays.stream(utilMapString.split(","))
            .map(s -> s.split("="))
            .collect(Collectors.toMap(s -> s[0], s -> s[1]));

Note: As pointed out by Andreas@ in the comments, this is not a reliable way to convert between a map and a string

EDIT: Thanks to Holger for this suggestion.

Use s.split("=", 2) to ensure that the array is never larger than two elements. This will be useful to not lose the contents (when the value has =)

Example: when the input string is "a=1,b=2,c=3=44=5555" you will get {a=1, b=2, c=3=44=5555}

Earlier (just using s.split("=")) will give {a=1, b=2, c=3}

Answer from Thiyagu on Stack Overflow
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java collections โ€บ java map โ€บ map to string conversion in java
Map to String Conversion in Java | Baeldung
January 8, 2026 - Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.
Discussions

Converting string to Map<Integer,String> in java 8 - Stack Overflow
Can someone please guide me on how to achieve the below using Java 8. I don't know how to get that counter as the key String str = "abcd"; Map map = new HashMap (); ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
February 20, 2019
java - Map<String, String>, how to print both the "key string" and "value string" together - Stack Overflow
I'm new to Java and is trying to learn the concept of Maps. I have came up with the code below. However, I want to print out the "key String" and "value String" at the same time. ProcessBuilder... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to Convert a Map<String, List<String>> to Map<String, String> in java 8 - Stack Overflow
2 Convert List> to Map>> using Java 8 streams API More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - Using lambda to format Map into String - Stack Overflow
I have a map with Integer keys and values. I need to transform it into a String with this specific format: key1 - val1, key2 - val2, key3 - val3. Now, I'm using forEach to format each element, coll... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java collections โ€บ java map โ€บ converting map to map in java
Converting Map to Map in Java | Baeldung
January 8, 2024 - As the test above shows, although result2 is in type Map<String, String>, one value in the map isnโ€™t a string. This could lead to unexpected issues in further processes. So next, letโ€™s see how to achieve a safe conversion. We can build a checkAndTransform() method to perform a safe conversion: Map<String, String> checkAndTransform(Map<String, Object> inputMap) { Map<String, String> result = new HashMap<>(); for (Map.Entry<String, Object> entry : inputMap.entrySet()) { try { result.put(entry.getKey(), (String) entry.getValue()); } catch (ClassCastException e) { throw e; // or a required error handling } } return result; }
๐ŸŒ
Java Code Geeks
examples.javacodegeeks.com โ€บ home โ€บ java development โ€บ core java
Converting String or String Array to Map in Java - Java Code Geeks
October 10, 2023 - This process is particularly useful when you have serialized data of key-value pairs in a string format, and you need to transform it into a more structured data structure like a Map. If we have a String containing key-value pairs separated by a delimiter such as a comma or semicolon, We can use Java Streams to convert it into a Map.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ converting string or string array to map in java
Converting String or String Array to Map in Java | Baeldung
January 8, 2024 - Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.
Find elsewhere
๐ŸŒ
Techie Delight
techiedelight.com โ€บ home โ€บ java โ€บ convert a map to a string in java
Convert a Map to a String in Java | Techie Delight
July 7, 2026 - Just create a joiner, and configure the separator, and specify the collection to be added. ... With Java 8 and above, you can get the string representation of a map using a Stream API, easily customized according to your needs.
๐ŸŒ
KapreSoft
kapresoft.com โ€บ java โ€บ 2023 โ€บ 05 โ€บ 03 โ€บ java-string-to-map.html
Java โ€ข Converting Strings To Map | KapreSoft
May 3, 2023 - In conclusion, working with key-value pairs that are delimited by a specific character can be a common task in Java programming. By using the Map data structure and the Stream API, you can easily and efficiently convert a string of key-value pairs into a Map.
๐ŸŒ
Medium
neesri.medium.com โ€บ what-is-map-in-java-8-f9f334367f58
What is map in java 8. In Java 8, the map method is part ofโ€ฆ | by A cup of JAVA coffee with NeeSri | Medium
January 15, 2024 - In Java 8, the map method is part of the Stream API, and it is used for transforming each element of a stream using a given function. It applies the provided function to each element in the stream and produces a new stream of the transformed ...
Top answer
1 of 3
12

Well you can't safely cast it to a Map<String, String> because even though you know you've only got strings as the values, the compiler doesn't. That's like expecting:

Object x = "foo";
String y = x;

to work - it doesn't; you need to explicitly cast.

Likewise you can explicitly cast here, too, if you go via Object:

Map<String, Object> x = ...;
Map<String, String> y = (Map<String, String>) (Object) x;

Now you'll get a warning saying that it's an unchecked cast, because unlike the earlier "object to string" cast, there's no execution-time check that it's really valid. Type erasure means that a map doesn't really know its key/value types. So you end up with checking only being done when elements are fetched:

import java.util.*;

class Test {
    public static void main(String[] args) {
        Map<String, Object> x = new HashMap<>();
        x.put("foo", "bar");
        x.put("number", 0);
        Map<String, String> y = (Map<String, String>) (Object) x;
        // This is fine
        System.out.println(y.get("foo"));

        // This goes bang! It's trying to cast an Integer to a String
        System.out.println(y.get("number"));
    }
}

So if you really want to avoid creating a new map, this "cast via Object" will work - but it's far from ideal.

Your approach is safer, although you can make it slightly more efficient by avoiding the lookup:

public static Map<String, String> copyToStringValueMap(
        Map<String, Object> input) {
    Map<String, String> ret = new HashMap<>();
    for (Map.Entry<String, Object> entry : input.entrySet()) {
        ret.put(entry.getKey(), (String) entry.getValue());
    }
    return ret;
}
2 of 3
8

A Java 8 solution:

private Map<String, String> stringifyValues(Map<String, Object> variables) {
    return variables.entrySet().stream()
            .collect(Collectors.toMap(Map.Entry::getKey, e -> (String) e.getValue()));
}