Write a java program using HashMap (generate the state ID)

Write a program to generate the state ID.

1)Read n Strings as input(as State Name).
2)Create a String Array to Store the above Input.
3)Write a function  getStateId which accepts String Array as input.
4)Create a HashMap<String,String> which stores state name as key and state Id as Value.
5)The function getStateId returns the HashMap to the Main Class.

Input and Output Format:
Input Consists of an integer n denotes the size of the string array.
Output consists of an HashMap displayed in the string array order.

Sample Input 1:
3
Kerala
Gujarat
Goa

Sample Output 1:
KER:Kerala
GUJ:Gujarat
GOA:Goa

Write a java program using HashMap

import java.util.*;
public class Main
{
public static void main(String[] args)
{
String[] s1={"goa","kerala","gujarat"};
putvalues(s1);
}
public static void putvalues(String[] s1)
{
HashMap<String, String> hm = new HashMap<String, String>();
ArrayList<String> lst1 = new ArrayList<String>();
ArrayList<String> lst2 = new ArrayList<String>();
for(String s : s1)
lst1.add(s.toUpperCase().substring(0,3));
for(String s : s1)
lst2.add(s);
for(int i=0;i<s1.length;i++)
{
hm.put(lst1.get(i),lst2.get(i));
}
//System.out.println(map);
for(Map.Entry<String, String> ans: hm.entrySet())
{
System.out.println(ans.getKey()+":"+ans.getValue());
}
}
}

Add a Comment

Your email address will not be published. Required fields are marked *

7 + 3 =