Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

calling method put

call 1:      
aDataMap.put(city, adatalist);
call 2:      
aDataMap.put("city", adatalist);


I have above two ways of calling put method on the map.
call 1: worked fine

call 2:      did not work.


My map was declared as below.
Map<String, List<AData>> aDataMap = new HashMap<String, List<AData>>();

since first argument is String, I passed it as "city" which did not work for some reason.

I would like to know differences between calling above two ways.

Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
Avatar of CPColin
CPColin
Flag of United States of America image

In what way did the second way not work? You're passing a String object with the value "city", which should work fine. Were you getting some kind of error?
Avatar of CEHJ
You must understand that if it makes sense in the first place (i guess it doesn't) then the map will only contain the last occurrence of your using "city" - since a Map doesn't allow duplicate keys
on what basis you are saying that call 1 is working but call 2 not.

can you clarify that?
>> call 1:      
>> aDataMap.put(city, adatalist);
Can you tell us what kind of variable 'city' is?

>> call 2:      did not work.
Can you tell us how you conclude that it doesn't work?
The best way to check if the put "worked", is immediately after the put calling a get with the same key and check if it returns the value you just put.

Remark: the more information you give us, the better we can help you.
Avatar of gudii9

ASKER

city represenets the xls sheet name which is variable not constant.
Sheet1 is Detroit
Sheet2 is Chicago
Sheet3 Newyork etc

Please advise
Please also answer my second question
Avatar of gudii9

ASKER

Sorry regarding second question

when i call get method similar to put method
System.out.println("get values "+aDataMap.put("city", adatalist));

getting compilation error as follows


The method get(Object) in the type Map<String,List<AData>> is not applicable for the arguments (String, List).Remove argument to match get(Object)


Please advise
SOLUTION
Avatar of CPColin
CPColin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You put a value for a given key:  map.put(key, value);
You get a value for a given key:  value = map.get(key);
So, you should get like CPColin said.

If you don't know the above, I just wonder how you could say
>> call 1: worked fine
????
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

aDataMap.put("city", adatalist);


is giving last sheet values which is not correct


whereas

aDataMap.put(city, adatalist);

is giving all sheet values in the generated XML which is correct in my case.


I also changed HashMap to LinkedHashMap to read in the alphabetical order all cities not in andom haphazard manner.


When i try to call get method I am getting as object value in my system.out.println for all the 4 sheets corresponding to 4 cities. How to see proper data using get in this case.


>>>aDataMap.put("city", adatalist);
>>>List<AData> resultOutOfMap = aDataMap.get("city");
>>>System.out.println("get values " + resultOutOfMap);
com.mortgagefamily.nmls.dto.AData@12d263f
com.mortgagefamily.nmls.dto.AData@77d263f
com.mortgagefamily.nmls.dto.AData@55d263f
com.mortgagefamily.nmls.dto.AData@49d263f


Please advise
whereas

aDataMap.put(city, adatalist);

is giving all sheet values in the generated XML which is correct in my case.

Well that's a completely different approach, using a variable as a key and not a String literal. What does

System.out.println(city);

Open in new window

give?
How to see proper data using get in this case.
By providing a proper toString method for AData
Avatar of gudii9

ASKER

>>>using a variable as a key and not a String literal.


Can you please elaborate more on above statement.
I was not sure on above concept. Please advise.


city I am getting in the for loop by reading reach sheet of the excel


      for(int i=0;i<noOfSheet;i++){
                        HSSFSheet mySheet = myWorkBook.getSheetAt(i);
                        System.out.println("myWorkBook.getSheetName(i)"+myWorkBook.getSheetName(i));


inside above for loop I have while loop under that


while(rowIter.hasNext()){
                              HSSFRow myRow = (HSSFRow) rowIter.next();
                              HSSFCell cell = myRow.getCell((short)0);


Map<String, List<AData>> aDataMap = new HashMap<String, List<AData>>();

aDataMap.put(city, adatalist);




      String city=myWorkBook.getSheetName(i);



System.out.println(city);


gives Detroit, Chicago, NewYork et for each iteration of for loop.


>>By providing a proper toString method for AData

I tried toString  as below but it did not work. Do I need to change something. Please advise
>>>aDataMap.put("city", adatalist);
>>>List<AData> resultOutOfMap = aDataMap.get("city");
>>>System.out.println("get values " + resultOutOfMap.toString());
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

How to implement get method to print something declared like this

Map<String, List<AData>> aDataMap = new HashMap<String, List<AData>>();


Please advise
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanx 4 axxepting