Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert String into Hashmap in java

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.

like image 937
Naresh kumar Avatar asked Oct 21 '14 11:10

Naresh kumar


People also ask

Can we convert String to HashMap in java?

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.

Can we store String in 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.

Can we use String as key in HashMap?

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)...

How to convert a string to a hashmap object?

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}

How to split a string into a hashmap in Java?

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>(); /**.

What is the use of HashMap in Java?

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.

How do I convert a string to a map in Java?

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 (","))


1 Answers

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.

like image 75
kai Avatar answered Sep 19 '22 15:09

kai