KH19 KH19 - 5 months ago 27
Java Question

How can I get Map getters values

I create Details class and that class create

Map
object. and set
getters setters
.
setdatavalues class I set values to the setters
Then I try to get that values in getdatavalues class. by calling
getters
methord.

problem is I cannot get values in that getters. display empty array.
In getdatavalues calss I create
mymap
object and assign
getMyMap()
method and display the values

public class Details{

private Map<String, String> myMap = new LinkedHashMap<String, String>();

public Details() {
super();
}

public Map<String, String> getMyMap() {
return myMap;
}

public void setMyMap(Map<String, String> myMap) {
this.myMap = myMap;
}
}





public static void setdatavalues(){

LinkedHashMap<String, String> myMap=new LinkedHashMap<String, String>();
ArrayList<String> fields,values=new ArrayList<String>();
Details details= new Details();

|

|

fields=readNumbers();
values=readStrings();
for(int j=0;j<fields.size();j++)
{
myMap.put(fields.get(j),values.get(j));
}
details.setMyMap(myMap);
}
}





public static void getdatavalues(){
Details details= new Details();
//System.out.println(details.getMyMap().values());

Map<String,String> mymap = details.getMyMap();
System.out.println(mymap.values());
}


output

enter image description here

Answer

details that is set values is setdatavalues is thrown away and new empty details is used in getdatavalues. You must pass the Details object that is set data to where data in Details object is printed to print the data set.

Comments