This example shows how to convert String to HashMap object. The example also shows how to convert String array to HashMap using for loop and the split method.
How to convert String to HashMap object in Java?
Suppose you have a String object containing the following content.
1 |
11:Jack, 23:Emily, 31:Ryan, 56:John, 99:Maria |
The string contains multiple employees separated by a comma and each employee contains id and name separated by a colon. 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import java.util.HashMap; import java.util.Map; public class StringToHashmapExample { public static void main(String[] args) { String strSource = "11:Jack, 23:Emily, 31:Ryan, 56:John, 99:Maria"; //new HashMap object Map<String, String> hMapData = new HashMap<String, String>(); //split the String by a comma String parts[] = strSource.split(","); //iterate the parts and add them to a map for(String part : parts){ //split the employee data by : to get id and name String empdata[] = part.split(":"); String strId = empdata[0].trim(); String strName = empdata[1].trim(); //add to map hMapData.put(strId, strName); } System.out.println("String to HashMap: " + hMapData); } } |
Output
1 |
String to HashMap: {11=Jack, 99=Maria, 23=Emily, 56=John, 31=Ryan} |
Using Java 8
The below given code does the same but using Java 8 stream.
1 2 3 4 5 6 7 |
String strSource = "11:Jack, 23:Emily, 31:Ryan, 56:John, 99:Maria"; Map<String, String> hMapData = Arrays.stream( strSource.split(",") ) .map(s -> s.split(":")) .collect(Collectors.toMap(s -> s[0].trim(), s -> s[1].trim())); System.out.println("String to HashMap: " + hMapData); |
Output
1 |
String to HashMap: {99=Maria, 11=Jack, 56=John, 23=Emily, 31=Ryan} |
How to convert String Array to HashMap object in Java?
If you have an array containing the data that you want to convert to the HashMap, you can use the below given code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
String[] arrayData = { "11:Jack", "23:Emily", "31:Ryan", "56:John", "99:Maria"}; Map<String, String> hMapData = new HashMap<String, String>(); //iterate through an array for(String str : arrayData){ //split the data by : String[] parts = str.split(":"); hMapData.put( parts[0].trim(), parts[1].trim() ); } System.out.println("String Array to HashMap: " + hMapData); |
Output
1 |
String Array to HashMap: {11=Jack, 99=Maria, 23=Emily, 56=John, 31=Ryan} |
How to convert two arrays containing keys and values to HashMap?
If you have two arrays, one array containing keys and other containing values, you can convert them to a map object as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
//keys array Integer[] arrayKeys = {1, 2, 3, 4, 5}; //values array String[] arrayValues = {"Jack", "Emily", "Ryan", "John", "Maria"}; Map<Integer, String> hMapData = new HashMap<Integer, String>(); /* * Assuming both the arrays are of the same size */ for( int i = 0; i < arrayKeys.length; i++ ){ hMapData.put( arrayKeys[i], arrayValues[i] ); } System.out.println("HashMap contains: " + hMapData); |
Output
1 |
HashMap contains: {1=Jack, 2=Emily, 3=Ryan, 4=John, 5=Maria} |
Both the arrays should of the same size for the above example to work.
This example is a part of the HashMap in Java tutorial.
Please let me know your views in the comments section below.