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.
🌐
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.
🌐
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.
Find elsewhere
🌐
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 ...
🌐
Coderanch
coderanch.com › t › 685203 › java › Create-Map-Character-List-String
Create Map of Character and List of String using Set of String (Features new in Java 8 forum at Coderanch)
["hi","hills","and","at","not","bad","that","the"] The output like, map of character and list of string: ['h':["hi","hills"], 'a':["and","at"], 'n':["not"], 'b':["bad"], 't':["that","the"]] I am able to do it in using earlier java version but stuck with creattion on this on Java8 using stream. Thanks, Atul ... Can you post what you have tried to solve the problem? Hint: One method that will be useful to solve this is Collectors.groupingBy(...) ... Well, OP can practise the use of two other java 8 thingies: create a map using Collectors.toMap and using the Map method 'computeIfAbsent' (see the Api)
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()));
}