Open In App

Convert String or String Array to HashMap In Java

Considering a string object that contains the student name and roll number separated by a comma, and each student contains the name and roll number separated by a colon. Now need is to convert the String to a Map object so that each student roll number becomes the key of the HashMap, and the name becomes the value of the HashMap object. 

In order to convert strings to HashMap, the process is divided into two parts:



 Input string elements as follows:

Input : "Aashish:1, Bina:2, Chintu:3"

Approach : 



Phase 1:

"Aashish:1","Bina:2", "Chintu:3"

Phase 2:

We can also convert an array of String to a HashMap. Suppose we have a string array of the student name and an array of roll numbers, and we want to convert it to HashMap so the roll number becomes the key of the HashMap and the name becomes the value of the HashMap.

Note: Both the arrays should be the same size.

Input array of string elements as follows which will be output in phase 1

String stuName[] = {"Aashish","Bina","Chintu"}
Integer stuRollNo[] = {1,2,3}

Approach : 

Phase 1 – String to Array of string




// Convert String or String array to HashMap In Java
  
// Phase 1: Input- String converted to ArrayofStrings
  
// Importing java generic libraries
import java.util.*;
  
class GFG {
    
  // Main driver method
    public static void main(String[] args)
    {
        // String object that contains the student name and
        // roll number separated by a comma
        String student = "Aashish:1, Bina:2, Chintu:3";
  
        // New HashMap obj
        Map<String, String> hashMap
            = new HashMap<String, String>();
  
        // split the String by a comma
        String parts[] = student.split(",");
  
        // iterate the parts and add them to a HashMap
        for (String part : parts) {
  
            // split the student data by colon to get the
            // name and roll number
            String stuData[] = part.split(":");
  
            String stuRollNo = stuData[0].trim();
            String stuName = stuData[1].trim();
  
            // Add to map
            hashMap.put(stuRollNo, stuName);
        }
  
        // Print hashMap
        System.out.println("String to HashMap: " + hashMap);
    }
}

Output
String to HashMap: {Chintu=3, Bina=2, Aashish=1}

Phase 2 – String to Array to HashMap




// Convert String or String array to HashMap In Java
  
// Phase 2: Input- Array of strings converted to HashMap.
  
// Importing java generic libraries
import java.util.*;
  
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // String array that contains the student name
        String stuName[] = { "Aashish", "Bina", "Chintu" };
  
        // Integer array that contains roll number of the
        // students
        Integer stuRollNo[] = { 1, 2, 3 };
  
        // New HashMap obj
        Map<Integer, String> hashMap
            = new HashMap<Integer, String>();
  
        // Iterating over array of strings
        for (int i = 0; i < stuName.length; i++) {
  
            // And set roll no as key and the name as value
            hashMap.put(stuRollNo[i], stuName[i]);
        }
  
        // Printing HashMap
        System.out.println("String to hashmap: " + hashMap);
    }
}

Output
String to hashmap: {1=Aashish, 2=Bina, 3=Chintu}

Article Tags :