Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the size of this set 1 after adding 5 objects?

I'm trying to figure out why this code outputs 1:

import java.util.HashSet;
import java.util.Set;

public class Test {
    public static void main(String[] args) {
        Set<Foo> myFooSet= new HashSet<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;
     }
}
like image 764
Fredrick Norman Avatar asked Oct 24 '18 07:10

Fredrick Norman


People also ask

How to add items to a set in Java?

The add() method of Set in Java is used to add a specific element into a Set collection. The function adds the element only if the specified element is not already present in the set else the function return False if the element is already present in the Set.

What does set () do in Java?

The set() method of Java Stack is used to replace any particular element in the stack created using the Stack class with another element. This can be done by specifying the position of the element to be replaced and the new element in the parameter of the set() method.

Can you index a set in Java?

Unlike List, Set DOES NOT allow you to add duplicate elements. Set allows you to add at most one null element only. Set interface got one default method in Java 8: spliterator. Unlike List and arrays, Set does NOT support indexes or positions of it's elements.

How to check values in set in Java?

Check if Set Contains Element You can check if a Java Set contains a given element (object) by calling the contains() method. Here is an example of checking if a Java Set contains a given element: Set<String> set = new HashSet<>(); set. add("123"); set.


2 Answers

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.

like image 154
Eran Avatar answered Oct 26 '22 23:10

Eran


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

like image 35
user7294900 Avatar answered Oct 27 '22 00:10

user7294900