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
🌐
Laurie Excel
laurieexcell.com › home › how to convert string to map in java 8?
How to convert string to map in Java 8? - Laurie Excel
January 27, 2025 - Converting strings to maps in Java 8 is a simple and efficient process. By using the HashMap, ConcurrentHashMap, or LinkedHashMap classes, you can easily convert strings to maps and store key-value pairs.
Discussions

Convert String array to Map using Java 8 Lambda expressions - Stack Overflow
Is there a better functional way of converting an array of Strings in the form of "key:value" to a Map using the Java 8 lambda syntax? More on stackoverflow.com
🌐 stackoverflow.com
Collect list of strings to map with Java 8 Stream API - Stack Overflow
Could someone suggest how could I transform list like ["bla", "blabla", "blablabla"] to map like {"bla" : 3, "blabla" : 6, "blablabla" : 9} with words stands for keys and values stands for words le... More on stackoverflow.com
🌐 stackoverflow.com
collections - How to convert String into Hashmap in java - Stack Overflow
How can I convert a String into a HashMap? String value = "{first_name = naresh, last_name = kumar, gender = male}" into Map = { first_name = naresh, last_name = kum... More on stackoverflow.com
🌐 stackoverflow.com
java - List of String to Map Java8 - Stack Overflow
I have a list of String in my application. Every String is actually an output of 2 different strings concatenated by ".". e.g. Stack.Overflow Now, I am trying to convert this list to a Map with key More on stackoverflow.com
🌐 stackoverflow.com
🌐
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
🌐
Know Program
knowprogram.com › home › convert string to map java
Convert String to Map Java - Know Program
June 19, 2022 - Also, the map only stores unique key values, therefore, no duplicate values are allowed on the map. Now this blog, teaches you how to convert the string to a map. The string has a particular type of elements but in the map, we need to store the elements in the form of key and value pairs.
🌐
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.
🌐
Tpoint Tech
tpointtech.com › convert-string-to-map-in-java
Convert String to Map in Java - Tpoint Tech
September 10, 2024 - Handling key-value-based data is a common requirement in various Java applications.
🌐
Javatpoint
javatpoint.com › convert-string-to-map-in-java
Convert String to Map in Java - Javatpoint
Convert String to Map in Java with java tutorial, features, history, variables, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
YouTube
youtube.com › watch
Convert List into map using java 8 Streams | key should be String value should be length of String - YouTube
📌Java 8 Streams: Convert List into map using java 8 Streams 📌Please do subscribe my channel: https://www.youtube.com/c/javashastra💛 Join my telegram chann...
Published: June 6, 2023
🌐
Java Guides
javaguides.net › 2023 › 10 › convert-json-string-to-map-in-java.html
Convert JSON string to Map in Java
October 26, 2023 - Note: The generic Map.class was used, implying the keys are Strings, but values are treated as Objects. This allows for flexibility in handling different data types within the JSON string. Below is the code to convert a complex JSON string to a Java Map:
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Split String To Map In Java - Java Code Geeks
April 3, 2024 - In this example, the input string name:John age:30 city:New York is split into key-value pairs using the regular expression pattern (\\w+):(\w+), where \\w+ matches any word character and : is the delimiter.
🌐
Reddit
reddit.com › r/learnjava › java 8 streams - string to hashmap counter of characters
r/learnjava on Reddit: Java 8 streams - String to HashMap counter of characters
August 22, 2018 -

Given a String, I want to create a hashmap that maps a Character to the number of times it shows up in the string.

I saw in Stack Overflow the following line:

Map<String, Long> map =
    Arrays.stream(s1.split("")).
            collect(Collectors.groupingBy(c -> c, Collectors.counting()));

And it works great, but it maps a String to the counter.

I tried writing it for Characters, and as far as I understood, there isn't a char stream, and I'll have to use IntStream. So I converted it to an IntStream, and used the same Collectors function.

Map<Integer, Long> map =
    s1.chars().
            collect(Collectors.groupingBy(c -> c, Collectors.counting()));

The above doesn't work. The error is:

Error:(14, 27) java: method collect in interface java.util.stream.IntStream cannot be applied to given types;

required: java.util.function.Supplier<R>,java.util.function.ObjIntConsumer<R>,java.util.function.BiConsumer<R,R>

found: java.util.stream.Collector<java.lang.Object,capture#1 of ?,java.util.Map<java.lang.Object,java.lang.Long>>

reason: cannot infer type-variable(s) R

(actual and formal argument lists differ in length)

What is the difference between the two?