equals

Objective-C constants: NSString comparison using ==?

▼魔方 西西 提交于 2019-12-01 06:44:38
The discussions I found about setting NSString constants made me code it the following way: .h file: extern NSString * const kSectionHeaders; .m file: NSString * const kSectionHeaders = @"header"; As the program runs, it has to test words from a text file against a series of NSString constants. I read memory comparison should work when setting function like stated above: if (property == kSectionHeaders) {...} Doesn't work tough :( The following works, but it was described as a bad solution (slower, what else?): if ([property isEqualToString:kSectionHeaders]){...} I feel I've done something

Implementing hashcode and equals for custom classes

巧了我就是萌 提交于 2019-12-01 06:35:52
So I have many custom classes are also have custom clases inside of them using composition. My custom classes have variables that change very frequently and I add them to HashSets. So my question is when I implement hashCode - what should I do for a class that only has private fields that constantly change? Here is an example of one custom class: public class Cell { protected boolean isActive; protected boolean wasActive; public Cell() { this.isActive = false; this.wasActive = false; } // getter and setter methods... @Override public int hashCode() { // HOW SHOULD I IMPLEMENT THIS IF THIS

override equals method to compare more than one field in java

Deadly 提交于 2019-12-01 05:49:05
问题 What is the best way to override equals method in java to compare more than one field? For example, I have 4 objects in the class, o1, o2, o3, o4 and I want compare all of them with the passed object to the equals method. if (o1 != null && o2 != null && o3 != null && o4 != null && obj.o1 != null && obj.o2 != null && obj.o3 != null && obj.o4 != null && o1.equals(obj.o1) && o2.equals(obj.o2) && o3.equals(obj.o3) && o4.equals(obj.o4)) { do something } The problem with this code is that it's not

Objective-C constants: NSString comparison using ==?

夙愿已清 提交于 2019-12-01 05:32:18
问题 The discussions I found about setting NSString constants made me code it the following way: .h file: extern NSString * const kSectionHeaders; .m file: NSString * const kSectionHeaders = @"header"; As the program runs, it has to test words from a text file against a series of NSString constants. I read memory comparison should work when setting function like stated above: if (property == kSectionHeaders) {...} Doesn't work tough :( The following works, but it was described as a bad solution

Split list into two equal lists in F#

二次信任 提交于 2019-12-01 05:16:09
I'm really new to F#, and I need a bit of help with an F# problem. I need to implement a cut function that splits a list in half so that the output would be... cut [1;2;3;4;5;6];; val it : int list * int list = ([1; 2; 3], [4; 5; 6]) I can assume that the length of the list is even. I'm also expected to define an auxiliary function gencut(n, xs) that cuts xs into two pieces, where n gives the size of the first piece: gencut(2, [1;3;4;2;7;0;9]);; val it : int list * int list = ([1; 3], [4; 2; 7; 0; 9]) I wouldn't normally ask for exercise help here, but I'm really at a loss as to where to even

Implementing hashcode and equals for custom classes

狂风中的少年 提交于 2019-12-01 05:15:44
问题 So I have many custom classes are also have custom clases inside of them using composition. My custom classes have variables that change very frequently and I add them to HashSets. So my question is when I implement hashCode - what should I do for a class that only has private fields that constantly change? Here is an example of one custom class: public class Cell { protected boolean isActive; protected boolean wasActive; public Cell() { this.isActive = false; this.wasActive = false; } //

LinkedHashSet .equals() vs LinkedList .equals() with same elements but different order

﹥>﹥吖頭↗ 提交于 2019-12-01 04:19:25
Consider the following SSCCE: public static void main(String[] args) { LinkedHashSet<String> set1 = new LinkedHashSet<>(); set1.add("Bob"); set1.add("Tom"); set1.add("Sam"); LinkedHashSet<String> set2 = new LinkedHashSet<>(); set2.add("Sam"); set2.add("Bob"); set2.add("Tom"); System.out.println(set1); System.out.println(set2); System.out.println(set1.equals(set2)); } This prints: [Bob, Tom, Sam] [Sam, Bob, Tom] true Yet if you change LinkedHashSet to LinkedList : public static void main(String[] args) { LinkedList<String> set1 = new LinkedList<>(); set1.add("Bob"); set1.add("Tom"); set1.add(

Overriding hashCode() - is this good enough?

谁都会走 提交于 2019-12-01 04:09:24
For a class whose fields are solely primitive, ex.: class Foo { int a; String b; boolean c; long d; boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Foo)) return false; Foo other = (Foo) o; return a == other.a && b.equals(other.b) && c == other.c && d = other.d; } } Is this a reasonably "good enough" way to write hashCode() ? boolean hashCode() { return (b + a + c + d).hashCode(); } That is, I construct a String out of the same fields that equals() uses, and then just use String#hashCode() . Edit: I've updated my question to include a long field. How should a long be

Is there a way to list all calls of equals() of a certain class using Eclipse?

≯℡__Kan透↙ 提交于 2019-12-01 03:41:12
I'm confronted with the following problem at the moment: I have a certain class in which the equals() - Method is overridden. However I'm not sure if it is ever being used (either in my or in one of my collegues projects). Is there a way to find out? When I search for references, well, it just gives me ALL references to the Object equals() - Method (which are quite a few). There surely must be an easier way than scan through all of them... Anyone got an idea? You're asking Eclipse to solve an impossible task. To figure out if a particular overridden method is called or not is not statically

Why are two objects with same data not equal while using equals() method [duplicate]

廉价感情. 提交于 2019-12-01 03:38:24
问题 This question already has answers here : Why do I need to override the equals and hashCode methods in Java? (28 answers) Closed 6 years ago . public class Account { String account; double balance; Account(String account, double balance) { this.account = account; this.balance = balance; } } public class AccountTest { public static void main(String[] args) { Account a1 = new Account("Sandy", 1000); Account a2 = new Account("Sandy", 1000); System.out.println(a1.equals(a2)); } } When i execute it