aspose file tools*
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 Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Get Hashtable from ArrayList" Watch "Get Hashtable from ArrayList" New topic
Author

Get Hashtable from ArrayList

Pratik Lohia
Ranch Hand

Joined: May 05, 2005
Posts: 88
Hi

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);
if(beforeHash.get(""+pomVO.getPomId())!=null)
{
tempArrayList = (ArrayList)beforeHash.get(""+pomVO.getPomId());
tempArrayList.add(pomVO);
}else
{
tempArrayList = new ArrayList();
tempArrayList.add(pomVO);
}
beforeHash.put(""+pomVO.getPomId(),tempArrayList);
}



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
FOUND THE SOLUTION
public void getHashMapFromArrayList(ArrayList list,String variableName){
HashMap resultMap=new HashMap();
try{
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();
System.err.println("obj.instance"+valueId);
resultMap.put(valueId+"",obj);
}
}catch(Exception e){
e.printStackTrace();
}

}
 
jQuery in Action, 2nd edition
 
subject: Get Hashtable from ArrayList
 
Similar Threads
Global array is null after its been filled
I passed, but I suffered the 44/80 locking penalty. Why?
arrayList.clear() VERSUS arrayList = new ArrayList()
Sorting of hashtables in a vector
How can we append/retrieve serialized objects to/from a file?