Why does this result in 1?

The name of the pictureThe name of the pictureThe name of the pictureClash 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









share









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 on equals() and hashCode() 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















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









share









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 on equals() and hashCode() 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













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









share









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





share









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.










share









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.








share



share








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 on equals() and hashCode() 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




    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




    @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













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.






share|improve this answer


















  • 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


















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






share|improve this answer




















    Your Answer





    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    Fredrick Norman is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    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






























    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.






    share|improve this answer


















    • 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















    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.






    share|improve this answer


















    • 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













    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.






    share|improve this answer














    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 32 mins ago

























    answered 40 mins ago









    Eran

    268k35415500




    268k35415500







    • 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













    • 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








    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













    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






    share|improve this answer
























      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






      share|improve this answer






















        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






        share|improve this answer












        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 35 mins ago









        user7294900

        15.6k92851




        15.6k92851




















            Fredrick Norman is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            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.













             


            draft saved


            draft discarded














            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













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            One-line joke