hashcode

is it incorrect to define an hashcode of an object as the sum, multiplication, whatever, of all class variables hashcodes?

Let's say I have the following class: class ABC { private int myInt = 1; private double myDouble = 2; private String myString = "123"; private SomeRandomClass1 myRandomClass1 = new ... private SomeRandomClass2 myRandomClass2 = new ... //pseudo code public int myHashCode() { return 37 * ...

System.identityHashCode() and compacting garbage collectors

Possible Duplicate: How does the JVM ensure that System.identityHashCode() will never change? I was just wondering how System.identityHashCode() is able to always yield the same result for a given object, even if the garbage collector moves it around in memory. Does every object have an additional invisible field storing its i...

How does Java order items in a HashMap or a HashTable?

I was wondering how Java orders items in the Map (HashMap or Hashtable) when they are added. Are the keys ordered by the hashcode, memory reference or by allocation precedence...? It's because I've noticed same pairs in the Map are not always in the same order ...

Effective java hashcode implementation

I was wondering if someone could explain in detail what (int)(l ^ (l >>> 32)); does in the following hashcode implementation (generated by eclipse, but the same as Effective Java): private int i; private char c; private boolean b; private short s; private long l; private double d; private float f; @Override public int hashCode()...

Can I use a ShareThis/AddThis Button if my site's navigation relies on Hash tags in the url?

I want to embed a ShareThis/AddThis button on my site but the site's navigation relies on Hash tags in the url. Each page is assigned a unique hash value (ie, http://domain.com/index.php#products). Changing the navigation/page design isn't an option. Will these service preserve the hash value (both embed JS on the page)? If they don't, w...

why can't I call .update on a MessageDigest instance

when i run this from the repl: (def md (MessageDigest/getInstance "SHA-1")) (. md update (into-array [(byte 1) (byte 2) (byte 3)])) I get: No matching method found: update for class java.security.MessageDigest$Delegate the Java 6 docs for MessageDigest show: update(byte[] input) Updates the digest using the specifie...

Collection, which method is used to authorize an add of an element ?

We find a lot of concrete subclasses under Collection. While trying to add an element in a concrete collection, this collection will use a method to determine if it can accept to store the element (and eventually that this element is not already in the collection). It could use equals(), hashCode() or compareTo() of the element. Is it ...

Why does C# not implement GetHashCode for Collections?

I am porting something from Java to C#. In Java the hashcode of a ArrayList depends on the items in it. In C# I always get the same hashcode from a List... Why is this? For some of my objects the hashcode needs to be different because the objects in their list property make the objects non-equal. I would expect that a hashcode is alwa...

Python: What's a correct and good way to implement __hash__()?

What's a correct and good way to implement __hash__()? I am talking about the function that returns a hashcode that is then used to insert objects into hashtables aka dictionaries. As __hash__() returns an integer and is used for "binning" objects into hashtables I assume that the values of the returned integer should be uniformly dist...

why we need to override equals and hashcode in java and why cannot we use Object class implementation

guys please let me know, in real world why we need to override equals and hashcode and cant we use Object's equals and hashcode. ...

How to "reduce" a hash?

Suppose I have any "long" hash, like a 16 bytes MD5 or a 20 bytes SHA1. I want to reduce this hash to fit on 4 bytes, for GetHashCode() purposes. First, I'm perfectly aware that I'll get more collisions. That's totally fine in my case, but I'd still prefer to get the less possible collisions. There are several solutions to my problem: ...

construct a unique number for a string in java

We have a requirement of reading/writing more than 10 million strings into a file. Also we do not want duplicates in the file. Since the strings would be flushed to a file as soon as they are read we are not maintaining it in memory. We cannot use hashcode because of collisions in the hash code due to which we might miss a string as dup...

Why might a System.String object not cache its hash code?

A glance at the source code for string.GetHashCode using Reflector reveals the following (for mscorlib.dll version 4.0): public override unsafe int GetHashCode() { fixed (char* str = ((char*) this)) { char* chPtr = str; int num = 0x15051505; int num2 = num; int* numPtr = (int*) chPtr; for ...

Are the two codes equivalent to each other?

i know ive asked similar questions like this before, but: Is this pseudocode here the same as my code? upper case variables are the variables in the pseudocode with " ' " and the values with conditions are all in lists, such as: all "s" conditions are in list "s", and " s' " conditions in list "S" for i in xrange(t): a = h0; b = h1;...

Java profiling: java.lang.Object.hashCode takes half of the CPU time but never explictly called

I have been benchmarked my multihreaded program using -agentlib:hprof=cpu=samples and was surprised to find the following line in the results: rank self accum count trace method 1 52.88% 52.88% 8486 300050 java.lang.Object.hashCode I never explicitly call hashCode() in my program. What can be the reason for this? How can I ...

HashCode. How to use it.

Hi, Although I understand very well what HashCode is and what a Hash Table does, I have to admit I don´t know how to use it (Beyond a common dictionary). I wanted to implement my own Hash Table so first I want to know the very basic about Hash: I know I can get the hash code with getHashCode()/hashCode() in Java and Scala. How is this...

How to implement equals() and hashcode() methods in BaseEntity of JPA?

I have BaseEntity class which is a superclass of all JPA entities in my application. @MappedSuperclass public abstract class BaseEntity implements Serializable { private static final long serialVersionUID = -3307436748176180347L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "ID", nullable=false...

Hibernate: Strategy/pattern for object and entity identity mapping using composite keys?

What is a general collision-free Java best practice to generate hash codes for any-type (atomic types) multi-column primary keys? I thought about it for a few hours and came to the conclusion, that a string concatenated by all primary key columns would be the only reliable way to do so. Then calling Java's hashCode method on that concat...

What happens to identity-hashcode when there are more objects than `int` can hold?

The method System.identityHashCode(...) is called that way, because it identifies objects, so two distinct objects can't have same identity-hashcode, right? It returns an int. But what happens, on a system with huge amount of RAM, when the number of objects exceeds the integer range 2^32? Wouldn't it be a problem for HashMaps and HashS...

HMAC security - Is the security of the HMAC based on SHA-1 affected by the collisions attacks on SHA-1?

Is the security of the HMAC based on SHA-1 affected by the collisions attacks on SHA-1? ...