equals

Using '==' instead of .equals for Java strings [duplicate]

风流意气都作罢 提交于 2019-12-17 07:37:41
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: What makes reference comparison (==) work for some strings in Java? I know this has been asked before, but in spite of recommendations to use .equals() instead of the == comparison operator, I found that == works all the time: String s1 = "Hello"; String s2 = "Hello"; System.out.println(s1 == s2); // true Can anyone give me an example of the == operator failing? 回答1: This is because you're lucky. The == operator

Why does `Array(0,1,2) == Array(0,1,2)` not return the expected result?

梦想的初衷 提交于 2019-12-17 07:24:40
问题 As far as I understand, Scala's == defines the natural equality of two objects. I expected that Array(0,1,2) == Array(0,1,2) compares the natural equality. For example, checks if all elements of the array return true when compared with the corresponding elements of the other array. People told me that Scala's Array is just a Java [] which only compares identity. Wouldn't it be more meaningful to override Array 's equals method to compare natural equality instead? 回答1: Scala 2.7 tried to add

what is the difference between == operator and equals()? (with hashcode() ???)

爱⌒轻易说出口 提交于 2019-12-17 06:17:07
问题 I was learning hashcode in more depth and figured that: 1. If you override equals(), you must override hashcode() too. 2. To find if 2 objects are same object, use == operator Given those 2 factors, in Java I was assuming that when == operator is used to compare if 2 instances are same or not, if(object1 == object2) is actually doing if(object1.hashcode() == object2.hashcode()) But it appears I was wrong by running the test below. public class Main { public static void main(String[] args){

String.Equals() not working as intended

寵の児 提交于 2019-12-17 06:13:05
问题 I am using LINQ to search through one of my Entity Framework tables and find a "group" based on the name. The name is a string and appears to be Unicode (says it is in the edmx). I have a method GetGroup() and I pass in a name to search for. Debugging through the code, I already have a group named "Test" in my database. Once I pass in a group named "TEST" I expect it to return the "Test" which was already in the database. It for some reason, does not find the "Test" and thinks "TEST" doesn't

String.Equals() not working as intended

杀马特。学长 韩版系。学妹 提交于 2019-12-17 06:12:28
问题 I am using LINQ to search through one of my Entity Framework tables and find a "group" based on the name. The name is a string and appears to be Unicode (says it is in the edmx). I have a method GetGroup() and I pass in a name to search for. Debugging through the code, I already have a group named "Test" in my database. Once I pass in a group named "TEST" I expect it to return the "Test" which was already in the database. It for some reason, does not find the "Test" and thinks "TEST" doesn't

Why should I override hashCode() when I override equals() method?

情到浓时终转凉″ 提交于 2019-12-17 04:30:46
问题 Ok, I have heard from many places and sources that whenever I override the equals() method, I need to override the hashCode() method as well. But consider the following piece of code package test; public class MyCustomObject { int intVal1; int intVal2; public MyCustomObject(int val1, int val2){ intVal1 = val1; intVal2 = val2; } public boolean equals(Object obj){ return (((MyCustomObject)obj).intVal1 == this.intVal1) && (((MyCustomObject)obj).intVal2 == this.intVal2); } public static void main

Getting an element from a Set

扶醉桌前 提交于 2019-12-17 04:12:12
问题 Why doesn't Set provide an operation to get an element that equals another element? Set<Foo> set = ...; ... Foo foo = new Foo(1, 2, 3); Foo bar = set.get(foo); // get the Foo element from the Set that equals foo I can ask whether the Set contains an element equal to bar , so why can't I get that element? :( To clarify, the equals method is overridden, but it only checks one of the fields, not all. So two Foo objects that are considered equal can actually have different values, that's why I

compareTo() vs. equals()

戏子无情 提交于 2019-12-17 03:50:50
问题 When testing for equality of String 's in Java I have always used equals() because to me this seems to be the most natural method for it. After all, its name already says what it is intended to do. However, a colleague of mine recently told me had been taught to use compareTo() == 0 instead of equals() . This feels unnatural (as compareTo() is meant to provide an ordering and not compare for equality) and even somewhat dangerous (because compareTo() == 0 does not necessarily imply equality in

compareTo() vs. equals()

元气小坏坏 提交于 2019-12-17 03:50:48
问题 When testing for equality of String 's in Java I have always used equals() because to me this seems to be the most natural method for it. After all, its name already says what it is intended to do. However, a colleague of mine recently told me had been taught to use compareTo() == 0 instead of equals() . This feels unnatural (as compareTo() is meant to provide an ordering and not compare for equality) and even somewhat dangerous (because compareTo() == 0 does not necessarily imply equality in

Understanding the workings of equals and hashCode in a HashMap

霸气de小男生 提交于 2019-12-16 22:44:26
问题 I have this test code: import java.util.*; class MapEQ { public static void main(String[] args) { Map<ToDos, String> m = new HashMap<ToDos, String>(); ToDos t1 = new ToDos("Monday"); ToDos t2 = new ToDos("Monday"); ToDos t3 = new ToDos("Tuesday"); m.put(t1, "doLaundry"); m.put(t2, "payBills"); m.put(t3, "cleanAttic"); System.out.println(m.size()); } } class ToDos{ String day; ToDos(String d) { day = d; } public boolean equals(Object o) { return ((ToDos)o).day == this.day; } // public int