Why does this result in 1?
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
I'm trying to figure out why the output of this code results in 1?
The code is as follows:
importjava.util.HashSet;
importjava.util.Set;
public class Test
public static void main(String args)
Set<Foo> myFooSet= newHashSet<Foo();
myFooSet.add(new Foo(2));
myFooSet.add(new Foo(1));
myFooSet.add(new Foo(3));
myFooSet.add(new Foo(3));
myFooSet.add(new Foo(2));
System.out.print(myFooSet.size());
class Foo
Integer code;
Foo(Integer c)
code= c;
public boolean equals(Foo f)
return false;
public boolean equals(Object f)
return true;
public int hashCode()
return 17;
I was wondering why does this code result in 1?
some extraa details
java hashmap hashset
New contributor
Fredrick Norman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
6
down vote
favorite
I'm trying to figure out why the output of this code results in 1?
The code is as follows:
importjava.util.HashSet;
importjava.util.Set;
public class Test
public static void main(String args)
Set<Foo> myFooSet= newHashSet<Foo();
myFooSet.add(new Foo(2));
myFooSet.add(new Foo(1));
myFooSet.add(new Foo(3));
myFooSet.add(new Foo(3));
myFooSet.add(new Foo(2));
System.out.print(myFooSet.size());
class Foo
Integer code;
Foo(Integer c)
code= c;
public boolean equals(Foo f)
return false;
public boolean equals(Object f)
return true;
public int hashCode()
return 17;
I was wondering why does this code result in 1?
some extraa details
java hashmap hashset
New contributor
Fredrick Norman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
Because the hashcode is the same and equals returns true so all 5 elemens are considered equal and replace each other (as you surely know sets don't allow duplicates). Read up onequals()
andhashCode()
for more information.
– Thomas
40 mins ago
1
@Thomas actually they don't replace each other. Only the first element is added.
– Eran
36 mins ago
@Eran ah yes, you're right. I should reread the JavaDoc ("If this set already contains the element, the call leaves the set unchanged and returns false.") - d'oh.
– Thomas
35 mins ago
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I'm trying to figure out why the output of this code results in 1?
The code is as follows:
importjava.util.HashSet;
importjava.util.Set;
public class Test
public static void main(String args)
Set<Foo> myFooSet= newHashSet<Foo();
myFooSet.add(new Foo(2));
myFooSet.add(new Foo(1));
myFooSet.add(new Foo(3));
myFooSet.add(new Foo(3));
myFooSet.add(new Foo(2));
System.out.print(myFooSet.size());
class Foo
Integer code;
Foo(Integer c)
code= c;
public boolean equals(Foo f)
return false;
public boolean equals(Object f)
return true;
public int hashCode()
return 17;
I was wondering why does this code result in 1?
some extraa details
java hashmap hashset
New contributor
Fredrick Norman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I'm trying to figure out why the output of this code results in 1?
The code is as follows:
importjava.util.HashSet;
importjava.util.Set;
public class Test
public static void main(String args)
Set<Foo> myFooSet= newHashSet<Foo();
myFooSet.add(new Foo(2));
myFooSet.add(new Foo(1));
myFooSet.add(new Foo(3));
myFooSet.add(new Foo(3));
myFooSet.add(new Foo(2));
System.out.print(myFooSet.size());
class Foo
Integer code;
Foo(Integer c)
code= c;
public boolean equals(Foo f)
return false;
public boolean equals(Object f)
return true;
public int hashCode()
return 17;
I was wondering why does this code result in 1?
some extraa details
java hashmap hashset
java hashmap hashset
New contributor
Fredrick Norman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Fredrick Norman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 41 mins ago


Himanshu Bhardwaj
3,24321030
3,24321030
New contributor
Fredrick Norman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 42 mins ago


Fredrick Norman
391
391
New contributor
Fredrick Norman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Fredrick Norman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Fredrick Norman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
Because the hashcode is the same and equals returns true so all 5 elemens are considered equal and replace each other (as you surely know sets don't allow duplicates). Read up onequals()
andhashCode()
for more information.
– Thomas
40 mins ago
1
@Thomas actually they don't replace each other. Only the first element is added.
– Eran
36 mins ago
@Eran ah yes, you're right. I should reread the JavaDoc ("If this set already contains the element, the call leaves the set unchanged and returns false.") - d'oh.
– Thomas
35 mins ago
add a comment |Â
3
Because the hashcode is the same and equals returns true so all 5 elemens are considered equal and replace each other (as you surely know sets don't allow duplicates). Read up onequals()
andhashCode()
for more information.
– Thomas
40 mins ago
1
@Thomas actually they don't replace each other. Only the first element is added.
– Eran
36 mins ago
@Eran ah yes, you're right. I should reread the JavaDoc ("If this set already contains the element, the call leaves the set unchanged and returns false.") - d'oh.
– Thomas
35 mins ago
3
3
Because the hashcode is the same and equals returns true so all 5 elemens are considered equal and replace each other (as you surely know sets don't allow duplicates). Read up on
equals()
and hashCode()
for more information.– Thomas
40 mins ago
Because the hashcode is the same and equals returns true so all 5 elemens are considered equal and replace each other (as you surely know sets don't allow duplicates). Read up on
equals()
and hashCode()
for more information.– Thomas
40 mins ago
1
1
@Thomas actually they don't replace each other. Only the first element is added.
– Eran
36 mins ago
@Thomas actually they don't replace each other. Only the first element is added.
– Eran
36 mins ago
@Eran ah yes, you're right. I should reread the JavaDoc ("If this set already contains the element, the call leaves the set unchanged and returns false.") - d'oh.
– Thomas
35 mins ago
@Eran ah yes, you're right. I should reread the JavaDoc ("If this set already contains the element, the call leaves the set unchanged and returns false.") - d'oh.
– Thomas
35 mins ago
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
9
down vote
Your defined public boolean equals(Object f)
and public int hashCode()
methods in the Foo
class that basically say all Foo
instances are equal to each other, so only one instance of Foo
can be added to any HashSet
.
Therefore myFooSet.size()
will return 1, regardless of the number of Foo
elements you attempted to add to it (as long as you added at least one).
Note: your public boolean equals(Foo f)
method is never used by HashSet
, since HashSet
only uses the equals
method declared in Object
class - public boolean equals(Object obj)
- which you overrode to always return true
.
1
The other important detail to make explicit is theequals(Foo)
overload is never invoked, because HashMap has no way to know that exists, soequals(Object)
is the overload that is invoked. So whilstnew Foo(1).equals(new Foo(1))
is false, that's never actually used.
– Andy Turner
35 mins ago
add a comment |Â
up vote
1
down vote
Adding to @Eran answer, defining equals(Foo f)
method isn't overriding the Object.equals(java.lang.Object) method being used when comparing, even if your Object is Foo
public boolean equals(Foo f) {
Isn't called in your code
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
Your defined public boolean equals(Object f)
and public int hashCode()
methods in the Foo
class that basically say all Foo
instances are equal to each other, so only one instance of Foo
can be added to any HashSet
.
Therefore myFooSet.size()
will return 1, regardless of the number of Foo
elements you attempted to add to it (as long as you added at least one).
Note: your public boolean equals(Foo f)
method is never used by HashSet
, since HashSet
only uses the equals
method declared in Object
class - public boolean equals(Object obj)
- which you overrode to always return true
.
1
The other important detail to make explicit is theequals(Foo)
overload is never invoked, because HashMap has no way to know that exists, soequals(Object)
is the overload that is invoked. So whilstnew Foo(1).equals(new Foo(1))
is false, that's never actually used.
– Andy Turner
35 mins ago
add a comment |Â
up vote
9
down vote
Your defined public boolean equals(Object f)
and public int hashCode()
methods in the Foo
class that basically say all Foo
instances are equal to each other, so only one instance of Foo
can be added to any HashSet
.
Therefore myFooSet.size()
will return 1, regardless of the number of Foo
elements you attempted to add to it (as long as you added at least one).
Note: your public boolean equals(Foo f)
method is never used by HashSet
, since HashSet
only uses the equals
method declared in Object
class - public boolean equals(Object obj)
- which you overrode to always return true
.
1
The other important detail to make explicit is theequals(Foo)
overload is never invoked, because HashMap has no way to know that exists, soequals(Object)
is the overload that is invoked. So whilstnew Foo(1).equals(new Foo(1))
is false, that's never actually used.
– Andy Turner
35 mins ago
add a comment |Â
up vote
9
down vote
up vote
9
down vote
Your defined public boolean equals(Object f)
and public int hashCode()
methods in the Foo
class that basically say all Foo
instances are equal to each other, so only one instance of Foo
can be added to any HashSet
.
Therefore myFooSet.size()
will return 1, regardless of the number of Foo
elements you attempted to add to it (as long as you added at least one).
Note: your public boolean equals(Foo f)
method is never used by HashSet
, since HashSet
only uses the equals
method declared in Object
class - public boolean equals(Object obj)
- which you overrode to always return true
.
Your defined public boolean equals(Object f)
and public int hashCode()
methods in the Foo
class that basically say all Foo
instances are equal to each other, so only one instance of Foo
can be added to any HashSet
.
Therefore myFooSet.size()
will return 1, regardless of the number of Foo
elements you attempted to add to it (as long as you added at least one).
Note: your public boolean equals(Foo f)
method is never used by HashSet
, since HashSet
only uses the equals
method declared in Object
class - public boolean equals(Object obj)
- which you overrode to always return true
.
edited 32 mins ago
answered 40 mins ago


Eran
268k35415500
268k35415500
1
The other important detail to make explicit is theequals(Foo)
overload is never invoked, because HashMap has no way to know that exists, soequals(Object)
is the overload that is invoked. So whilstnew Foo(1).equals(new Foo(1))
is false, that's never actually used.
– Andy Turner
35 mins ago
add a comment |Â
1
The other important detail to make explicit is theequals(Foo)
overload is never invoked, because HashMap has no way to know that exists, soequals(Object)
is the overload that is invoked. So whilstnew Foo(1).equals(new Foo(1))
is false, that's never actually used.
– Andy Turner
35 mins ago
1
1
The other important detail to make explicit is the
equals(Foo)
overload is never invoked, because HashMap has no way to know that exists, so equals(Object)
is the overload that is invoked. So whilst new Foo(1).equals(new Foo(1))
is false, that's never actually used.– Andy Turner
35 mins ago
The other important detail to make explicit is the
equals(Foo)
overload is never invoked, because HashMap has no way to know that exists, so equals(Object)
is the overload that is invoked. So whilst new Foo(1).equals(new Foo(1))
is false, that's never actually used.– Andy Turner
35 mins ago
add a comment |Â
up vote
1
down vote
Adding to @Eran answer, defining equals(Foo f)
method isn't overriding the Object.equals(java.lang.Object) method being used when comparing, even if your Object is Foo
public boolean equals(Foo f) {
Isn't called in your code
add a comment |Â
up vote
1
down vote
Adding to @Eran answer, defining equals(Foo f)
method isn't overriding the Object.equals(java.lang.Object) method being used when comparing, even if your Object is Foo
public boolean equals(Foo f) {
Isn't called in your code
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Adding to @Eran answer, defining equals(Foo f)
method isn't overriding the Object.equals(java.lang.Object) method being used when comparing, even if your Object is Foo
public boolean equals(Foo f) {
Isn't called in your code
Adding to @Eran answer, defining equals(Foo f)
method isn't overriding the Object.equals(java.lang.Object) method being used when comparing, even if your Object is Foo
public boolean equals(Foo f) {
Isn't called in your code
answered 35 mins ago


user7294900
15.6k92851
15.6k92851
add a comment |Â
add a comment |Â
Fredrick Norman is a new contributor. Be nice, and check out our Code of Conduct.
Fredrick Norman is a new contributor. Be nice, and check out our Code of Conduct.
Fredrick Norman is a new contributor. Be nice, and check out our Code of Conduct.
Fredrick Norman is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52963174%2fwhy-does-this-result-in-1%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
3
Because the hashcode is the same and equals returns true so all 5 elemens are considered equal and replace each other (as you surely know sets don't allow duplicates). Read up on
equals()
andhashCode()
for more information.– Thomas
40 mins ago
1
@Thomas actually they don't replace each other. Only the first element is added.
– Eran
36 mins ago
@Eran ah yes, you're right. I should reread the JavaDoc ("If this set already contains the element, the call leaves the set unchanged and returns false.") - d'oh.
– Thomas
35 mins ago