Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hash table Java insert

Tags:

java

hashtable

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");
like image 692
Alex G Avatar asked Dec 08 '12 01:12

Alex G


People also ask

How do you add to a Hashtable in Java?

put() method of Hashtable is used to insert a mapping into a table. This means we can insert a specific key and the value it is mapping to into a particular table. If an existing key is passed then the previous value gets replaced by the new value. If a new pair is passed, then the pair gets inserted as a whole.

How do you add an element to a hash table?

Add(Object, Object) Method is used to adds an element with the specified key and value into the Hashtable. Syntax: public virtual void Add(object key, object value);

Does Java have a built in hash table?

Java has two hash table classes: HashTable and HashMap. In general, you should use a HashMap. While both classes use keys to look up values, there are some important differences, including: A HashTable doesn't allow null keys or values; a HashMap does.

What is a hash table in Java?

A Hashtable is an array of a list. Each list is known as a bucket. The position of the bucket is identified by calling the hashcode() method. A Hashtable contains values based on the key. Java Hashtable class contains unique elements.


1 Answers

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:

  1. 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.

  2. 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);
    }
}
like image 199
Isaac Avatar answered Sep 20 '22 23:09

Isaac