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
🌐
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 - Finally, the assertEquals() method is used to compare the expectedMap with the resultMap to ensure that the conversion is performed correctly and that the output Map with Lists of values matches the expected one. In this article, we have explored different techniques for converting Strings and String arrays into Maps in Java.
🌐
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.
🌐
Mkyong
mkyong.com › home › java › convert json string to map using jackson
Convert JSON string to Map using Jackson | mkyong.com
April 23, 2024 - package com.mkyong.json.jackson; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.Map; public class JsonToMapExample { public static void main(String[] args) throws JsonProcessingException { // JSON string String json = "{\"name\": \"mkyong\", \"age\": 20}"; // Create an ObjectMapper instance. ObjectMapper mapper = new ObjectMapper(); // convert JSON string to Map // uncheck assignment // Map<String, Object> map = mapper.readValue(json, Map.class); // Convert JSON string to Map Map<String, Object> map = mapper.readValue(json, new TypeReference<>() {}); // Output the contents of the Map to verify the conversion System.out.println("Map content: " + map); } }
🌐
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.
🌐
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.
🌐
KapreSoft
kapresoft.com › java › 2023 › 05 › 03 › java-string-to-map.html
Java • Converting Strings To Map | KapreSoft
May 3, 2023 - Therefore, it’s important to benchmark and test different implementations to determine the most efficient solution for a given problem. 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.
🌐
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.
Find elsewhere
🌐
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.databind.ObjectMapper; import java.util.Map; public class MainApp { public static void main(String[] args) { // Sample JSON string String jsonStr = "{\"id\":1,\"name\":\"Jane Smith\",\"email\":\"jane.smith@example.com\"}"; // Create an ObjectMapper instance ObjectMapper mapper = 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 : map.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } } catch (Exception e) { e.printStackTrace(); } } }
🌐
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 (TypeReference) reference to the type.
🌐
Educative
educative.io › answers › how-to-convert-a-json-string-to-a-map-using-jackson
How to convert a JSON string to a map using Jackson
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.13.0-rc1</version> </dependency> Create an object of the ObjectMapper class.
🌐
javathinking
javathinking.com › blog › convert-string-to-map-in-java-using-objectmapper
Convert String to Map in Java Using ObjectMapper — javathinking.com
In Java, there are numerous scenarios where you might need to convert a JSON string into a `Map`. One of the most efficient and widely used ways to achieve this is by leveraging the `ObjectMapper` class from the Jackson library.
🌐
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 ...
🌐
Stack Overflow
stackoverflow.com › questions › 43627222 › converting-string-to-to-map-in-java
json - converting string to to Map in Java - Stack Overflow
April 26, 2017 - 6 Error converting JSON string to map in Java using Jackson · 2 Mapping Json string to map or hashmap field in java · 3 Java Jackson JSON parse into Map<String, String> 0 Convert Java String to HashMap Object · 12 How to convert a JSON string to a Map<String, Set<String>> with Jackson JSON ·
🌐
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. ... Using StringTokenizer provides a simple way to split strings into key-value pairs, making it useful for parsing data from various formats in Java.