equals

Changing the elements in a set changes the 'equals' semantics

て烟熏妆下的殇ゞ 提交于 2019-11-29 08:15:47
Imagine that we have this piece of code. public class HashAddAfter { private class A { public int value; public A(int value) { this.value = value; } public void setValue(int value) { this.value = value; } // Code for hashCode()... // Code for equals()... } Set<A> list1 = new HashSet<A>(); Set<A> list2 = new HashSet<A>(); public static void main(String[] args) { HashAddAfter x = new HashAddAfter(); A e1 = x.new A(1); A e2 = x.new A(1); x.list1.add(e1); x.list2.add(e2); System.out.println(x.list1.equals(x.list2)); // true e1.setValue(4); e2.setValue(4); System.out.println(x.list1.equals(x.list2)

What is Type.GUID and how does it relate to Type.Equals()?

﹥>﹥吖頭↗ 提交于 2019-11-29 07:54:33
问题 I came across some interesting behavior while trying to compare an instance of System.RuntimeType with a generic type TOut : Type runtimeT = methodInfo.ReturnType; // get RuntimeType using reflection Type genericT = typeof(TOut); // This condition fails because runtimeT doesn't // seem to include an assembly qualified name if(runtimeT.Equals(genericT)) { ... } Here is my evidence: Disclaimer: I don't know precisely what a GUID is in the context of the CLR / type-system, except of course that

Why does '$true -eq “string”' returns $true? [duplicate]

a 夏天 提交于 2019-11-29 07:46:58
This question already has an answer here: Why is $false -eq “” true? 2 answers In powerShell you compare a boolean with a string with the "-eq" operator it will always return the same boolean as I used to compare. E.g. $shouldBeFalse = $true -eq "hello" $shouldBeTrue = $false -eq "hello" The variable $shouldBeFalse is $true. The variable $shouldBeTrue is $false. I had to use the "Equals" method: $shouldBeFalse = $true.Equals("hello") In this case $shouldBeFalse is $false. But why returns the -eq operator with boolean these kind of results? Matt PowerShell will always evaluate using the type of

Which equality test does Ruby's Hash use when comparing keys?

天涯浪子 提交于 2019-11-29 07:22:55
I have a wrapper class around some objects that I want to use as keys in a Hash. The wrapped and unwrapper objects should map to the same key. A simple example will be this: class A attr_reader :x def initialize(inner) @inner=inner end def x; @inner.x; end def ==(other) @inner.x==other.x end end a = A.new(o) #o is just any object that allows o.x b = A.new(o) h = {a=>5} p h[a] #5 p h[b] #nil, should be 5 p h[o] #nil, should be 5 I've tried ==, ===, eq? and hash all to no avail. Hash uses key.eql? to test keys for equality. If you need to use instances of your own classes as keys in a Hash, it

Windows Batch check if variable starts with, ends with and contains a specific string

送分小仙女□ 提交于 2019-11-29 06:45:07
I'm trying to check if a variable in a batch file starts with " contains BETA somewhere and ends with ") . Is it possible? And if yes, may somebody help me with that? @ECHO OFF SETLOCAL SET var=abc&CALL :check SET var="abc"&CALL :llcheck SET var="")&CALL :check SET var=")"&CALL :llcheck SET var=abc")"&CALL :llcheck SET var=xyzbetazyx&CALL :check SET var="xyzbetazyx"&CALL :llcheck SET var=xyzbetazyx")"&CALL :llcheck SET var=xyzbetazyx")"&CALL :check SET var="xyzbetazyx")&CALL :check GOTO :eof :: Lop last, then check :llcheck SET var=%var:~0,-1% :check SET result=N SET var2=%var% SET varvar=%var

Why does Json.Net call the Equals method on my objects when serializing?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 06:37:22
I just ran into an error when I was using the Newtonsoft.Json SerializeObject method. It has been asked before here , but there was no answer from the people working with Newtonsoft as to why this happens. Basically, when calling SerializeObject like this: string json = Newtonsoft.Json.JsonConvert.SerializeObject(from, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All }); I get errors in a lot of Equals methods I have overridden in my classes: public override bool Equals(object obj) { if (obj == null) return false; CapacityConfiguration cc = (CapacityConfiguration)obj; //

Java, Why it is implied that objects are equal if compareTo() returns 0?

孤者浪人 提交于 2019-11-29 05:34:00
Let's have a class Person . Person has a name and height. Equals and hashCode() takes into account only name. Person is comparable (or we implement comparator for it, does not matter which one). Persons are compared by height. It seems reasonable to expect a situation where two different persons can have same height, but eg. TreeSet behaves like comapareTo()==0 means equals, not merely same size. To avoid this, comparison can secondarily look at something else if size is the same, but then it cannot be used to detect same sized different objects. Example: import java.util.Comparator; import

Comparator and equals()

橙三吉。 提交于 2019-11-29 03:59:21
Suppose I need TreeSet with elements sorted with some domain logic. By this logic it doesn't matter order of some elements that doesn't equal so compare method can return 0, but in this case I couldn't put them in TreeSet . So, question: what disadvantages I'll have from code like this: class Foo implements Comparable<Foo>{} new TreeSet<Foo>(new Comparator<Foo>(){ @Override public int compare(Foo o1, Foo o2) { int res = o1.compareTo(o2); if(res == 0 || !o1.equals(o2)){ return o1.hashCode() - o2.hashCode(); } return res; } }); Update : Ok. If it should always be a consistency between the

How to compare two arrays in Kotlin?

半城伤御伤魂 提交于 2019-11-29 02:49:55
Given some arrays in Kotlin let a = arrayOf("first", "second") val b = arrayOf("first", "second") val c = arrayOf("1st", "2nd") Are there built-in functions to the Kotlin std-lib that tests two arrays for (value) equality for each element? Thus resulting in: a.equals(b) // true a.equals(c) // false equals() is actually returning false in both cases, but maybe there are built-in functions to Kotlin that one could use? There is the static function java.utils.Arrays.deepEquals(a.toTypedArray(), b.toTypedArray()) but I would rather prefer an instance method as it would work better with optionals.

Equals and Comparable with Sets

烈酒焚心 提交于 2019-11-29 02:01:14
I posted some code here which correctly solved a problem the poster had. OP wanted to remove duplicates and bring certain special items to the top of a list. I used a TreeSet with a special Comparable class which wrapped the Locale they were working with to achieve what they wanted. I then got to thinking ... as you do ... that I was eliminating duplicates by returning 0 from the compareTo method, not by returning true from an equals implementation as one would need to do to correctly indicate a duplicate in a Set (from the definition of a Set ). I have no objection to using this technique but