Java HashMap Key Set to String ,Array or Vector Convert: Use multiple type (String or Integer) data together as Key Value|| Procedure to retrieve (show) or get individual values from the multiple data type Hash Map key by splitting or breaking || next convert the key into vector.

hashmap key to vector or string array

After a long time I am sharing a post related to Java Programming language. Recently, I have faced a problem while coding   on java HashMap key value set on my job. In my last post I have shown How to show Java HashMap Key value pair and work on it. But the problem I have faced is to convert the Key value set to Vector or String. I used two (2) integer type data, separated with colon, as the key value of my HashMap.  What I need to convert that colon separated key values to individual integer and then convert them or stored them on Vector.

The program given below will show you how to solve such situation.

Code Example:

import  java.util.Vector;

import java.util.HashMap;

import java.util.Set;

class HashMapKeyToVector

{

HashMap studentInfo=new HashMap();

public HashMapKeyToVector()

{

/*** following Roll and age will be used as key in the studentInfo HashMap **/

int student1Roll=10;

int student1age=11;

int student2Roll=12;

int student2age=13;

String  stdInfo1= student1Roll+”:”+student1age;

String  stdInfo2= student2Roll+”:”+student2age;

// Student Name is used to save data, or you can use any other vector

studentInfo.put(stdInfo1,”Adward”);

studentInfo.put(stdInfo2,”Rahim”);

/****************************************************/

System.out.println(“\n //Student Name as Value  and  Roll + Age  as  Key  in the HashMap \n”);

System.out.println(“1st Student Name = “+ studentInfo.get(stdInfo1)+ ” and Roll:Age as key = “+stdInfo1);

System.out.println(“2nd  Student Name = “+ studentInfo.get(stdInfo2)+ ” and  Roll:Age as key = “+stdInfo2);

//student

System.out.println(“\n  “);

Set key=studentInfo.keySet();

String[] strkeys =(String[]) key.toArray(new String[key.size()]);

Vector fullStudentInfo=new Vector();

System.out.println(“\n // Retrieving and Breaking  Keys (Roll+Age)  of  HashMap into individual Roll   and Key”);

System.out.println(“\n “);

for (int i=0;i<key.size();i++){

String[] result = strkeys[i].toString().split(“:”);

System.out.println(“Student  “+(i+1)+ ”  Roll is :”+result[0]+  ” and Age is :”+result[1] + ”  and Name is  : ” +studentInfo.get(strkeys[i].toString()));

//System.out.println(“\n “);

Vector  individualStudentInfo = new Vector();

individualStudentInfo.add(result[0]);

individualStudentInfo.add(result[1]);

individualStudentInfo.add(studentInfo.get(strkeys[i].toString()));

fullStudentInfo.add(individualStudentInfo);

}

System.out.println(“\n // Converting  HashMap Key  value  to Vector  “);

System.out.println(“\nAfter Entering into Vector  All student Info :”+fullStudentInfo);

}

public static void main(String args[])

{

HashMapKeyToVector x= new HashMapKeyToVector();

}

}

About these ads

One Response to Java HashMap Key Set to String ,Array or Vector Convert: Use multiple type (String or Integer) data together as Key Value|| Procedure to retrieve (show) or get individual values from the multiple data type Hash Map key by splitting or breaking || next convert the key into vector.

  1. javacoder says:

    Wonderful…………. it’s help me a lot !!!!!!!!
    Keep posting about Java Basics.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 309 other followers