I have array list in java:
List<Correction> Auv=new ArrayList<>();
List<Correction> Produv=new ArrayList<>();
then I want to substract Produv value by Auv, here's an example:
Produv.add(new Correction("a","b"));
Produv.add(new Correction("a","c"));
Produv.add(new Correction("b","d"));
Produv.add(new Correction("b","c"));
Auv.add(new Correction("c","a"));
Auv.add(new Correction("b","c"));
Produv.removeall(Auv);
but nothing subtracted, the array still contain it initial value, is there any way to do this?
I try to override equals(), and still got the same result
here the code of my Correction class:
public class Correction {
private String node0;
private String node1;
public Correction(String node0, String node1) {
this.node0 = node0;
this.node1 = node1;
}
public void setNode0(String node0){
this.node0=node0;
}
public void setNode1(String node1){
this.node1=node1;
}
public String getNode0(){
return node0;
}
public String getNode1(){
return node1;
}
#Override
public boolean equals(Object object){
boolean same = false;
if (object != null && object instanceof Correction)
{
same = this.node0 == ((Correction) object).node1 && this.node1 == ((Correction) object).node1;
}
return same;
}
}
Solved!!
it just simply a mistake on overriding equals() method(thank's guys)
here my correction:
#Override
public boolean equals(Object object){
boolean same = false;
if (object != null && object instanceof Correction)
{
same = (this.node0 == ((Correction) object).node1 && this.node1 == ((Correction) object).node0)||(this.node0 == ((Correction) object).node0 && this.node1 == ((Correction) object).node1);
}
return same;
}
Your equals method looks wrong. It makes more sense to compare this.node0 to ((Correction) object).node0.
I think it should be:
public boolean equals(Object object){
boolean same = false;
if (object != null && object instanceof Correction)
{
same = this.node0.equals(((Correction) object).node0) && this.node1.equals(((Correction) object).node1);
}
return same;
}
Also, is this a typo?
Auv.add("c","a");
Auv.add("b","c");
It should probably be :
Auv.add(new Correction ("c","a"));
Auv.add(new Correction ("b","c"));
Related
I need to write abstract class, which looks like this.
public abstract class Value {
public abstract String toString();
public abstract Value add(Value v);
public abstract Value sub(Value v);
public abstract boolean eq(Value v);
public abstract boolean lte(Value v);
public abstract boolean gte(Value v);
public abstract boolean neq(Value v);
public abstract boolean equals(Object other);
public abstract int hashCode();
public abstract Value create(String s);
}
Now I need to make few classe, which inherit from that one. I started from Int class and implemented it like this:
public class Int extends Value {
int val;
public String toString() {
String toStr = Integer.toString(val);
return toStr;
}
public Int add(Value v) {
Int result = new Int();
if(v instanceof Int) {
Int temp = (Int) v;
result.val = val + temp.val;
}
return result;
}
public Int sub(Value v) {
Int result = new Int();
if(v instanceof Int) {
Int temp = (Int) v;
result.val = val - temp.val;
}
return result;
}
public boolean eq(Value o) {
if(this == o) return true;
if(this == null) return false;
if(getClass() != o.getClass()) return false;
Int other = (Int) o;
return toString() == other.toString();
}
public boolean lte(Value v) {
if(v instanceof Int) {
Int temp = (Int) v;
return this.val < temp.val;
}
return false;
}
public boolean gte(Value v) {
if(v instanceof Int) {
Int temp = (Int) v;
return this.val > temp.val;
}
return false;
}
public boolean neq(Value v) {
if(v instanceof Int) {
Int temp = (Int) v;
return !eq(temp);
}
return true;
}
public boolean equals(Object o) {
if(this == o) return true;
if(this == null) return false;
if(getClass() != o.getClass()) return false;
Int other = (Int) o;
return toString() == other.toString();
}
public int hashCode() {
Integer hash = val;
return hash.hashCode();
}
public Int create(String s) {
val = Integer.parseInt(s);
return this;
}
}
Everything is compiling and working, but I have no clue if my hashcode() function and equals() are good. Furthermore i want to use create() to make objects like this:
getInstance().create("1234");
Is my method also sufficient?
Everything is compiling and working, but I have no clue if my hashcode() function and equals() are good.
Your equals() should compare int val and not result of toString() of compared objects (this.val == other.val).
Your hashCode() looks good, though I would add #Override to it (same with equals()).
Furthermore i want to use create() to make objects like this: getInstance().create("1234");
Looking at its implementation, it looks fine (i.e. would work according to your needs):
public Int create(String s) {
val = Integer.parseInt(s);
return this;
}
though I don't think you really want to use it with getInstance(). Simply Int.create() would be enough:
public static Int create(String s) {
val = Integer.parseInt(s);
return new Int(val);
}
Note that you would need a private constructor.
Also, as someone noted in the comments, consider using generics instead of inheritance.
The hashCode() method is fine (although I'd add an #Override annotation, just to make the code easier to maintain and avoid mistakes), but the equals(Object) definitely isn't.
Following the logic you have in place, == isn't the right way to compare strings. You should use equals instead (see, e.g., How do I compare strings in Java?). In addition, as Joakim Danielson noted in the comments, this can never be null - you should check if o is null instead:
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null) {
return false;
}
if(getClass() != o.getClass()) {
return false;
}
Int other = (Int) o;
return toString().equals(other.toString()); // Here!
}
But in all fairness, there's no reason to use toString - you could just compare the internal val:
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null) {
return false;
}
if(getClass() != o.getClass()) {
return false;
}
Int other = (Int) o;
return val == other.val; // Here!
}
First when you override Methods please do it with #Override Annotation. Then i would implement your equals method in another way. Just do return this.val == other.val instead of doing this.toString() == other.toString(). Your toString() method implementation is ok. Your hashCode is good as well. But please remove that create method. Use a constructor instead.
Can I implement equals() method using eq() like this ?
public boolean equals(Object o) {
Value compare = (Value) o;
return eq(compare);
}
#IdClass=(value = TripleKey.class)
class Triple {
String subject;
String predicate;
String object;
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Triple triple = (Triple) o;
if (!subject.equals(triple.subject)) return false;
return predicate.equals(triple.predicate);
}
#Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + subject.hashCode();
result = 31 * result + predicate.hashCode();
return result;
}
}
my objects are:
{
"subject": "http://www.someurl.com/thing/resources/<owner>#SenMLJSON",
"predicate": "probes_in",
"object":"http://sweet.jpl.nasa.gov/2.3/matrSediment.owl#clay"
}
and
{
"subject": "http://www.someurl.com/thing/resources/<owner>#SenMLJSON",
"predicate": "probes_in",
"object":"http://sweet.jpl.nasa.gov/2.3/matrSediment.owl#sand"
}
When I try the following I still have duplicates :
public static List<Triple> mergeTripleLists(List<Triple> oldList, List<Triple> newList) {
Set<Triple> tripleSet = new HashSet<>(oldList);
tripleSet.removeAll(newList);
tripleSet.addAll(newList);
return new ArrayList<>(tripleSet);
}
The problem is in:
if (!super.equals(o)) return false;
If should work after removing it.
The problem is the call to the equals method of the super class which uses object reference to test equality, so remove the line with
!super.equals(o)
You also need to remove the call to the hashCode method of the super class.
What is the correct way to compare two Pair<?,?> ?
I use this code, but I'm not 100% sure about it?
May be you know another better solution?
public boolean equals(Object other) {
if (other instanceof Pair<?,?>) {
Pair<?, ?> otherPair = (Pair<?, ?>) other;
return equalsPart(this._first, otherPair._first) && equalsPart(this._second, otherPair._second);
}
return false;
}
public boolean equalsPart(Class<?> one, Class<?> two) {
return ((one== two || (one!= null && two != null && one.equals(two))));
}
public boolean equals(Object other) {
if(this == other)
return true;
if (!(other instanceof Pair)) {
return false;
}
Pair<?, ?> otherPair = (Pair<?, ?>) other;
return Equality.equals(this.first,otherPair._first) &&
Equality.equals(this._second, otherPair._second);
}
class Equality{
public static boolean equals(Object first, Object second){
return first == null ? second == null : first.equals(second);
}
}
You will find this kind of idiom being followed in many libraries for equals(). A precondition check and a fail fast approach to equals().
Have created a new Utility Equality for use in further classes which improves readability as well as provides proper null checking.
I believe the following is an accurate solution to this. Since neither Java nor the OP provide a definition for Pair<?, ?>, I have included the complete definition for the class I actually use below. The code includes a definition of hashCode for completeness.
public class Tuple2<T1, T2> {
private final T1 item1;
private final T2 item2;
public Tuple2(T1 item1, T2 item2) {
this.item1 = item1;
this.item2 = item2;
}
public final T1 getItem1() {
return item1;
}
public final T2 getItem2() {
return item2;
}
#Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
else if (!(obj instanceof Tuple2<?, ?>)) {
return false;
}
Tuple2<?, ?> other = (Tuple2<?, ?>)obj;
return Tuple.equals(this.item1, other.item1)
&& Tuple.equals(this.item2, other.item2);
}
#Override
public int hashCode() {
int hash = 5;
hash = 79 * hash + (this.item1 != null ? this.item1.hashCode() : 0);
hash = 79 * hash + (this.item2 != null ? this.item2.hashCode() : 0);
return hash;
}
}
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 the timestamp attribute, so that when I compare these two objects, the equals() method would return a true value:
[SSID: __mynetwork__, BSSID: 00:0e:2e:ae:4e:85, capabilities: [WPA-PSK-TKIP][ESS], level: -69, frequency: 2457, timestamp: 117455824743]
[SSID: __mynetwork__, BSSID: 00:0e:2e:ae:4e:85, capabilities: [WPA-PSK-TKIP][ESS], level: -69, frequency: 2457, timestamp: 117460312231]
Note: When I derive a customer class which extends ScanResult I get the following error when I try to implement a constructor: The constructor ScanResult() is not visible
You just have to implement it without checking the fields you want to ignore. Don't forget to override the hashode() too.
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((field1 == null) ? 0 : field1.hashCode());
result = prime * result + ((field2 == null) ? 0 : field2.hashCode());
...etc
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ScanResult other = (ScanResult ) obj;
if (field1 == null) {
if (other.field1 != null)
return false;
} else if (!field1.equals(other.field1))
return false;
if (field2 == null) {
if (other.field2 != null)
return false;
} else if (!field2 .equals(other.field2 ))
return false;
}
... etc
}
This is your Overriden equals() Method....
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ScanResult)) return false;
if(!BSSID.equals(o.BSSID)) return false;
if(!SSID.equals(o.SSID)) return false;
if(!capabilities.equals(o.capabilities)) return false;
if(frequency != o.frequency) return false;
if(level != o.level) return false;
return true;
}
EDIT
Now that the above solution not working, may I suggest:
class ScanResultComparator {
public static boolean equals(ScanResult a, ScanResult b){
if(!a.BSSID.equals(b.BSSID)) return false;
if(!a.SSID.equals(b.SSID)) return false;
if(!a.capabilities.equals(b.capabilities)) return false;
if(a.frequency != b.frequency) return false;
if(a.level != b.level) return false;
return true;
}
}
Extend the ScanResult class and only override the equals() method
class CustomScanResult extends ScanResult {
#Override
public boolean equals(Object o) {
// ... custom logic ...
}
}
simple way use check all variables for true.
example:
public class FtpFile {
public String host;
public String port;
public String fileName;
public String path;
public String username;
public String password;
/**
* Override Equals()
*/
#Override
public boolean equals(Object o) {
try {
if (o == null || getClass() != o.getClass())
return false;
FtpFile p = (FtpFile) o;
return ((host.equals(p.host))
&& (port.equals(p.port))
&& (path.equals(p.path))
&& (username.equals(p.username))
&& (password.equals(p.password)));
} catch (Exception ex) {
return false;
}
}
}
I have a set of this structure. How to remove duplicates of equal object of that class? Equals means field File plik is the same.
EDIT:
But the problem gut bigger I don't have duplicates but I would like to replace old SET member by new.
Withoud 3rd party libraries.
import java.io.*;
public class WordInfo implements Serializable {
File plik;
Integer wystapienia;
public WordInfo(File plik, Integer wystapienia) {
this.plik = plik;
this.wystapienia = wystapienia;
}
public String toString() {
// if (plik.getAbsolutePath().contains("src") && wystapienia != 0)
return plik.getAbsolutePath() + "\tWYSTAPIEN " + wystapienia;
// return "";
}
}
EDIT
I don't have this HashCodeBuilder I want to use Java standard libraries
public int hashCode() {
return new HashCodeBuilder(17, 31).append(plik).append(wystapienia).toHashCode();
}
public boolean equals(Object obj) {
File f = (File) obj;
return(plik.getAbsoluteFile().equals(f.getAbsolutePath()));
}
As discussed here, override equals.
public class Person {
private String name;
private int age;
// ...
public int hashCode() {
return new HashCodeBuilder(17, 31). // two randomly chosen prime numbers
// if deriving: appendSuper(super.hashCode()).
append(name).
append(age).
toHashCode();
}
public boolean equals(Object obj) {
if (obj == null)
return false;
if (obj == this)
return true;
if (obj.getClass() != getClass())
return false;
Person rhs = (Person) obj;
return new EqualsBuilder().
// if deriving: appendSuper(super.equals(obj)).
append(name, rhs.name).
append(age, rhs.age).
isEquals();
}
}
public class WordInfo implements Serializable {
File plik;
Considering that following is how you can override the equals and hashCode method as per your requirement:
#Override
public boolean equals(Object obj) {
if(this == obj) return true;
if(!(obj instanceof WordInfo)) return false;
return this.plik.equals(((WordInfo) obj).plik);
}
#Override
public int hashCode() {
return this.plik.hashCode();
}