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.
In order to convert strings to HashMap, the process is divided into two parts: The input string is converted to an array of strings as output. Input as an array of strings is converted to HashMap.
String is as a key of the HashMap When you create a HashMap object and try to store a key-value pair in it, while storing, a hash code of the given key is calculated and its value is placed at the position represented by the resultant hash code of the key.
So, to answer the question, no, it would not be a bad idea to use a String for a key to a HashMap . Of course equals is also used, but as soon as you have multiple objects within the same bucket, your O(1) is more like O(n)...
You want to convert the string to a map object so that each employee id becomes the key of the HashMap and name becomes the value of the HashMap object. The below example shows how to do the conversion using the string split method. String to HashMap: {11=Jack, 99=Maria, 23=Emily, 56=John, 31=Ryan}
The method used is split, it splits up the string and then stores into hash map in correct format. // Defines a Hashmap which will be used in this sample. Map<String, String> map = new HashMap<String, String>(); /**.
This is very handy , in the conditions when you have to store a value which has a distinct key and can be identified by the unique key.In hashmap using a key and a value, you can store the value in Map object,we can retrieve it later by using it’s key.
Convert a String to a Map Using Streams To perform conversion from a String to a Map, let's define where to split on and how to extract keys and values: public Map<String, String> convertWithStream(String mapAsString) { Map<String, String> map = Arrays.stream (mapAsString.split (","))
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With