equals

Java Crazyness - Contains fails when equals passes

蓝咒 提交于 2020-01-13 11:07:49
问题 This is the most crazy thing I have seen in java (1.6): Set<ActionPlan> actionPlans = assessment.getActionPlans(); //getActionPlans() returns a java.util.HashSet<ActionPlan> ActionPlan actionPlan = actionPlans.iterator().next(); assertTrue(actionPlan1.equals(actionPlan)); assertEquals(actionPlan1.hashCode(), actionPlan.hashCode()); assertTrue(actionPlans.contains(actionPlan1)); The first two asserts pass but the last one fails. I'm not giving you details on the ActionPlan and Assessment

Java Crazyness - Contains fails when equals passes

家住魔仙堡 提交于 2020-01-13 11:06:08
问题 This is the most crazy thing I have seen in java (1.6): Set<ActionPlan> actionPlans = assessment.getActionPlans(); //getActionPlans() returns a java.util.HashSet<ActionPlan> ActionPlan actionPlan = actionPlans.iterator().next(); assertTrue(actionPlan1.equals(actionPlan)); assertEquals(actionPlan1.hashCode(), actionPlan.hashCode()); assertTrue(actionPlans.contains(actionPlan1)); The first two asserts pass but the last one fails. I'm not giving you details on the ActionPlan and Assessment

Can .NET test arrays for equivalence and not just equal references?

筅森魡賤 提交于 2020-01-13 09:44:08
问题 var a = new double[] {1, 2, 3}; var b = new double[] {1, 2, 3}; System.Console.WriteLine(Equals(a, b)); // Returns false However, I'm looking for a way to compare arrays which would compare the internal values instead of refernces. Is there a built in way to do this in .NET? Also, while I understand Equals comparing references, GetHashCode returns different values for these two arrays also, which I feel shouldn't happen, since they have the same internal values. 回答1: I believe you are looking

Boolean.TRUE == myBoolean vs. Boolean.TRUE.equals(myBoolean)

拥有回忆 提交于 2020-01-12 07:38:15
问题 Is there ever a situation where using equals(Boolean) and == would return different results when dealing with Boolean objects? Boolean.TRUE == myBoolean; Boolean.TRUE.equals(myBoolean); I'm not thinking about primitive types here, just Boolean objects. 回答1: How about: System.out.println(new Boolean(true) == new Boolean(true)); System.out.println(new Boolean(true) == Boolean.TRUE); (both print false, for the same reason as any other type of objects). 回答2: It would be dangerous to use ==

Implement “tolerant” `equals` & `hashCode` for a class with floating point members

天涯浪子 提交于 2020-01-12 04:52:08
问题 I have a class with a float field. For example: public class MultipleFields { final int count; final float floatValue; public MultipleFields(int count, float floatValue) { this.count = count; this.floatValue = floatValue; } } I need to be able to compare instances by value. Now how do I properly implement equals & hashCode ? The usual way to implement equals and hashCode is to just consider all fields. E.g. Eclipse will generate the following equals : public boolean equals(Object obj) { //

How to implement “equals” method for generics using “instanceof”?

人走茶凉 提交于 2020-01-11 16:36:04
问题 I have a class that accepts a generic type , and I want to override the equals method in a non-awkward way (i.e. something that looks clean and has minimal amount of code, but for a very general use case). Right now I have something like this: public class SingularNode<T> { private T value; @SuppressWarnings("unchecked") @Override public boolean equals(Object other){ if(other instanceof SingularNode<?>){ if(((SingularNode<T>)other).value.equals(value)){ return true; } } return false; } }

Meaning of Double.doubleToLongBits(x)

孤者浪人 提交于 2020-01-10 03:38:19
问题 I am writing a class Vec2D , representing a 2 dimensional vector. I store x and y in double s. When asked to generate equals(Object obj and hashCode() , eclipse generated this: @Override public int hashCode() { final int prime = 31; int result = 1; long temp; temp = Double.doubleToLongBits(x); result = prime * result + (int) (temp ^ (temp >>> 32)); temp = Double.doubleToLongBits(y); result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } @Override public boolean equals(Object

Why we need to override hashCode and equals?

馋奶兔 提交于 2020-01-07 09:47:09
问题 By default hashCode and equals works fine. I have used objects with hash tables like HashMap, without overriding this methods, and it was fine. For example: public class Main{ public static void main(String[] args) throws Exception{ Map map = new HashMap<>(); Object key = new Main(); map.put(key, "2"); Object key2 = new Main(); map.put(key2, "3"); System.out.println(map.get(key)); System.out.println(map.get(key2)); } } This code works fine. By default hashCode returning memory address of

Input can only be string

≡放荡痞女 提交于 2020-01-07 02:44:05
问题 I'm making a JOptionpane where the user can has to enter hes name. So it can't be empty and it can't be a number The empty part is working but now I have to find a solution for the number part As you can see in my code I made a while loop to check if "naam" isn't empty. Now I need to find a way to check if "naam" isn't a number. String naam = JOptionPane.showInputDialog("Geef uw naam in: "); while (naam.equals("")) { JOptionPane.showMessageDialog(null, "Geef een naam in", "Geef naam",

HashSet retainAll using interface

时光毁灭记忆、已成空白 提交于 2020-01-06 20:36:59
问题 I have some code where I try to use the HashSet.retainAll() function. In the example code below, the HashSet contains the interface IPerson , but the equals functions in object Person is never reached. I have even tried to expose the equals function in the interface and several other things. I feel I have tried everything. How can I make retainAll() use my implemented equal function? class Person implements IPerson { private String name; public Person(String name){ this.name = name; }