10 Replies - 688 Views - Last Post: 13 January 2013 - 04:21 PM Rate Topic: -----

#1 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 174
  • Joined: 05-May 12

Hashcode

Posted 10 January 2013 - 10:54 AM

key: Hashcode Algorithm Hashcode

Alex A(1)+L(12)+E(5)+X(24) = 42

Dirk D(4)+I(9)+R(18)+K(11) = 42

This is an example from my text book. Both names with be found the the same bucket as their Hashcodes are the same, so to find the name you looking for you need to do an equality test.
They gave us an example of how to find the name you want using the equality test but i don't understand how it finds the name you want exactly.
public class HasHash {
	public static void main(String[] args) {
		}
		
		public int x;
		private int xValue;
		HasHash(int xVal) {x = xVal; }

		class x {
			private int xValue;
			x (int val) {
				xValue = val;
			}
			public int getxValue() {
				return xValue;
			}
		}
		
		public boolean equals(Object o) {
			HasHash h = (HasHash) o;
			if (h.x == this.x){ 
				return true;
			}else{
				return false;
			}
			}
		
		public int hashCode() { return (x * 17); }
	}



Any help on this would be great!
Thanks :)

Is This A Good Question/Topic? 0
  • +

Replies To: Hashcode

#2 IceHot  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 213
  • Joined: 28-August 12

Re: Hashcode

Posted 10 January 2013 - 11:16 AM

Try filling each part with comments, and then analyze those comments

This post has been edited by IceHot: 10 January 2013 - 11:16 AM

Was This Post Helpful? 2
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: Hashcode

Posted 10 January 2013 - 02:31 PM

Having an inner class and instance field with the same name is not good practice. Use descriptive and distinct variable names so your code doesn't have ambiguities.

The hashing algorithm in the code is pretty simple- 17 * x. There is only one value of x for which two objects will be equal. I'm not really sure what was so difficult to understand about that.
Was This Post Helpful? 3
  • +
  • -

#4 IceHot  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 213
  • Joined: 28-August 12

Re: Hashcode

Posted 10 January 2013 - 03:01 PM

It is also a good programming habit to use proper indentation and use
//end whatever_structure
at the end of those control structures. That would make the code more readable and thus easier to interpret. (My CS teacher, Andy Harris, hit us HARD with this in first-semester computing...)
For example instead of
function myfunction() {
	if (something) {
		//some statements
	}else{
		//do some other statements
	}
	}


you should make it look something like:
function myfunction()
{
	if (something) 
	{
		//some statements
	}	//end if
	else
	{
		//do some other statements
	}	//end else
}	//end myfunction


This post has been edited by IceHot: 10 January 2013 - 03:05 PM

Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: Hashcode

Posted 10 January 2013 - 05:50 PM

View PostIceHot, on 10 January 2013 - 11:01 PM, said:

It is also a good programming habit to use proper indentation


Definitely.

Quote

and use
//end whatever_structure
at the end of those control structures.


That's a matter of opinion.
Was This Post Helpful? 2
  • +
  • -

#6 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Hashcode

Posted 10 January 2013 - 05:53 PM

I really don't see any need in having a comment to signal the end of a loop, method etc etc. It's just a comment for comments sake. If your structure is long enough to warrant a comment telling you where the end is, chances are you need to refactor it anyways.
Was This Post Helpful? 2
  • +
  • -

#7 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2021
  • View blog
  • Posts: 4,195
  • Joined: 11-December 07

Re: Hashcode

Posted 11 January 2013 - 05:49 AM

View Postsepp2k, on 11 January 2013 - 12:50 AM, said:

View PostIceHot, on 10 January 2013 - 11:01 PM, said:

It is also a good programming habit to use proper indentation


Definitely.


Definitely.

View Postsepp2k, on 11 January 2013 - 12:50 AM, said:

Quote

and use
//end whatever_structure
at the end of those control structures.


That's a matter of opinion.


Not at all. It's 100% awful code. It's a crutch used by people who don't indent their code or who nest heavily instead of writing smaller methods. It's one thing for a programming-101 teacher to use it in written examples while they are braces but forcing students to emulate the clutter is doing them a disservice.

There are a lot of ideas about good and bad programming style. Whenever someone tells you a best practice, question it. Does it make intuitive sense to you? What is so bad about the alternative? What was the motive for inventing that best practice? A controversial one is the "single exit point for each function". People stick to it religiously but they don't consider the best practice was invented in a time where people wrote looong methods - several screen worths. Multiple exit points were just too difficult to keep track of. These days, in a 10 line method, who cares if you have 2 returns.

One reason this kind of comment is harmful is because it lies. Not when you write it but eventually it will. What if you modify your code to work with a collection and your if becomes a for? What if your for changes to a while? What if you cut and paste code, get it wrong and have to even up the braces? You might mean to edit your comments in all these instances but the compiler won't warn you the 1 in 50 times you forget. The comment will simply stay there and lie to anyone who reads it.

It's worse than:

// gets x
public int getX() {
  return x;
}

// end quote

This post has been edited by cfoley: 11 January 2013 - 05:53 AM
Reason for edit:: Changed my quote to a code block

Was This Post Helpful? 2
  • +
  • -

#8 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 174
  • Joined: 05-May 12

Re: Hashcode

Posted 13 January 2013 - 02:48 PM

When talking about hashCode( ), two objects with x value of 8 will have the same hashcode, but then again so will two unequal objects, one with an x value of 12 and the other of a value of -920 so they will both land in the same bucket.

How do you stop that from happening?

Could some please give me an example to help me understand?

Thanks! :)
Was This Post Helpful? 0
  • +
  • -

#9 FallenG  Icon User is offline

  • New D.I.C Head

Reputation: 22
  • View blog
  • Posts: 44
  • Joined: 12-January 13

Re: Hashcode

Posted 13 January 2013 - 03:02 PM

You keep talking about buckets, but haven't mentioned a HashSet or any other structure which uses buckets. Can you give us a full description of your scenario, as I feel you are leaving out details.

Your description seems to be talking about HashSets, but then I don't really see how your code relates to it at all...? Perhaps I am missing something.
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: Hashcode

Posted 13 January 2013 - 03:53 PM

You stop this by overriding the hashCode() method and having it return a different value based on the attributes you are comparing. So if you are comparing two Accounts based on their balances, the hashCode() method would need to return the same values these two Accounts when their balances are the same. You could look up a number of mathematical hash functions to accomplish this, or you could write your own.
Was This Post Helpful? 0
  • +
  • -

#11 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: Hashcode

Posted 13 January 2013 - 04:21 PM

View Postmacosxnerd101, on 13 January 2013 - 11:53 PM, said:

You stop this by overriding the hashCode() method and having it return a different value based on the attributes you are comparing.


Since the number of buckets will almost certainly be smaller than Integer.MAX_VALUE, you'll have to take into account that the hash value will be taken modulo the number of buckets. So even if you do have a perfect hashing function (which you generally don't and for many types is impossible), you'll still get collisions.

So no, in most contexts you won't be able to avoid hash collisions. You'll have to handle them somehow (either by storing multiple values in the same bucket using a list or by using open addressing).
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1