Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: question from the Core Java II book

  1. #1
    Member
    Join Date
    Jun 2013
    Posts
    61
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default question from the Core Java II book

    A hash table is an array of linked lists. Each list is called a bucket. To find the place of an object in the table, compute its hash code and reduce it modulo the total number of buckets.
    The resulting number is the index of the bucket that holds the element. For example, if an object has hash code 345 and there are 101 buckets, then the object is placed in bucket 42 (because the remainder of the integer division 345/101 is 42).

    Could please somebody elaborated why the rest of the division (in this case 42) points to the "bucket"?

    Normally one talks, concerning maps. about keys and values, right?
    So the bucket here is the key?


  2. #2
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: question from the Core Java II book

    The point of a hash table is to make looking for objects faster because they are put into buckets. I think i have a decent analogy that may help.

    Imagine you have a huge bin of baseball/football/hockey/basketball cards. If i wanted to find a Yankees card i would have to go through the whole lot till i found it, but if i put them into a "hashtable" it would be faster. Imagine my hash function divides the cards by sport. So i would have 4 buckets one for each sport. Then if i wanted to find a Yankees card i would instantly know what bucket to look in. This cuts down on search time greatly.

    Back to your example. You are able to set the initial size of a HashTable. In your example they chose 101 buckets. If i want to input a value with a hashcode of 345, it would go in bucket 42 as they said. If i added a value with a hashcode of 446/244/143 they would also go in that bucket. So later, if i came back with my 345 hash code object i would know to go to bucket 42 then progress with a linear search to find my particular object.

    Obviously the highest performance would be to have a hashtable that has one bucket per possible hash value, but that could take up a lot of space. So it is a tradeoff of performance vs space. Side-note, all of this is done in the background. All the coder cares about typically is their keys and values. Unless this hashtable is a bottleneck, people dont typically mess with the number of buckets or how full buckets can get (initial capacity and load factor as java calls them).

    God i hope that all made sense.

    Side note, in case you were wondering. What's the difference between hashtable and hashmap? Hashtable is synchronous and hashmap is not. Hashtable does not allow null keys or values, hashmap allows null values and one null key.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  3. The Following User Says Thank You to Chris.Brown.SPE For This Useful Post:

    GregBrannon (July 5th, 2013)

  4. #3
    Member
    Join Date
    Jun 2013
    Posts
    61
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: question from the Core Java II book

    Yes chris that does make sense, since collections is all about selecting and sorting things out. The other confusing part is that hash-stuff. It, at first, seems like a weird computer hocus spocus! But okay when it is up to computers to label data you indeed get these weird number codes and for them (the computers) it is just fine.

    "Obviously the highest performance would be to have a hashtable that has one bucket per possible hash value"

    That would be a hash map right? whereas each bucket would be a key?

    Thanks chris for your answer.

  5. #4
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: question from the Core Java II book

    Hash table is an idea of a data structure. HashTable and HashMap as they are known in java are both hash tables. You can pass in an initial capacity and load factor into both of them. The only differences between the two i mentioned at the end of my previous post. If you need null values or do not need it to be synchronous then use HashMap. If you need it to be synchronous use a HashTable. Remember, things being synchronous comes with overhead. If you dont need it, dont use it.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

Similar Threads

  1. Question about an exercise from D.S. Malik's Book "Java Programming"
    By Lahoree in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 15th, 2013, 01:47 AM
  2. core java
    By abinaya.cs in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 30th, 2013, 04:10 AM
  3. Core Java
    By abinaya.cs in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 28th, 2013, 07:39 AM
  4. core java begineer
    By vinod in forum Object Oriented Programming
    Replies: 3
    Last Post: November 19th, 2011, 06:20 AM
  5. Replies: 1
    Last Post: June 4th, 2011, 11:22 AM