equals

How to compare two arrays in Kotlin?

ぃ、小莉子 提交于 2019-11-27 17:06:41
问题 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(),

Equals and Comparable with Sets

和自甴很熟 提交于 2019-11-27 16:23:32
问题 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

Demonstrating string comparison with Java

99封情书 提交于 2019-11-27 16:08:05
I want to demonstrate with a few line of code that in Java, that to compare two strings ( String ), you have to use equals() instead of the operator == . Here is something I tried : public static void main(String Args[]) { String s1 = "Hello"; String s2 = "Hello"; if (s1 == s2) System.out.println("same strings"); else System.out.println("different strings"); } I was expecting this output : different strings , because with the test s1 == s2 I'm actually comparing two references (i.e. addresses) instead of the objet's content. But I actually got this output : same strings ! Browsing the internet

Is relational comparison between int and float directly possible in C?

你离开我真会死。 提交于 2019-11-27 15:10:52
I am using Visual Studio 6 with some old time code written in c. I found an issue where the code looks like this.. int x = 3; float y = 3.0; if(x == y){ do some crazy stuff } is this a valid comparison? is it possible at run time the allocation for the float is 3.0000001 and this would fail? Well, I guess you won't be too surpised to hear that comparing floats for equality is a rookie mistake then. The problem is that many increments smaller than an integer values can't actually be represented exactly in IEEE floating point. So if you arrive at the float by trying to "index" it up to the value

Java, how to compare Strings with String Arrays

a 夏天 提交于 2019-11-27 14:53:33
问题 I have been searching here for some time but haven't been able to find the answer to it. I am basically required to use an array for this assignment from college. And then I am supposed to check that the input (which is also a String) matches whatever's stored within the String array. I know one can easily compare Strings by using the .equals() method. However, the same method is not working with the String array. I created the following example of code for the purpose of StackOverflow so you

JUnit assertEquals( ) fails for two objects

送分小仙女□ 提交于 2019-11-27 14:30:49
问题 I created a class and overridden the equals() method. When I use assertTrue(obj1.equals(obj2)) , it will pass the test; however, assertEquals(obj1, obj2) will fail the test. Could someone please tell the reason why? 回答1: My guess is that you haven't actually overridden equals - that you've overloaded it instead. Use the @Override annotation to find this sort of thing out at compile time. In other words, I suspect you've got: public boolean equals(MyClass other) where you should have:

java why should equals method input parameter be Object

喜你入骨 提交于 2019-11-27 14:06:19
I'm going through a book on data structures. Currently I'm on graphs, and the below code is for the vertex part of the graph. class Vertex<E>{ //bunch of methods public boolean equals(Object o){ //some code } } When I try to implement this equals method my compiler complains about not checking the type of the parameter and just allowing any object to be sent it. It also does seem a bit strange to me why that parameter shouldn't be a Vertex instead of an Object. Is there a reason why the author does this or is this some mistake or antiquated example? @Override public boolean equals(Object obj)

Java Set collection - override equals method

倖福魔咒の 提交于 2019-11-27 13:41:59
问题 Is there any way to override the the equals method used by a Set datatype? I wrote a custom equals method for a class called Fee . Now I have a LnkedList of Fee and I want to ensure that there are no duplicated entries. Thus I am considering using a Set insted of a LinkedList , but the criteria for deciding if two fees are equal resides in the overriden equals method in the Fee class. If using a LinkedList , I will have to iterate over every list item and call the overriden equals method in

Deep reflective compare equals

匆匆过客 提交于 2019-11-27 12:28:42
问题 I am trying to validate serialize and de-serialize routines by comparing the resulting object with the original object. The routines can serialize arbitrary and deeply nested classes and consequently I want a comparison routine which can be given the original and final instance and reflectively walk through each value type and compare the values and iteratively dive into reference types to compare values. I have tried the Apache Commons Lang EqualsBuilder.reflectionEquals(inst1, inst2) but

Java: clean way to automatically throw UnsupportedOperationException when calling hashCode() and equals()?

耗尽温柔 提交于 2019-11-27 12:28:33
问题 We've got an OO codebase where in quite a lot of cases hashcode() and equals() simply don't work, mostly for the following reason: There is no way to extend an instantiable class and add a value component while preserving the equals contract, unless you are willing to forgo the benefits of object-oriented abstraction. That's a quote from "Effective Java" by Joshua Bloch and there's more on that subject in a great Artima article here: http://www.artima.com/lejava/articles/equality.html And we