This week's book giveaway is in the Design and Architecture forum.
We're giving away four copies of Communication Patterns: A Guide for Developers and Architects and have Jacqui Read on-line!
See this thread for details.
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

problem with retriving values from hashtable

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

i am storing string array as keys and arraylist as values in my hashtable.when i retrieving the values at particular keys am getting values of last key index to all of my key values.that means the last key values(arraylist)are reflected to all while displaying.can anyone help regarding this

Thanks & regards,
Murali
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your keys are String arrays. Java computes the hashcode of the key. So if the string is for e.g "abc" the hashcode could be "abc".hascode(). Not sure how will the Java compute hash code of a string array. You may have to overwrite the hashCode() method.
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kindly post the code to help us help you better.
{Use code tags while posting code}
I have a strong feeling that you are assigning the values to the Map in a loop and you are not initializing the arraylist for every iteration.

patanjali: You may have to overwrite the hashCode() method.


You mean override? True that an array is a first class object in java but the implementation is not open for modifications. So, one can not override hashcode() method for arrays.
[ February 25, 2008: Message edited by: Nitesh Kant ]
 
Murali Jaya Rao
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if (tmpsub.getSubheaderName().equalsIgnoreCase(nodes[i])){

Vector tmpvecvpd = tmpsub.getVPDElements();
Enumeration vpd = tmpvecvpd.elements();
while(vpd.hasMoreElements()){
VPDElement tmpvpd = (VPDElement)vpd.nextElement();
list.add(tmpvpd.getName());
list.add(tmpvpd.getDefaultSize());
list.add(tmpvpd.getType());
list.add(tmpvpd.getActualSize());
list.add(tmpvpd.getActualValue());
list.add(tmpvpd.getDescription());
}
hash.put(nodes[i], list);
i++;

list.clear();
}
}

}
Enumeration e = hash.keys();
Object obj;
while (e.hasMoreElements()) {
obj=e.nextElement();
System.out.println("hashtable " + obj + ": " + hash.get(obj));

}
}

this is the code,node[i] of type string array and list is of type arraylist.go through the code if any doubts post me.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like "list" always refers to the same single list, which you keep clearing and then putting new items in. All the values in the map are references to the same single list. Instead, you want each value to be a different list. Create a new ArrayList() (or whatever kind of List you're using) for each set of values you want to put in the map.

Also, please follow the link Nitesh gave which explains how to use code tags. They're very useful for making your code readable in this forum.
[ February 25, 2008: Message edited by: Jim Yingst ]
 
Murali Jaya Rao
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jim Yingst ,now i am getting the result what i want.from next time onwards i will definitely follow code tags.
Thanks & Reggards,
Murali.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, it is incorrect to say that your keys here are String arrays. The variable nodes refers to a String array, but nodes[ i ] refers to a single element in the array, which is just a single String, not an array. That's good, because arrays really cannot be used successfully as keys in a Map (Hashtable in this case). The fact is that equals() and hashCode() for an array object are not suitable for use as a key, and you can't override those methods in an array. You'd have been better off with a custom class that contained an array. However, it turns out that this discussion was unnecessary, because your keys are not arrays at all.
[ February 25, 2008: Message edited by: Jim Yingst ]
 
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic