Questions tagged [hashcode]

A hash code is a result of applying a hash function to data, usually resulting in an integer.

2025 questions
161
votes
8 answers

Apache Commons equals/hashCode builder

I'm curious to know, what people here think about using org.apache.commons.lang.builder EqualsBuilder/HashCodeBuilder for implementing the equals/hashCode? Would it be a better practice than writing your own? Does it play well with Hibernate?…
aug70co
  • 3,965
  • 5
  • 30
  • 44
147
votes
5 answers

What is hashCode used for? Is it unique?

I notice there is a getHashCode() method in every controls, items, in WP7, which return a sequence of number. Can I use this hashcode to identify an item? For example I want to identify a picture or a song in the device, and check it whereabout.…
Nghia Nguyen
  • 2,585
  • 4
  • 25
  • 38
146
votes
8 answers

Consistency of hashCode() on a Java string

The hashCode value of a Java String is computed as (String.hashCode()): s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] Are there any circumstances (say JVM version, vendor, etc.) under which the following expression will evaluate to false? boolean…
knorv
  • 49,059
  • 74
  • 210
  • 294
139
votes
9 answers

GetHashCode Guidelines in C#

I read in the Essential C# 3.0 and .NET 3.5 book that: GetHashCode()’s returns over the life of a particular object should be constant (the same value), even if the object’s data changes. In many cases, you should cache the method return to…
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
137
votes
11 answers

.NET unique object identifier

Is there a way of getting a unique identifier of an instance? GetHashCode() is the same for the two references pointing to the same instance. However, two different instances can (quite easily) get the same hash code: Hashtable hashCodesSeen = new…
Martin Konicek
  • 39,126
  • 20
  • 90
  • 98
137
votes
14 answers

How to create a HashMap with two keys (Key-Pair, Value)?

I have a 2D array of Integers. I want them to be put into a HashMap. But I want to access the elements from the HashMap based on Array Index. Something like: For A[2][5], map.get(2,5) which returns a value associated with that key. But how do I…
Crocode
  • 3,056
  • 6
  • 26
  • 31
131
votes
6 answers

How do you get the "object reference" of an object in java when toString() and hashCode() have been overridden?

I would like to print the "object reference" of an object in Java for debugging purposes. I.e. to make sure that the object is the same (or different) depending on the situation. The problem is that the class in question inherits from another class,…
Nicolai
  • 3,698
  • 3
  • 30
  • 34
131
votes
2 answers

Boolean.hashCode()

The hashCode() method of class Boolean is implemented like this: public int hashCode() { return value ? 1231 : 1237; } Why does it use 1231 and 1237? Why not something else?
user471011
  • 7,104
  • 17
  • 69
  • 97
125
votes
6 answers

How default .equals and .hashCode will work for my classes?

Say I have my own class public class MyObj { /* ... */ } It has some attributes and methods. It DOES NOT implement equals, DOES NOT implement hashCode. Once we call equals and hashCode, what are the default implementations? From Object class? And…
alexeypro
  • 3,633
  • 7
  • 36
  • 49
111
votes
8 answers

How should equals and hashcode be implemented when using JPA and Hibernate

How should model class's equals and hashcode be implemented in Hibernate? What are the common pitfalls? Is the default implementation good enough for most cases? Is there any sense to use business keys? It seems to me that it's pretty hard to get it…
egaga
  • 21,042
  • 10
  • 46
  • 60
106
votes
6 answers

Hashing with SHA1 Algorithm in C#

I want to hash given byte[] array with using SHA1 Algorithm with the use of SHA1Managed. The byte[] hash will come from unit test. Expected hash is 0d71ee4472658cd5874c5578410a9d8611fc9aef (case sensitive). How can I achieve this? public string…
Merve Kaya
  • 1,219
  • 2
  • 10
  • 16
106
votes
10 answers

Quick and Simple Hash Code Combinations

Can people recommend quick and simple ways to combine the hash codes of two objects. I am not too worried about collisions since I have a Hash Table which will handle that efficiently I just want something that generates a code quickly as…
RobV
  • 28,022
  • 11
  • 77
  • 119
97
votes
6 answers

What's a good recipe for overriding hashcode in Dart?

I find myself wanting to override hashcode and == for an object, and I'm wondering if there are best practices for how to implement a hashcode that depends on multiple attributes, and it seems like there are some Dart-specific considerations. The…
Alan Knight
  • 2,759
  • 1
  • 15
  • 13
86
votes
12 answers

What is an object's hash code if hashCode() is not overridden?

If the hashCode() method is not overridden, what will be the result of invoking hashCode() on any object in Java?
java_geek
  • 17,585
  • 30
  • 91
  • 113
80
votes
7 answers

HashMaps and Null values?

How do you pass in null values into a HashMap? The following code snippet works with options filled in: HashMap options = new HashMap(); options.put("name", "value"); Person person = sample.searchPerson(options); …
Neutron_boy
  • 919
  • 1
  • 6
  • 7