hashcode

How to distinguish MethodBase in generics

筅森魡賤 提交于 2019-12-20 03:19:11
问题 I have a cache based on Dictionary<MethodBase, string> The key is rendered from MethodBase.GetCurrentMethod. Everything worked fine until methods were explicitly declared. But one day it is appeared that: Method1<T>(string value) Makes same entry in Dictionary when T gets absolutely different types. So my question is about better way to cache value for generic methods. (Of course I can provide wrapper that provides GetCache and equality encountered generic types, but this way doesn't look

Generating equals / hashcode / toString using annotation

断了今生、忘了曾经 提交于 2019-12-19 16:28:27
问题 I believe I read somewhere people generating equals / hashcode / toString methods during compile time (using APT) by identifying which fields should be part of the hash / equality test. I couldn't find anything like that on the web (I might have dreamed it ?) ... That could be done like that : public class Person { @Id @GeneratedValue private Integer id; @Identity private String firstName, lastName; @Identity private Date dateOfBirth; //... } For an entity (so we want to exlude some fields,

What is an appropriate `GetHashCode()` algorithm for a 2D point struct (avoiding clashes)

纵然是瞬间 提交于 2019-12-19 14:59:42
问题 Consider the following code: struct Vec2 : IEquatable<Vec2> { double X,Y; public bool Equals(Vec2 other) { return X.Equals(other.X) && Y.Equals(other.Y); } public override bool Equals(object obj) { if (obj is Vec2) { return Equals((Vec2)obj); } return false; } // this will return the same value when X, Y are swapped public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode(); } } Beyond the conversation of comparing doubles for equality (this is just demo code), what I am

What is an appropriate `GetHashCode()` algorithm for a 2D point struct (avoiding clashes)

二次信任 提交于 2019-12-19 14:59:12
问题 Consider the following code: struct Vec2 : IEquatable<Vec2> { double X,Y; public bool Equals(Vec2 other) { return X.Equals(other.X) && Y.Equals(other.Y); } public override bool Equals(object obj) { if (obj is Vec2) { return Equals((Vec2)obj); } return false; } // this will return the same value when X, Y are swapped public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode(); } } Beyond the conversation of comparing doubles for equality (this is just demo code), what I am

Hash code in Dictionary<TKey, TValue>

ε祈祈猫儿з 提交于 2019-12-19 10:21:16
问题 I was playing around with Dictionary and stumbled across the below scenario public class MyObject { public string I { get; set; } public string J { get; set; } public string K { get; set; } public override int GetHashCode() { int hashCode = (I+J+K).GetHashCode(); Debugger.Log(9, "INFO", hashCode.ToString() + System.Environment.NewLine); return hashCode; } } class Program { static void Main(string[] args) { MyObject obj1 = new MyObject() { I = "Hello", J = "World" }; MyObject obj2 = new

Do I need to implement hashCode() and equals() methods?

情到浓时终转凉″ 提交于 2019-12-19 09:56:09
问题 If I have a map and an object as map key, are the default hash and equals methods enough? class EventInfo{ private String name; private Map<String, Integer> info } Then I want to create a map: Map<EventInfo, String> map = new HashMap<EventInfo, String>(); Do I have to explicitly implement hashCode() and equals()? Thanks. 回答1: Yes, you do. HashMap s work by computing the hash code of the key and using that as a base point. If the hashCode function isn't overriden (by you), then it will use the

Why does HashSet allow equal items if hashcodes are different?

不打扰是莪最后的温柔 提交于 2019-12-19 07:35:35
问题 The HashSet class has an add(Object o) method, which is not inherited from another class. The Javadoc for that method says the following: Adds the specified element to this set if it is not already present. More formally, adds the specified element e to this set if this set contains no element e2 such that (e==null ? e2==null : e.equals(e2)) . If this set already contains the element, the call leaves the set unchanged and returns false . In other words, if two objects are equal, then the

hashCode, implementation, and relation to HashMap

白昼怎懂夜的黑 提交于 2019-12-19 06:18:12
问题 So I asked another related question here: java string hash function with avalanche effect, but I have a different, related question now. What I established in that question was that the hashCode() function for String does not have an avalanche effect. This means, for example, that if I have strings "k1", "k2", "k3", and I call hashCode() on each, the values returned will be contiguous. Now, based on my recollection of data structures 101, I was under the impression that this is a bad thing.

Java hashCode from one field

空扰寡人 提交于 2019-12-19 02:49:08
问题 Edit: Prepare my objects for the use within a HashMap. after reading a bit about how to generate a hash code, im kind of confused now. My (probably trivial) question is, how should I implement a hashCode method when I have one field that I could use? Can I use the fiels directly? If I understand correctly, the values for hashCode must not change during the lifetime of an object, and I only have an ID filed that fits this, but I have read otherwhere, that one should not use ID...despide of

Is there a way to get a hashcode of a float with epsilon?

点点圈 提交于 2019-12-18 19:10:12
问题 It is well known that comparing floats by == is usually a mistake. In a 3D-vector class (with float components X, Y, Z) i wrote, two vectors are considered equal if their distance is considered zero. public override bool Equals(object obj) { if (obj == null) { return false; } if (GetType () != obj.GetType ()) { return false; } float d = DistSq ((Vec) obj); return IsConsideredZero (d); } public float DistSq(Vec p) { Vec d = this - p; return d.LengthSq (); } public float LengthSq() { return X *