• 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

Hashtable, put and get sanity check

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm hanging tough, please help.
This is a example from Java 2 Cert. study guide Roberts and Heller page 52
The hierarchy exists - Person is the base class and Parent is a sub.
public class Classroom {
private Hashtable inTheRoom = new Hashtable(); //creates hastable object
public void enterRoom(Person p) {
inTheRoom.put(p.getName(), p);
// hashtable item is added with (p.getName()) as the key and p the object.
// getName is not in the API? what does this do?
}
public Person getParent(String name) {
Object p = inTheRoom.get(name);
// p holds key/value "name" of type Object from the hashtable inTheRoom
if (p instanceof Parent) {
return (Parent)p;
}
else {
return null;
}
}
}
Looking at the API and this example:
Hashtable myhashtable = new Hashtable();
myhashtable.put("one", new Integer(1));
myhashtable.put("two", new Integer(2));
myhashtable.put("three", new Integer(3));
Where the number names are the keys.
The objects are wrapper Integer objects representing the values(1,2,3).
Now looking at the code snipplet:
inTheRoom.put(p.getName(),p);
//the hashTable is "inTheRoom"
//"put" is putting the "p.getName()" key and object p passed in(Person)p)).
Am I getting this stuff right?
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


public void enterRoom(Person p) {
inTheRoom.put(p.getName(), p);
// hashtable item is added with (p.getName()) as the key and p the object.
// getName is not in the API? what does this do?


p.getName() is a method in class Person.
It returns the name of the person as a String object.
The signature is: Map.put(Object, Object)
As String IS_A object the method invocation is correct.
reply
    Bookmark Topic Watch Topic
  • New Topic