equals

ArrayList - add “same” objects (same => equals, hashCode), Threads

不打扰是莪最后的温柔 提交于 2019-12-10 03:06:14
问题 Ive got one question. What happens when I try to add the "same" object twice to an ArrayList. With "the same" I mean an object of an individual class, which is identified as the same with equals() and hashCode(). It has different values for most of the member variables and was created from maybe different threads, but for equals() and hashCode() its the "same". Does the second object then replace the first object? Also, what happens if two threads try to add those objects exactly at the same

How does compareTo work?

不羁的心 提交于 2019-12-10 02:25:17
问题 I know that compareTo returns a negative or positive result on how well one string correlates to the other, but then why: public class Test { public static void main(String[] args) { String y = "ab2"; if(y.compareTo("ac3") == -1) { System.out.println("Test"); } } } is true and public class Test { public static void main(String[] args) { String y = "ab2"; if(y.compareTo("ab3") == -1) { System.out.println("Test"); } } } is also true? 回答1: The general contract of Comparable.compareTo(o) is to

Different fields for equals and hashcode

蹲街弑〆低调 提交于 2019-12-09 20:46:02
问题 I agree with the statement from this post What issues should be considered when overriding equals and hashCode in Java? Use the same set of fields that you use to compute equals() to compute hashCode(). But i've some doubts : Is this absolutely necessary to have same fields ? If yes, what if I don't use same field ? Will it affect HashMap performance or HashMap Accuracy ? 回答1: The fields don't have to be the same. The requirement is for two objects that are equal, they must have the same hash

find all occurrences of comparison with == in visual studio

笑着哭i 提交于 2019-12-09 13:36:37
问题 I made the mistake of using == for comparing IP addresses instead of using the equals() method of the IPAddress class in C#, which will result in the comparison of references instead of values. Since the solution I am currently working on is very large for a one-man project (> 100.000 lines of source code), I am very sure that I still have some of these wrong statements in my code. Is there any possibility to tell Visual Studio to find all occurrences of == operations on a specific class for

Example of ==, equals and hashcode in java

落爺英雄遲暮 提交于 2019-12-09 13:04:50
问题 Given this: String s1= new String("abc"); String s2= new String("abc"); String s3 ="abc"; System.out.println(s1==s3); System.out.println(s1==s2); System.out.println(s1.equals(s2)); System.out.println(s1.equals(s3)); System.out.println(s1.hashCode()); System.out.println(s2.hashCode()); System.out.println(s3.hashCode()); Output is: false false true true 96354 96354 96354 Here == is giving false for each object but the hashcode for each String object is same. Why is it so? 回答1: == does compare

Compare two Java Collections using Comparator instead of equals()

人盡茶涼 提交于 2019-12-09 08:30:42
问题 Problem Statement I have two Collections of the same type of object that I want to compare. In this case, I want to compare them based on an attribute that does not factor into equals() for the Objects. In my example, I'm using ranked collections of Names for instance: public class Name { private String name; private int weightedRank; //getters & setters @Override public boolean equals(Object obj) { return this.name.equals(obj.name); //Naive implementation just to show //equals is based on

What is the difference between using IEqualityComparer and Equals/GethashCode Override?

爱⌒轻易说出口 提交于 2019-12-09 08:25:35
问题 When i am using dictionaries sometimes I have to change the default Equals meaning in order to compare Keys. I see that if I override the Equals and GetHashCode on the key's class or i create a new class which implements IEqualityComparer I have the same result. So what's the difference between using IEqualityComparer and Equals/GethashCode Override? Two Examples: class Customer { public string name; public int age; public Customer(string n, int a) { this.age = a; this.name = n; } public

“Not equal” sign in Visual Prolog?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-09 07:43:25
问题 I can't find any documentation on "not equal" sign in Visual Prolog. Please provide the right solution of this problem: class predicates sister : (string Person, string Sister) nondeterm(o,o). clauses sister(Person, Sister) :- Person [not-equal-sign] Sister, parent(Person, Parent), parent(Sister, Parent), woman(Sister). 回答1: I don't know what do you mean by "not equal" (does not unify?), but you could try these: X \= Y not(X = Y) \+ (X = Y) 回答2: Documentation for the second variant pointed

Comparing doubles in Java gives odd results

断了今生、忘了曾经 提交于 2019-12-09 04:44:44
问题 I really can'get my head around why the following happens: Double d = 0.0; System.out.println(d == 0); // is true System.out.println(d.equals(0)); // is false ?! This however works as expected: Double d = 0.0; System.out.println(d == 0.0); // true System.out.println(d.equals(0.0)); // true I'm positive that this is related to autoboxing in some way, but I really don't know why 0 would be boxed differently when the == operator is used and when .equals is called . Doesn't this implicitly

Using for loop to get the Hamming distance between 2 strings

强颜欢笑 提交于 2019-12-08 21:28:37
问题 In this task i need to get the Hamming distance (the Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different - from Wikipedia) between the two strings sequence1 and sequence2. First i made 2 new strings which is the 2 original strings but both with lowered case to make comparing easier. Then i resorted to using the for loop and if to compare the 2 strings. For any differences in characters in these 2 pair of string, the