equals

Java file equals

老子叫甜甜 提交于 2019-12-01 15:40:39
I don't know about you guys but at least I expected that f1 would be equal to f2 in the below code but apparently that's not the case! What's your thoughts about this? It seems like I have to write my own equals method to support it, right? import java.io.*; public class FileEquals { public static void main(String[] args) { File f1 = new File("./hello.txt"); File f2 = new File("hello.txt"); System.out.println("f1: " + f1.getName()); System.out.println("f2: " + f2.getName()); System.out.println("f1.equals(f2) returns " + f1.equals(f2)); System.out.println("f1.compareTo(f2) returns " + f1

why listbox1.Items.Add use Equals Method of my Object?

心已入冬 提交于 2019-12-01 15:31:21
i'm scrutinizing Windows Forms ListBoxCollection Add Method, during the analyzing i found that the method "add" calls Equals method in base Object why this method do this action ? I Have used "Call Stack" and I have Find This Answer : We Call :System.Windows.Forms.ListBox.ObjectCollection.Add(object item) and it will Call : System.Windows.Forms.ListBox.ObjectCollection.AddInternal(object item) and it will Call : System.Windows.Forms.ListBox.NativeAdd(object item) and it will call : System.Windows.Forms.ListControl.GetItemText(object item) and it will call : System.Windows.Forms.Formatter

Equals method of System.Collections.Generic.List<T>…?

旧街凉风 提交于 2019-12-01 15:26:08
I'm creating a class that derives from List... public class MyList : List<MyListItem> {} I've overridden Equals of MyListItem... public override bool Equals(object obj) { MyListItem li = obj as MyListItem; return (ID == li.ID); // ID is a property of MyListItem } I would like to have an Equals method in the MyList object too which will compare each item in the list, calling Equals() on each MyListItem object. It would be nice to simply call... MyList l1 = new MyList() { new MyListItem(1), new MyListItem(2) }; MyList l2 = new MyList() { new MyListItem(1), new MyListItem(2) }; if (l1 == l2) { ..

Java file equals

落爺英雄遲暮 提交于 2019-12-01 13:47:31
问题 I don't know about you guys but at least I expected that f1 would be equal to f2 in the below code but apparently that's not the case! What's your thoughts about this? It seems like I have to write my own equals method to support it, right? import java.io.*; public class FileEquals { public static void main(String[] args) { File f1 = new File("./hello.txt"); File f2 = new File("hello.txt"); System.out.println("f1: " + f1.getName()); System.out.println("f2: " + f2.getName()); System.out

Why does changing the hashcode of an object used as a key in a HashMap make a lookup return null?

前提是你 提交于 2019-12-01 11:31:15
Consider the following scenario: Object o1 = new Object(); Object o2 = new Object(); HashMap<Object, Object> map = new HashMap<Object, Object>(); map.put(o1, o2); boolean test1 = map.get(o1) == o2; // This evaluates to true // Now lets say we alter the state of o1: o1.setSomeInternalState(Object newState); boolean test2 = map.get(o1) == o2; // This evaluates to false, because now map.get(o1) returns null Assume that the class for o1 has overridden equals() and hashCode() . I've encountered this issue during debugging because I had explicitly overridden equals and hashCode on one particular

Do I need to implement hashCode() and equals() methods?

若如初见. 提交于 2019-12-01 09:28:17
If I have a map and an object as map key, are the default hash and equals methods enough? class EventInfo{ private String name; private Map<String, Integer> info } Then I want to create a map: Map<EventInfo, String> map = new HashMap<EventInfo, String>(); Do I have to explicitly implement hashCode() and equals()? Thanks. Yes, you do. HashMap s work by computing the hash code of the key and using that as a base point. If the hashCode function isn't overriden (by you), then it will use the memory address, and equals will be the same as == . If you're in Eclipse, it'll generate them for you.

Determine if Equals() is an override?

橙三吉。 提交于 2019-12-01 09:24:26
I have an instance of Type (type). How can I determine if it overrides Equals()? private static bool IsObjectEqualsMethod(MethodInfo m) { return m.Name == "Equals" && m.GetBaseDefinition().DeclaringType.Equals(typeof(object)); } public static bool OverridesEqualsMethod(this Type type) { var equalsMethod = type.GetMethods() .Single(IsObjectEqualsMethod); return !equalsMethod.DeclaringType.Equals(typeof(object)); } Note that this reveals whether object.Equals has been overridden anywhere in the inheritance hierarchy of type . To determine if the override is declared on the type itself, you can

override equals method to compare more than one field in java

蹲街弑〆低调 提交于 2019-12-01 08:51:29
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 clear and can't be modified easily specially if we have more fields. Is there a better way to achieve

Why does changing the hashcode of an object used as a key in a HashMap make a lookup return null?

别等时光非礼了梦想. 提交于 2019-12-01 07:36:43
问题 Consider the following scenario: Object o1 = new Object(); Object o2 = new Object(); HashMap<Object, Object> map = new HashMap<Object, Object>(); map.put(o1, o2); boolean test1 = map.get(o1) == o2; // This evaluates to true // Now lets say we alter the state of o1: o1.setSomeInternalState(Object newState); boolean test2 = map.get(o1) == o2; // This evaluates to false, because now map.get(o1) returns null Assume that the class for o1 has overridden equals() and hashCode() . I've encountered

Comparing two lists and removing duplicates from one

℡╲_俬逩灬. 提交于 2019-12-01 07:36:41
I have an object called FormObject that contains two ArrayLists - oldBooks and newBooks - both of which contain Book objects. oldBooks is allowed to contain duplicate Book objects newBooks is not allowed to contain duplicate Book objects within itself and cannot include any duplicates of Book objects in the oldBooks list. The definition of a duplicate Book is complex and I can't override the equals method as the definition is not universal across all uses of the Book object. I plan to have a method on the FormObject class called removeDuplicateNewBooks which will perform the above