File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Extract HashMap array value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Extract HashMap array value" Watch "Extract HashMap array value" New topic
Author

Extract HashMap array value

Adrian Airmil
Greenhorn

Joined: Dec 12, 2005
Posts: 24
The value in the hashmap is an array and i wish to get that array to loop through it.



However result in the browser is:

Would appreciate if someone could point out whats wrong in that code. Thanks
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Use generics (if JDK 1.5 or higher) to specify at compile time what would the collection be storing. Else cast the value to a String array and use it.
Jaime M. Tovar
Ranch Hand

Joined: Mar 28, 2005
Posts: 133
The String array is an object by its own. When you ask in the hashmap for the value of the key you get the array object. Try to wrap the array so it will print in a clean way. You can use the array to create a structure that will make it easier to handle the data. Something like a Collection or alike.


She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
gkumar kumar
Greenhorn

Joined: May 01, 2007
Posts: 8
Hi,
Extract array from hashmap for the example you mentioned, code is as follows

HashMap hmap=new HashMap();
hmap.put("storeKey", new String[]{"first","second"});
String obj[]=(String[])hmap.get("storeKey");

for(int i=0;i<obj.length;i++){
System.out.println(obj[i]);
}


Thanks&Regards,<br />gkumar
Adrian Airmil
Greenhorn

Joined: Dec 12, 2005
Posts: 24
Thanks everyone. Wrapping it made it work.
 
 
subject: Extract HashMap array value