Skip to content

Instantly share code, notes, and snippets.

@albanoj2
Created February 28, 2018 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albanoj2/8601c974d9ed357cafea914a4b290538 to your computer and use it in GitHub Desktop.
Save albanoj2/8601c974d9ed357cafea914a4b290538 to your computer and use it in GitHub Desktop.
public class Foo {
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
else if (obj == null || obj.getClass() != getClass()) {
return false;
}
else {
Foo foo = (Foo) obj;
return Objects.equals(foo.firstName, firstName);
}
}
}
Foo foo = new Foo("John");
Foo fooIdentical = new Foo("John");
Bar bar = new Bar("John");
Set < Foo > foos = new HashSet < > ();
foos.add(foo);
foos.add(fooIdentical);
System.out.println(foos); // [Foo@23514a]
foos.add(bar);
System.out.println(foos); // [Foo@23514a, Bar@23514a]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment