• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Get Hashtable from ArrayList

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Pratik Lohia
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}

}
 
Life just hasn't been the same since the volcano erupted and now the air is full of tiny ads.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic