Monday, September 13, 2021

Convert String or String Array to HashMap In Java

Oracle Java Exam Prep, Oracle Java Preparation, Oracle Java Guides, Oracle Java Learning, Core Java, Oracle Java Career

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:

◉ The input string is converted to an array of strings as output.

◉ Input as an array of strings is converted to HashMap.

Input string elements as follows:

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

Approach : 

Phase 1: The input string is converted to an array of strings as output.

Phase 2: Input as an array of strings is converted to HashMap.

Phase 1:

◉ First, we split the String by a comma and store it in an array parts. After the split we have the following content:

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

◉ And then iterate on parts and split the student data to get the student name and roll number. And set roll no as key and the name as the value of the HashMap.

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 : 

◉ Iterate over the array and set the roll number as key and the name as values in the HashMap.

Oracle Java Exam Prep, Oracle Java Preparation, Oracle Java Guides, Oracle Java Learning, Core Java, Oracle Java Career
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 OJC {

// 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 OJC {

// 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}

Source: geeksforgeeks.org

Related Posts

0 comments:

Post a Comment