This is one solution. If you want to make it more generic, you can use the StringUtils library.

String value = "{first_name = naresh,last_name = kumar,gender = male}";
value = value.substring(1, value.length()-1);           //remove curly brackets
String[] keyValuePairs = value.split(",");              //split the string to creat key-value pairs
Map<String,String> map = new HashMap<>();               

for(String pair : keyValuePairs)                        //iterate over the pairs
{
    String[] entry = pair.split("=");                   //split the pairs to get key and value 
    map.put(entry[0].trim(), entry[1].trim());          //add them to the hashmap and trim whitespaces
}

For example you can switch

 value = value.substring(1, value.length()-1);

to

 value = StringUtils.substringBetween(value, "{", "}");

if you are using StringUtils which is contained in apache.commons.lang package.

Answer from kai on Stack Overflow
๐ŸŒ
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 - We can also use the String.split() method to convert a string to a map in Java. This involves splitting the input string into tokens based on a delimiter, and then parsing these tokens into key-value pairs. Here is an example:
๐ŸŒ
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 - In this method, we iterate through the String array and split each element into key and value. Next, the resulting key-value pairs are added to the Map. The following test method is used to validate the convertStringArrayToMap() method:
Discussions

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 - Convert string representing key-value pairs to Map - Stack Overflow
@Gevorg Sorry for the confusion, ... use Java 8, but can use Guava or Kotlin. I see I didn't make this clear in my comment. 2017-07-24T13:50:22.077Z+00:00 ... You don't need a library to do that. You just need to use StringTokenizer or String.split and iterate over the tokens to fill the map. The import of the library plus its settings would be almost as big as the three lines of code needed to do it yourself. For example : public static Map convert(String str) ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
lambda - Java 8: How to convert String to Map<String,String>? - Stack Overflow
Note: As pointed out by Andreas@ ... 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 ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
October 8, 2018
How can I convert a hashmap to a string and vice versa
For the hashMap to String you'll almost there, all you need to do is to format a bit more each map entry to look exactly like in the example string, and then specify the proper prefix & suffix in the joining afterwards. For String to hashMap it's a bit tricky, you need to decompose the string in key-value pairs so you can then collect them in a map if you want to do it in one go (feasible, especially since you can chain string methods). Thus you'll have to distinguish between two types of commas : the ones between each key and value and the ones between each key-value pair (at least it's the way I would approach it). More on reddit.com
๐ŸŒ r/javahelp
8
0
April 9, 2022
๐ŸŒ
KapreSoft
kapresoft.com โ€บ java โ€บ 2023 โ€บ 05 โ€บ 03 โ€บ java-string-to-map.html
Java โ€ข Converting Strings To Map | KapreSoft
May 3, 2023 - Example can be further improved by using the Stream API to write a functional version of the code that converts a string with key-value pairs delimited by semi-colons into a Map in Java.
๐ŸŒ
Know Program
knowprogram.com โ€บ home โ€บ convert string to map java
Convert String to Map Java - Know Program
June 19, 2022 - String: {โ€œappleโ€:โ€1โ€ณ,โ€bananaโ€:โ€2โ€ณ,โ€pomegranateโ€:โ€4โ€ณ,โ€Mangoโ€:โ€6โ€ณ} Map: {banana=2, apple=1, pomegranate=4, Mango=6} If you enjoyed this post, share it with your friends.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ convert-string-to-map-in-java
Convert String to Map in Java - Javatpoint
Java Program to swap two string variables without using third or temp variable.
๐ŸŒ
Java Guides
javaguides.net โ€บ 2023 โ€บ 10 โ€บ convert-json-string-to-map-in-java.html
Convert JSON string to Map in Java
October 26, 2023 - import com.fasterxml.jackson.d... = new ObjectMapper(); try { // Convert JSON string to Map Map<String, Object> map = mapper.readValue(jsonStr, Map.class); // Display the map for (Map.Entry<String, Object> entry : ...
Find elsewhere
๐ŸŒ
javathinking
javathinking.com โ€บ blog โ€บ convert-string-to-map-in-java-using-objectmapper
Convert String to Map in Java Using ObjectMapper โ€” javathinking.com
You may need to transform a JSON string into a Map for further processing, such as aggregating data, performing calculations, or converting it to another format. Here is a simple Java code example that demonstrates how to convert a JSON string to a Map using ObjectMapper:
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ convert-json-string-to-java-map-with-jackson
Convert JSON String to Java Map with Jackson
March 16, 2023 - Map<String, TASK_STATUS> result = objectMapper.readValue(json, new TypeReference<LinkedHashMap>(){}); ... Both of these construct an object by calling the exact same deserialization process. So the only difference between these two calls is whether you're making a static (JavaType) or dynamic ...
๐ŸŒ
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 - 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()) ...
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ convert json string to map using jackson
Convert JSON string to Map using Jackson | mkyong.com
April 23, 2024 - This example parses JSON string to a Map using Jackson. String json = "..."; ObjectMapper mapper = new ObjectMapper(); // convert JSON string to Map Map<String, Object> map = mapper.readValue(json, new TypeReference<>() {}); JsonToMapExample.java ยท
๐ŸŒ
Linux Hint
linuxhint.com โ€บ how-convert-map-string-java
Linux Hint โ€“ Linux Hint
April 24, 2023 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
๐ŸŒ
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 : delimiter, and then stored in a HashMap. The output displays the resulting key-value map.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ convert-string-or-string-array-to-hashmap-in-java
Convert String or String Array to HashMap In Java - GeeksforGeeks
July 23, 2025 - First, we split the String by a comma and store it in an array parts. After the split we have the following content: ... And then iterate on parts and split the student data to get the student name and roll number.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 570738 โ€บ java โ€บ Convert-String-Map
Convert String to Map (Java in General forum at Coderanch)
March 18, 2012 - Here i have to split this whole String "a" into broken tokens like a=100,b=200,c=300 so have used split method and now have to seperate the key and values like key:a and value:100.should not use StringTokenizer but can use any other collection like HashMap,HashTable.etc.I dont know how to add ...