Creating a double array (one row for states, and one for capitols), I am trying to use 'map.put' in a for loop to save the arrays 'key(states)' and 'value(capitols)' to the HashMap. When using a key from user input after assigning a new HashMap (hMap = getInfo();, My output returns "null". Im not quite sure what it is im doing wrong, but I have a feeling I made an error in the for loop.
public class HashMapProgram {
public static void main (String[]args) {
Scanner input = new Scanner(System.in);
//Assign contents of map in getInfo to hMap
HashMap<String, String> hMap = getInfo();
//Prompting user to input a state (key)
System.out.print("Enter a state, or \"done\" when finished: ");
String state = input.next();
if(hMap.get(state) != "done")
System.out.println("The capital is "+ hMap.get(state));
}
public static HashMap<String, String> getInfo(){
//HashMap to save contents in
HashMap<String, String> map = new HashMap<>();
String x[][] = {
{"Alabama","Alaska","Arizona" ,"Arkansas","California","Colorado","Connecticut","Delaware","Florida","Georgia",
"Hawaii" ,"Idaho" ,"Illinois" ,"Indiana" ,"Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland",
"Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire","New Jersey",
"New Mexico","New York","North Carolina","North Dakota","Ohio","Oklahoma","Oregon","Pennsylvania", "Rhode Island", "South Carolina",
"South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington","West Virginia","Wisconsin","Wyoming"},
{"Montgomery","Juneau","Phoenix","Little Rock","Sacramento","Denver","Hartford","Dover","Tallahassee","Atlanta",
"Honolulu","Boise","Springfield","Indianapolis","Des Moines","Topeka","Frankfort","Baton Rouge","Augusta", "Annapolis",
"Boston","Lansing","St. Paul","Jackson","Jefferson City","Helena","Lincoln","Carson City","Concord","Trenton",
"Santa Fe","Albany","Raleigh","Bismarck","Columbus","Oklahoma City","Salem","Harrisburg","Providence","Columbia",
"Pierre","Nashville","Austin","Salt Lake City","Montpelier","Richmond","Olympia","Charleston","Madison","Cheyenne"}
};
//Saving contents in 'map'
for(int i = 0; i < x.length; i++) {
map.put(x[0][i], x[1][i]);
}
return map;
}
}
There are a few errors:
1) In your for-loop, change i < x.length; to i < x[0].length;, otherwise you're running the loop just 2 times.
2) Don't compare strings using !=. Use equals() instead. See this for more details.
3) You don't have a loop to ask for user input repeatedly. Change your code in main() to:
Scanner input = new Scanner(System.in);
HashMap<String, String> hMap = getInfo();
String state = "";
do {
System.out.print("Enter a state, or \"done\" when finished: ");
state = input.next();
System.out.println("The capital is " + hMap.get(state));
} while (!state.equals("done"));
4) Work with interface, not class. So change
HashMap<String, String> hMap = getInfo();
to
Map<String, String> hMap = getInfo();
and also update the method signature to return Map<String, String>.
5) Since Java 9, you can directly create a map like this:
Map<String, String> m = Map.of(
"Alabama", "Montgomery",
"Alaska", "Juneau",
"Arizona", "Phoenix"
//and so on...
);
Related
I need to do this for school. Its supposed to be a JAVA project.
So for example, if we give an input:
thomas teacher
charlie student
abe janitor
jenny teacher
The output will be:
teachers,thomas,jenny
students,charlie,
janitor,abe.
I am just a beginner so, so far I have this code:
`Scanner in = new Scanner(System.in);
String line = in.nextLine();
String[] words = line.split(" ");
//TreeMap treemap = new TreeMap();
ArrayList<String> admin = new ArrayList<String>();
Scanner input = new Scanner(System.in);
while(true){
Boolean s = input.nextLine().equals("Done");
//treemap.put(line, "admin");
if(words[1].contentEquals("admin")){
admin.add(words[0]);
}
else if(s == true){
break;
}
}
System.out.println("admins," + "," + admin);`
I was originally using a treemap but I don't know how to make it work so I thought of using an ArrayList and eliminating the brackets at the end.
EDIT:
So I now have the code:
HashMap<String, String> teacher = new HashMap<String, String>();
HashMap<String, String> student = new HashMap<String, String>();
HashMap<String, String> janitor = new HashMap<String, String>();
System.out.println("Enter a name followed by a role.");
Scanner in = new Scanner(System.in);
String line = in.nextLine();
Scanner name = new Scanner(System.in);
String r = name.nextLine();
while(true){
if(line.equals(r + " " + "teacher")){
teacher.put(r, "teacher");
}
}
I'll give you the hint because you should do it yourself.
Use a HashMap<String, List<String>> map and insert your inputs like below:
if(map.containsKey(words[1]))
{
List<String> list = map.get(words[1]);
list.add(words[0]);
map.put(words[1],list);
}
else
{
map.put(words[1],Arrays.asList(words[0]))
}
This way you will get list of names corresponding to each types(student/teacher) etc.
After that iterate over the map and print the list.
I think for a small amount of occupations it's reasonable to accomplish this using just array lists. I think the part you are having trouble with is the input structure so I'll help you out with an example of how to do that part and let you handle the filtering on your own:
private List<String> teachers = new ArrayList<>();
private List<String> students = new ArrayList<>();
private List<String> janitors = new ArrayList<>();
public void seperatePeople(){
Scanner in = new Scanner(System.in);
while(true){
//Keep getting the next line in an infinite loop
String line = in.nextLine();
if(line.equals("Done")){
break; //end the loop
}else{
//Split on the spaces
String[] personArray = line.split(" ");
//Remember each line is structured like : name, occupation
//So when we split the line the array list we get from it
//will be in the same order
putInArray(personArray[0], personArray[1]);
}
}
//Do whatever printing you have to do down here
}
private void putInArray(String name, String occupation) {
//filter and add to the correct list in here
}
If you wanted to implement this using a hashmap the input method would be the same, but instead of putting names into 3 pre-made occupation arraylists you create arraylists and put them inside a hashmap as you go:
private HashMap<String, List<String>> peopleHashMap = new HashMap<>();
public void seperatePeople(){
Scanner in = new Scanner(System.in);
while(true){
//Keep getting the next line in an infinite loop
String line = in.nextLine();
if(line.equals("Done")){
break; //end the loop
}else{
//Split on the spaces
String[] personArray = line.split(" ");
//Remember each line is structured like : name, occupation
//So when we split the line the array list we get from it
//will be in the same order
putInArray(personArray[0], personArray[1]);
}
}
//You can get all the keys that you created like this
List<String> keys = new ArrayList<>(peopleHashMap.keySet());
}
private void putInArray(String name, String occupation) {
if(peopleHashMap.containsKey(occupation)){
//If the key (occupation in this case) is already in the hashmap, that means that we previously
//made a list for that occupation, so we can just the name to that list
//We pull out a reference to the list
List<String> listOfNames = peopleHashMap.get(occupation);
//And then put the new name into that list
listOfNames.add(name);
}else{
//If the key isn't in the hashmap, then we need to make a new
//list for this occupation we haven't seen yet
List<String> listOfNames = new ArrayList<>();
//We then put the name into the new list we made
listOfNames.add(name);
//And then we put that new list with the into the hashmap with the occupation as the key
peopleHashMap.put(occupation, listOfNames);
}
}
I'm working on Rest API testing (POST method) for which I'm reading json data from spreadsheet using TestNg Dataprovider.
My Dataprovider returns HashMap with key: Integer Row_Number and value: ArrayList (String) of test data. Below is the sample map returned by DataProvider.
{0=[Sample1, Name1, sample1.name1#example.com, (000) 111-1111], 1=[Sample2, Name2, sample2.name2#example.com, (000) 111-1112]}
My current implementation of Dataprovider is,
#DataProvider
public Object[][] JSONBODY()
{
String test_data = "json_data";
int row = ExcelUtils.getRowNum(test_data, col_num);
int total_col = ExcelUtils.getLastColumnNumber(row);
Map<Integer, ArrayList<String>> map = ExcelUtils.getTableArray(spreadsheet_location,test_data,total_col);
return new Object[][] { { map } };
}
getTableArray implementation
public static Map<Integer, ArrayList<String>> getTableArray(String FilePath, String testdata, int total_Col) throws Exception {
Map<Integer, ArrayList<String>> map = new HashMap<Integer, ArrayList<String>>();
ArrayList<Integer> iTestCaseRow = null;
try
{
FileInputStream ExcelFile = new FileInputStream(FilePath);
ExcelWBook = new XSSFWorkbook(ExcelFile);
ExcelWSheet = ExcelWBook.getSheet(SheetName);
int startCol = 1;
iTestCaseRow = ExcelUtils.getRowContains(testdata ,col_num); // getRowContains returns list of row numbers for value in testdata.
int totalRows = iTestCaseRow.size();
int totalCols = total_Col;
for(int i=0; i<totalRows;i++)
{
ArrayList<String> str = new ArrayList<String>();
for (int j=startCol;j<=totalCols;j++)
{
str.add (ExcelUtils.getCellData(iTestCaseRow.get(i),j));
}
map.put(iTestCaseRow.get(i), str);
}
return map;
}
}
Test Method
#Test(dataProvider = "JSONBODY")
public void TestMethod(Map<Integer, ArrayList<String>> map) throws Exception {
try
{
Log.startTestCase("Start executing Test Case");
Set<Integer> key = map.keySet();
for(Integer row: key)
{
SamplePojo pojo = new SamplePojo();
ArrayList<String> data = map.get(row);
pojo.setFirstName(data.get(0));
pojo.setLastName(data.get(1));
pojo.setEmail(data.get(2));
pojo.setPhone(data.get(3));
Response res = RestAssured.given().contentType(ContentType).body(pojo).when().post(POST_URL);
Log.info(res.asString());
Assert.assertTrue(res.getStatusCode() == 200 , "Test Case failed");
}
}
}
Spreadsheet Test Data is,
Spreadsheet Data
When I execute my #Test method, TestNG executes as one method instead of two as I have 2 rows of test data(value: json_data) in the spreadsheet.
Kindly help me in running the Test method individually for each key:value pair.
Thanks in advance!
The problem is in your data provider.
After you obtain your map, you need to translate that map such that every entry in it is now part of the 2D Object array. In your case, you have basically just added that entire map as one single data item in the 2D object array.
Please see below for a full fledged example that shows what I am referring to. For the sake of convenience I have basically excluded the excel spreadsheet reading logic etc.,
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestClass {
#Test(dataProvider = "dp")
public void testMethod(Map<Integer, List<String>> data) {
Assert.assertTrue(data.size() == 1);
List<String> values = data.values().iterator().next();
System.err.println("Values = " + values);
}
#DataProvider(name = "dp")
public Object[][] getData() {
Map<Integer, List<String>> data = getTableArray();
//Transform the Map into a 2D array such that every key/value
//pair in the map becomes one element in the 2D array
int size = data.size();
Object[][] dataToUse = new Object[size][1];
int i = 0;
for (Map.Entry<Integer, List<String>> entry : data.entrySet()) {
Map<Integer, List<String>> localMap = new HashMap<>();
localMap.put(entry.getKey(), entry.getValue());
dataToUse[i++] = new Object[]{localMap};
}
return dataToUse;
}
static Map<Integer, List<String>> getTableArray() {
Map<Integer, List<String>> data = new HashMap<>();
data.put(1, Arrays.asList("Sample1", "Name1", "sample1.name1#gmail.com", "(000) 111-1111"));
data.put(2, Arrays.asList("Sample2", "Name2", "sample2.name2#gmail.com", "(000) 111-1112"));
data.put(3, Arrays.asList("Sample3", "Name3", "sample3.name3#gmail.com", "(000) 111-1113"));
return data;
}
}
Here's the output
Values = [Sample1, Name1, sample1.name1#gmail.com, (000) 111-1111]
Values = [Sample2, Name2, sample2.name2#gmail.com, (000) 111-1112]
Values = [Sample3, Name3, sample3.name3#gmail.com, (000) 111-1113]
===============================================
Default Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================
Two options:
Map<Integer, ArrayList<String>> map = ExcelUtils.getTableArray(spreadsheet_location,test_data,total_col);
Object[][] dataToBeReturned = new Object[map.size()][];
//Loop through map and build your array..code not tested..something to the effect
for(Entry<Integer, Arra..> datum : map.entrySet()) {
dataToBeReturned[i++] = new Object[] {datum.getKey(), datum.getValue()}
}
return dataToBeReturned;
or in your excelreader itself, since you are in any case looping through the data, either put it in an array instead of map - something like
instead of map.put(iTestCaseRow.get(i), str);
use dataToBeReturned[i++] = new Object[] {iTestCaseRow.get(i), str}
I've created the following:
import java.util.*;
public class morse5 {
public static void main(String [] args)
{
//Declare Variables
String [][] myIndexAlphaMorse = {{"a", ".-"}, {"b", "-..."}, {"c", "-.-."}, {"d", "-.."}, {"e", "."}, {"f", "..-."}, {"g", "--."}, {"h", "...."}, {"i", ".."}, {"j", ".---"}, {"k", "-.-"}, {"l", ".-.."}, {"m", "--"}, {"n", "-."}, {"o", "---"}, {"p", ".--."}, {"q", "--.-"}, {"r", ".-."}, {"s", "..."}, {"t", "-"}, {"u", "..-"}, {"v", "...-"}, {"w", ".--"}, {"x", "-..-"}, {"y", "-.--"}, {"z", "--.."}, {"1", ".----"}, {"2", "..---"}, {"3", "...--"}, {"4", "....-"}, {"5", "....."}, {"6", "-...."}, {"7", "--..."}, {"8", "---.."}, {"9", "----."}, {"0", "-----"}, {" ", "|"}};
//Test
System.out.println(Arrays.deepToString(myIndexAlphaMorse));
System.out.println(myIndexAlphaMorse[8][0]);
}
What I would like to know is how to get the value of the corresponding position based on user input. I'm learning, so I just want the piece on how to get, as an example, .- back when "a" is entered.
Simply iterate over you array and compare the 0th element at each position with the character you are looking for.
String input = "v";
String result= "";
for(int i = 0; i < myIndexAlphaMorse.length; i++){
if(myIndexAlphaMorse[i][0].equals(input)){
result = myIndexAlphaMorse[i][1];
break;
}
}
System.out.println("morse for " + input + " = " + result);
But as the other answer says you should use a map that whould fit perfect for this task.
You should consider using a Map instead of a two dimensional array for this problem:
Map<String, String> myIndexAlphaMap = new HashMap<>();
myIndexAlphaMap.put("a", ".-");
myIndexAlphaMap.put("b", "-...");
// etc.
// given user input of "a" you can access via
myIndexAlphaMap.get("a");
Or a map of string arrays
Map<String, String[]> myIndexAlphaMap = new HashMap<String, String[]>();
myIndexAlphaMap.put("a", new String {".","-"});
myIndexAlphaMap.put("b", new String {"-",".","."});
// given user input of "a" you can access via
myIndexAlphaMap.get("a")[0];
You can also use Hash tables, since they've already given sample above of HashMap/Map:
Hashtable<String, String> table = new Hashtable<String, String>();
table.put("a", ".-");
table.put("b", "-...");
It is also synchronized and thread safe unlike HashMaps, albeit is a smidgen slower for bigger data sets.
First of all you have to read the letter as the String object. Then you can just iterate through your array and when we find what we are looking for - just print it and break the loop:
Scanner scanner = new Scanner(new InputStreamReader(System.in));
String letter = scanner.next();
for (int i=0; i<myIndexAlphaMorse.length; i++)
if (myIndexAlphaMorse[i][0].equals(letter)){
System.out.println(myIndexAlphaMorse[i][1]);
break;
}
I have two arraylists as follows.
ArrayList<String> keys = new ArrayList<>();
ArrayList<String> values = new ArrayList<>();
keys.add("1","1","1","2","2","3");
values.add("2016-06-22 07:18:45", "2016-06-22 08:18:45", "2016-06-22 09:18:45",
"2016-06-22 03:18:45","2016-06-22 04:18:45","2016-06-22 01:18:45");
Now i need the function
HashMap latestValues(keys, values);
The output should be as follows,
["1"=>"2016-06-22 09:18:45","2"=>"2016-06-22 04:18:45", "3"=>"2016-06-22 01:18:45"]
Returning the latest dated single value for that particular key. Can anyone please suggest how to achieve this.
Thanks and advance!
I think this will do it.
public static void main(String[] args) {
ArrayList<String> keys = new ArrayList<>(Arrays.asList("1", "1", "1", "2", "2", "3"));
ArrayList<String> values = new ArrayList<>(Arrays.asList("2016-06-22 07:18:45", "2016-06-22 08:18:45", "2016-06-22 09:18:45",
"2016-06-22 03:18:45", "2016-06-22 04:18:45", "2016-06-22 01:18:45"));
HashMap<String, String> map = new HashMap<String, String>();
for (int i = 0; keys.size() == values.size() && i < keys.size(); i++) {
String key = keys.get(i);
String value = values.get(i);
if (!map.containsKey(key) || dateAsNo(value) > dateAsNo(map.get(key))) {
map.put(key, value);
}
}
System.out.println(map);
}
public static long dateAsNo(String v) {
return Long.parseLong(v.replaceAll("\\D", ""));
}
It will only work if all the dates have the same format yyyy-MM-dd hh:mm:ss
You can use the following approach. Values will be overwritten only when date is newer (I am assuming you are not changing the date format)
Map<String,String> hashMap = new HashMap<>();
List<String> keys = new ArrayList<>();
List<String> values = new ArrayList<>();
if(keys.size() != values.size())
return null;
for(int i=0; i<keys.size(); i++){
String keyVal = hashMap.get(keys.get(i));
if(keyVal==null || keyVal.compareTo(values.get(i))>0){
// second condition ensures value is only replaced if it is a later date
hashMap.put(keys.get(i), values.get(i));
}
}
return hashMap;
Try the same thing that you would do if you wre doing this manually. set your current key and go through your list till you find different key and put the current key and appropriate value (that is the value with the same index) in your map
public static void main (String [] args) throws IOException{
ArrayList<String> keys = new ArrayList<>();
ArrayList<String> values = new ArrayList<>();
keys.add("1");keys.add("1"); keys.add("1");
keys.add("2");keys.add("2");
keys.add("3");
values.add("2016-06-22 07:18:45");values.add("2016-06-22 08:18:45");values.add("2016-06-22 09:18:45");
values.add("2016-06-22 03:18:45");values.add("2016-06-22 04:18:45");
values.add("2016-06-22 01:18:45");
Map<String,String> myMap = new HashMap<>();
String currentKey = keys.get(0); // set your current key and go through your list till you find the next key and put the current key and appropriate value (that is the value with the same index) in your map
for(int i = 0;i<keys.size();i++){
if(!currentKey.equalsIgnoreCase(keys.get(i))){
myMap.put(currentKey, values.get(i-1));
currentKey = keys.get(i);
}
if(i==keys.size()-1){
myMap.put(currentKey, values.get(i));
}
}
System.out.println(myMap);
}
you can write just like :
package java7.demo;
import java.util.*;
public class MapTest {
public static void main(String args[]){
ArrayList<String> keys = new ArrayList<>();
ArrayList<String> values = new ArrayList<>();
keys.add("1");
keys.add("1");
keys.add("1");
keys.add("2");
keys.add("2");
keys.add("3");
values.add("2016-06-22 07:18:45");
values.add("2016-06-22 08:18:45");
values.add("2016-06-22 09:18:45");
values.add("2016-06-22 03:18:45");
values.add("2016-06-22 04:18:45");
values.add("2016-06-22 01:18:45");
LinkedHashMap<String,String>map = new LinkedHashMap<String,String>();
for(int i =0; i<keys.size();i++){
map.put(keys.get(i),values.get(i));
}
System.out.println(map);
}
}
as Map(include hashmap,linkedhashmap)replace old key value with new key value if old key value and new key value has same value,you will get 1->2016-06-22 09:18:45.at first it will put[ 1,2016-06-22 07:18:45]to map,in second loop,it will replace with [1,2016-06-22 08:18:45].int third time loop,it will replace with [1,09:18:45].to get data according to insertion Order,I use LinkedHashMap.
Map<Object,String> mp=new HashMap<Object, String>();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the instrument name");
String name=sc.next();
mp.put(name, "Control Valve");
mp.put(name, "BDV");
mp.put(name, "SDV");
mp.put(name, "ON-OFF VALVE");
mp.put(name,"Analyser");
Set s=mp.entrySet();
Iterator it=s.iterator();
while(it.hasNext())
{
Map.Entry m =(Map.Entry)it.next();
String key=(String)m.getKey();
String value=(String)m.getValue();
System.out.println("Instrument name :"+key+" fields:"+value);
}
}
}
In this only last value is mapped to the key .i.e analyser to key .
How to map all the values to one key entered by the user .And also it has to ask user to enter values for each value field.
updated code -:It asks for instrument name but then shows exception "java.util.ArrayList cannot be cast to java.lang.String"
Map<String,List<String>> mp=new HashMap<String,List<String>>();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the instrument name");
String name=sc.next();
List<String> valList = new ArrayList<String>();
valList.add("Control Valve");
valList.add("BDV");
valList.add("SDV");
valList.add("ON-OFF VALVE");
valList.add("Analyser");
mp.put(name, valList);
Set s=mp.entrySet();
Iterator it=s.iterator();
while(it.hasNext())
{
Map.Entry m =(Map.Entry)it.next();
String key=(String)m.getKey();
String value=(String)m.getValue();
System.out.println("Instrument name :"+key+" fields:"+value);
}
}
}
I would suggest to use
Map<Object,List<String>> mp=new HashMap<Object, List<String>>();
So that you can maintain set of values to a given key.
if a list available for given key , get the list and add the new value to list.
UPDATED
Map<String,List<String>> mp=new HashMap<String,List<String>>();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the instrument name");
String name=sc.next();
List<String> valList = new ArrayList<String>();
valList.add("Control Valve");
valList.add("BDV");
valList.add("SDV");
valList.add("ON-OFF VALVE");
valList.add("Analyser");
mp.put(name,valList);
for(String key : mp.keySet()){
System.out.print("Instrument name :"+key+" Values : ");
for(String val : mp.get(key)){
System.out.print(val+",");
}
}
I suspect you should be using a custom class with a field for each piece of information you need. This can be added once into the map for each name
public static void main(String... args) {
Map<String, MyType> mp = new LinkedHashMap<String, MyType>();
Scanner sc = new Scanner(System.in);
System.out.println("Enter the following comma separated with a blank line to stop.");
System.out.println("instrument name, Control Value, BDV, SDV, ON-OFF VALVE, Analyser");
for (String line; (line = sc.nextLine()).length() > 0; ) {
MyType mt = new MyType(line);
mp.put(mt.getName(), mt);
}
System.out.println("you entered");
for (MyType myType : mp.values()) {
System.out.println(myType);
}
}
static class MyType {
private final String name, controlValue, bdv, sdv, onOffValue, analyser;
MyType(String line) {
String[] parts = line.trim().split(" *, *");
name = parts[0];
controlValue = parts[1];
bdv = parts[2];
sdv = parts[3];
onOffValue = parts[4];
analyser = parts[5];
}
public String getName() {
return name;
}
public String getControlValue() {
return controlValue;
}
public String getBdv() {
return bdv;
}
public String getSdv() {
return sdv;
}
public String getOnOffValue() {
return onOffValue;
}
public String getAnalyser() {
return analyser;
}
#Override
public String toString() {
return "MyType{" +
"name='" + name + '\'' +
", controlValue='" + controlValue + '\'' +
", bdv='" + bdv + '\'' +
", sdv='" + sdv + '\'' +
", onOffValue='" + onOffValue + '\'' +
", analyser='" + analyser + '\'' +
'}';
}
}
if given the following input prints
Enter the following comma separated with a blank line to stop.
instrument name, Control Value, BDV, SDV, ON-OFF VALVE, Analyser
a,b,c,d,e,f
1,2,3,4,5,6
q,w,e,r,t,y
you entered
MyType{name='a', controlValue='b', bdv='c', sdv='d', onOffValue='e', analyser='f'}
MyType{name='1', controlValue='2', bdv='3', sdv='4', onOffValue='5', analyser='6'}
MyType{name='q', controlValue='w', bdv='e', sdv='r', onOffValue='t', analyser='y'}
You may need to use google guava multimap to achieve this.
A collection similar to a Map, but which may associate multiple values with a single key. If you call put(K, V) twice, with the same key but different values, the multimap contains mappings from the key to both values.
(or)
Change value as List and add all values which have same key to that list
Example:
List<String> valList = new ArrayList<String>();
valList.add(val1);
valList.add(val2);
mp.put(name, valList);
I would suggest looking at a Guava MultiMap
A collection similar to a Map, but which may associate multiple values
with a single key. If you call put(K, V) twice, with the same key but
different values, the multimap contains mappings from the key to both
values.
Otherwise you will have to implement a Map of Strings to some Java collection. That will work, but it's painful since you have to initialise the collection every time you enter a new key, and add to the collection if the empty collection exists for a key.
You may have a map of string array. Then you should retrive the array list and add new value.
Map<Object,ArrayList<String>> mp=new HashMap<Object, ArrayList<String>>();