File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Get Hashtable from ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Get Hashtable from ArrayList" Watch "Get Hashtable from ArrayList" New topic

Get Hashtable from ArrayList

Pratik Lohia
Ranch Hand

Joined: May 05, 2005
Posts: 88

I need to get a Hashtable from an arrayList.

My purpose is to write a generic method, to which I will pass an ArrayList, the Objects contained in the ArrayList and the key that should be used for the Hashtable.
Important points to be considered here are:

1. The Values in the Hashtable will again be ArrayLists(Each ArrayList containing a set of VOs, with the same Id as that of the key).

2. The key itself needs to be fetched from the VO in this method, and then checked for its presence in the Hashtable. If it is not present, then a new ArrayList will be created for that key, and the VO will be added to this list. Thereafter this list will be put in the Hashtable against that key.

3. If the key is already present, then the ArrayList against that key will be fetched and the VO object will now be added to this ArrayList and this arraylist will again be set in the Hashtable.

I am attaching the code snippet that works for a specific VO. I want a method to work for any kind of VO.

ArrayList tempArrayList;
for(int i=0;i<beforePOMList.size();i++)
POMVO pomVO = (POMVO)beforePOMList.get(i);
tempArrayList = (ArrayList)beforeHash.get(""+pomVO.getPomId());
tempArrayList = new ArrayList();

The problem I am facing is that though I try to create a variable of type object, I try to instantiate it in a if condition, depending upon the type of VO the user coder wants. Now this instantiated object obviously wont be available outside the if loop. So outside the loop there is no way for me to get the key from the VO.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
I'm not sure what you're asking, but it seems to be "how can I extract
an ID from an object when I don't know its type". The method you used
was called getPOMId, but that can vary, I suppose. I can think of two
ways, off the top of my head:
1. Design your objects (you seem to call them VO) to implement a
common interface:

2. If that can't be done, you could always pass into your routine one more object,
one that knows how to extract ids from the VOs in question:

[ November 22, 2005: Message edited by: Jeff Albrechtsen ]

There is no emoticon for what I am feeling!
Pratik Lohia
Ranch Hand

Joined: May 05, 2005
Posts: 88
public void getHashMapFromArrayList(ArrayList list,String variableName){
HashMap resultMap=new HashMap();
for(int i=0;i<list.size();i++){
Object obj=list.get(i);
Class listClass=obj.getClass();
Class[] paramTypes = null;
Method method = listClass.getMethod(variableName, paramTypes);
int valueId=new Integer(method.invoke(obj,paramTypes).toString()).intValue();
}catch(Exception e){

I agree. Here's the link:
subject: Get Hashtable from ArrayList
It's not a secret anymore!