# Hashcode

Page 1 of 1

## 10 Replies - 446 Views - Last Post: 13 January 2013 - 04:21 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=306552&amp;s=657c146a70c1ad07955d5efa3d110148&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 BrendanH

Reputation: -2
• Posts: 154
• 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

Reputation: 2
• Posts: 108
• Joined: 28-August 12

## Re: Hashcode

Posted 10 January 2013 - 11:16 AM

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

### #3 macosxnerd101

• Self-Trained Economist

Reputation: 9026
• Posts: 33,479
• 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.

### #4 IceHot

Reputation: 2
• Posts: 108
• 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...)
```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

### #5 sepp2k

• D.I.C Lover

Reputation: 1688
• Posts: 2,553
• Joined: 21-June 11

## Re: Hashcode

Posted 10 January 2013 - 05:50 PM

IceHot, 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.

### #6 Ryano121

• D.I.C Lover

Reputation: 1051
• Posts: 2,229
• 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.

### #7 cfoley

• Cabbage

Reputation: 1500
• Posts: 3,211
• Joined: 11-December 07

## Re: Hashcode

Posted 11 January 2013 - 05:49 AM

sepp2k, on 11 January 2013 - 12:50 AM, said:

IceHot, on 10 January 2013 - 11:01 PM, said:

It is also a good programming habit to use proper indentation

Definitely.

Definitely.

sepp2k, 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

### #8 BrendanH

Reputation: -2
• Posts: 154
• 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!

### #9 FallenG

Reputation: 22
• 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.

### #10 macosxnerd101

• Self-Trained Economist

Reputation: 9026
• Posts: 33,479
• 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.

### #11 sepp2k

• D.I.C Lover

Reputation: 1688
• Posts: 2,553
• Joined: 21-June 11

## Re: Hashcode

Posted 13 January 2013 - 04:21 PM

macosxnerd101, 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).