• 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

better way?

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I basically want to check a key in a hashtable (dog) and if there is a key named dog pull the value which is his name.

but it seems like alot of moving around, is there a better way to do this?

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can factor out the variable "dog", and you don't need to construct a new Hashtable (very inefficient, and totally unnecessary!)




You could remove the check for null if you assured that getHashTableData() never returned null -- generally a smarter, cleaner way to program. Then you could write it in one line:

String dogName = (String) myClass.getHashTableData().get("DOG");
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Avoid ever having a null collection. It's much better to assume you have a valid collection and check the size for something greater than zero. Also, unless you have an old version of Java you should specifiy what is in your collection. Never use Hashtable: use HashMap. You can synchronize access to your HashMap, if needed, and use it consitently everywhere. Finally, always declare the collection interface not the concrete type.

 
reply
    Bookmark Topic Watch Topic
  • New Topic