Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8949031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:08:39+00:00 2026-06-15T13:08:39+00:00

I am new to Java and I am trying to learn about hash tables.

  • 0

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");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:08:40+00:00Added an answer on June 15, 2026 at 1:08 pm

    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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to java and I am trying to learn about optimizing code
I am new to Java and trying to learn more about it. I studied
I am trying to learn a java-based program, but I am pretty new to
Right now i am trying to learn more about java threading, and i have
I'm trying to learn about java's event handlers and keep getting errors with type
I am trying to learn about instanceof operator in java as per the link
I am trying to learn some more about vectors and rotation in Java by
I am new to Java and trying to learn the various collections that programmers
I am new to android/java and currently am trying to learn custom events and
I am trying to learn more about MySQL and using Java (on Android) to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.