equals

Comparing two strings with boolean?

百般思念 提交于 2019-12-08 09:25:46
问题 I was trying this code writing exercise and I am so lost! The exercise is: Complete the method which takes two Strings and one boolean as input. If the boolean is true, this method compares the first two Strings, ignoring case considerations (uppercase/lowercase). Two Strings are considered equal ignoring case if they are of the same length, and corresponding characters in the two Strings are equal ignoring case. If the boolean is false, this method should compare two Strings and return true

hashcode and equals contract vice versa

醉酒当歌 提交于 2019-12-08 07:33:16
问题 I know the contract says "if two objects are equal then they should return the same hash code". That is so that those objects can be place in the same hash bucket and to improve hash code related collection functions know. Then again why it says "if two objects have same hash code those should not always be equals". I mean if it is true in the contract we should say "if two objects are equals they may return same hash code but which is not mandatory" 回答1: I mean if it is true in the contract

Comparing VALUE and REFERENCE of types

柔情痞子 提交于 2019-12-08 05:19:08
问题 I know there are a lot of ways to compare VALUE and REFERENCES in C#, but I'm still a bit confused about what type performs what when you try to compare either VALUE or REFERENCE. String examples: string str = "hello"; string str2 = "hello"; if (str == str2) { Console.WriteLine("Something"); } // Is this a comparison of value? if (str.Equals(str2)) { Console.WriteLine("Something"); } // Is this a comparison of value? string.ReferenceEquals(str, str2); // Comparison of reference (True) Console

Why Java uses 'equals' instead of '==' to check an object's presence in a Map? [duplicate]

筅森魡賤 提交于 2019-12-08 03:52:58
问题 This question already has answers here : Difference between Equals/equals and == operator? (11 answers) Closed 5 years ago . Suppose if I want to put a key and its value in a Map. I believe it is what Java does: Get the Hashcode of the key and check whether there is a key in the map with the same hashcode. If there is no key with the same Hashcode then the key can be put into Map. If there is a key with the same Hashcode then use equals to determine whether the key can be put into Map or not.

Java: If I overwrite the .equals method, can I still test for reference equality with ==?

 ̄綄美尐妖づ 提交于 2019-12-08 01:36:26
问题 I have the following situation: I need to sort trees based by height, so I made the Tree's comparable using the height attribute. However, I was also told to overwrite the equals and hashCode methods to avoid unpredictable behaviour. Still, sometimes I may want to compare the references of the roots or something along those lines using ==. Is that still possible or does the == comparison call the equals method? 回答1: equals() is meant to compare an object with rules set by the programmer. In

Overriding equals/hashCode on cross referencing classes in Java causes StackOverflowError

两盒软妹~` 提交于 2019-12-07 17:18:56
问题 I have two classes that represent two different database entities. Their relationship is 1:m in db and it is represented in class structures something like this: public class Company { private List<Employee> employees; public List<Employee> getEmployees() { return employees; } public void setEmployees(List<Employee> employees) { this.employees = employees; } } public class Employee { private Company company; public Company getCompany() { return company; } public void setCompany(Company

Override equals() method only of a Java object

自作多情 提交于 2019-12-07 17:02:06
问题 I am developing an Android application which makes use of the ScanResult object. This object is in the form of: [SSID: __mynetwork__, BSSID: 00:0e:2e:ae:4e:85, capabilities: [WPA-PSK-TKIP][ESS], level: -69, frequency: 2457, timestamp: 117455824743] How would I override only the equals() method without creating a customer class which extends it in order to compare only the SSID , BSSID , capabilties , level and frequency attributes only? In other words, in the equals method I want to eliminate

How to check if JSON array is equal to

让人想犯罪 __ 提交于 2019-12-07 14:32:27
问题 I am creating pie charts with JSON and Flot. The JS function to create the pie chart receives a JSON array from Django in this format: [1, 3, 2, 5, 4] If there is no data, the JSON array is: [0, 0, 0, 0, 0] I'm trying to adjust the function so that if there is no data, then the pie will not be plotted and some text will appear instead (e.g. "Nothing to show yet"). So far I have tried: function loadWeekChart(theData) { var blankData = [0, 0, 0, 0, 0]; if ($.data(theData) == $.data(blankData)){

Influence of HashMap optimization that caches the hash code associated with each entry to its get method

与世无争的帅哥 提交于 2019-12-07 10:07:01
问题 from p.46 "Effective Java" Joshua Bloch. Item 9: ALways override hashCode when you override equals Some class PhoneNumber overrides equals() and doesn't override hashCode() "two instances are involved: one is used for insertion into the HashMap, and a second, equal, instance is used for (attempted) retrieval." ... "... Even if the two instances happen to hash to the same bucket, the get method will almost certainly return null , as HashMap has an optimization that caches the hash code

Technique for extending a class with private constructors

邮差的信 提交于 2019-12-07 07:57:14
问题 Is there a standard technique to "extend" a class with private constructors, as with most Singleton classes? Specifcally, I'm trying to extend the java.lang.management.ThreadInfo class because I'm adding a LOT of them to a HashSet to control uniqueness. However, the way I determine if two threads are equal is different and not identical to the default implementation of the equals() method. Extending the class is obviously not an option in this case. Would it be reasonable to make something