How to convert String into Hashmap in java

Naresh kumar :

How can I convert a String into a HashMap?

String value = "{first_name = naresh, last_name = kumar, gender = male}"

into

Map<Object, Object> = {
    first_name = naresh,
    last_name = kumar,
    gender = male
}

Where the keys are first_name, last_name and gender and the values are naresh, kumar, male.

Note: Keys can be any thing like city = hyderabad.

I am looking for a generic approach.

kai :

This is one solution. If you want to make it more generic, you can us 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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Java: how to convert HashMap<String, Object> to array

From Dev

Convert String to Hashmap in Java

From Java

How to convert a HashMap to a K/V-String in Java 8 with Streams

From Dev

How to convert Json String to HashMap?

From Dev

How to convert List<NameValuePair> into a hashMap<String, String>?

From Dev

How to Convert ArrayList<HashMap<String, String>> to a StringArray?

From Java

How to convert Values in a Hashmap to a List<String>

From Java

How to convert untyped Object to HashMap<String,Object>

From Dev

How to convert a List<String[]> of Subtitle Structure to HashMap<String, List<String[]>>

From Java

How to convert hashmap to JSON object in Java

From Dev

How to convert a stream to a HashMap (not Map) in Java 8

From Dev

How to convert Array to HashMap in Java with Stream API

From Java

How does one convert a HashMap to a List in Java?

From Dev

How to convert following CSV to Hashmap using JAVA

From Dev

How to convert a Java Hashmap to a javascript array properly

From Dev

Convert String to HashMap<String,String[]>

From Dev

Convert String to Hashmap in RUST

From Java

Convert a JSON String to a HashMap

From Dev

Convert hashmap to string

From Dev

Convert String to HashMap

From Java

How to achieve a multi valued hashmap like HashMap<String,Hashmap<String,String>> in Java

From Java

Convert JSONArray to List<HashMap<String,Object>> in java without GSON

From Java

Failed to convert value of type java.util.HashMap to String

From Dev

Java - How to get an Integer associated with a String in a HashMap

From Dev

How to convert key values of Hashmap from String to int

From Dev

How to convert a Character Key into a String output from a hashmap

From Dev

Failed to convert value of type java.util.HashMap to String but no hashmap in code?

From Dev

How to convert jsonobject to hashmap

From Dev

How to convert JavaPairRDD into HashMap

Related Related

  1. 1

    Java: how to convert HashMap<String, Object> to array

  2. 2

    Convert String to Hashmap in Java

  3. 3

    How to convert a HashMap to a K/V-String in Java 8 with Streams

  4. 4

    How to convert Json String to HashMap?

  5. 5

    How to convert List<NameValuePair> into a hashMap<String, String>?

  6. 6

    How to Convert ArrayList<HashMap<String, String>> to a StringArray?

  7. 7

    How to convert Values in a Hashmap to a List<String>

  8. 8

    How to convert untyped Object to HashMap<String,Object>

  9. 9

    How to convert a List<String[]> of Subtitle Structure to HashMap<String, List<String[]>>

  10. 10

    How to convert hashmap to JSON object in Java

  11. 11

    How to convert a stream to a HashMap (not Map) in Java 8

  12. 12

    How to convert Array to HashMap in Java with Stream API

  13. 13

    How does one convert a HashMap to a List in Java?

  14. 14

    How to convert following CSV to Hashmap using JAVA

  15. 15

    How to convert a Java Hashmap to a javascript array properly

  16. 16

    Convert String to HashMap<String,String[]>

  17. 17

    Convert String to Hashmap in RUST

  18. 18

    Convert a JSON String to a HashMap

  19. 19

    Convert hashmap to string

  20. 20

    Convert String to HashMap

  21. 21

    How to achieve a multi valued hashmap like HashMap<String,Hashmap<String,String>> in Java

  22. 22

    Convert JSONArray to List<HashMap<String,Object>> in java without GSON

  23. 23

    Failed to convert value of type java.util.HashMap to String

  24. 24

    Java - How to get an Integer associated with a String in a HashMap

  25. 25

    How to convert key values of Hashmap from String to int

  26. 26

    How to convert a Character Key into a String output from a hashmap

  27. 27

    Failed to convert value of type java.util.HashMap to String but no hashmap in code?

  28. 28

    How to convert jsonobject to hashmap

  29. 29

    How to convert JavaPairRDD into HashMap

HotTag

Archive