Hash table Java insert
hashtable, java
Solution
You are doing things correctly. Remember, a `Hashtable` is not a direct-access structure. You can't "get the third item from a `Hashtable`", for example. There is no real meaning to the term "index" when you're talking about a `Hashtable`: numerical indexes of items mean nothing.
A `Hashtable` guarantees that it will hold key-value pairs for you, in a way that it will be very fast to conclude a value based on a key (for example: given `Donald`, you will get `Trump` very quickly). Of course, certain conditions have to be fulfilled for this to work right, but for your simple String-to-String example, that works.
You should read more about hash tables in general, to see how they really work behind the scenes.
EDIT (as per OP's request): you are asking about storing `Student` instances in your Hashtable. As I mentioned above, certain conditions have to be addressed for a Hashtable to work correctly. Those conditions are concerning the key part, not the value part.
If your `Student` instance is the value, and a simple String is the key, then there's nothing special for you to do, because the String primitive already answers all of the conditions required for a proper Hashtable key.
If your `Student` instance is the key, then the following conditions must be met:
Inside `Student`, you must override the `hashCode` method in such a way that subsequent invocations of `hashCode` will return exactly the same value. In other words, the expression `x.hashCode() == x.hashCode()` must always be true.
Inside `Student`, you must override the `equals` method in such a way that it will only return `true` for two identical instances of `Student`, and return `false` otherwise.
These conditions are enough for `Student` to function as a proper Hashtable key. You can further optimize things by writing a better `hashCode` implementation (read about it... it's quite long to type in here), but as long as you answer the aforementioned two, you're good to go.
Example:
class Student {
private String name;
private String address;
public int hashCode() {
// Assuming 'name' and 'address' are not null, for simplification here.
return name.hashCode() + address.hashCode();
}
public boolean equals (Object other) {
if (!(other instanceof Student) {
return false;
}
if (other == this) {
return true;
}
Student otherStudent = (Student) other;
return name.equals(otherStudent.name) && address.equals(otherStudent.address);
}
}
Problem
I am new to Java and I am trying to learn about hash tables. I want to insert objects into my hash table and then be able to print all the objects from the hash table at the end. I am not sure I am doing doing this right because I have read that I need to override the get() method or hashCode() method but I am not sure why. I am passing in String objects of student names. When I run the debugger after my inserts, it shows the key as "null" and the indexes of my inserts are at random places in the hash table. Ex. 1, 6, 10 This is how I have been adding. Can anyone tell me if this is correct and do I actually need to override things? Thanks in advance! CODE ``` Hashtable<String,String> hashTable=new Hashtable<String,String>(); hashTable.put("Donald", "Trump"); hashTable.put("Mike", "Myers"); hashTable.put ("Jimmer", "Markus"); ```