• 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

Primitive data types in Hashtables

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

A Hashtable is a useful construct for storing collections of Objects. But it does not seem to work for Primitive (non-object) variables such as doubles.
I could always use arrays, but I would like to use the Hashtable's functionality.
Another solution would be to encapsulate the primitive in a simple class, but I suppose I'm looking for an elegant solution?
Suggestions? What am I missing here? Thanks in advance.
 
Ranch Hand
Posts: 297
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, HashTables only take non-null objects as keys and values. Looks like you'd have to stick your primitive in one of Java's wrapper classes, i.e. Integer, Boolean etc. and then turn 'em back to primitives when you need them.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you could create a wrapper for the Hashtable:
public class IntTable {
private Hashtable table;
....
public void put(String key, int i) {
table.put(new Integer(key,i));
}

public int get(String key) {
return (int)table.get(key);
}
[This message has been edited by eric moon (edited December 20, 2000).]
 
Michael Mendelson
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Michael and Eric, thanks for your replies.
Eric, I will use your solution (since I'm planning to write a wrapper anyway). In a sense, it incorporates Michael's solution transparently.
 
Michael Hildner
Ranch Hand
Posts: 297
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very cool idea, Eric. I decided to re-write some code I had. Please correct me if I'm wrong, but wouldn't one have to use the intValue() method of Integer to retrieve the int? I mean you can't cast an Integer to an int, right?
As long as I'm being picky, I think this piece of code:

should be:

Again, please correct me if I'm missing something.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic