0

Here is the minimum working example:

import java.util.*;

public class Scratchpad {
    public static void main(String[] args) {
        Collection<int[]> collection = new HashSet<>();
        
        collection.add(new int[]{1,2});
        collection.remove(new int[]{1,2});

        System.out.println(collection.size());
        assert collection.size() == 1;
    }
}

The same works for if the items in the collection are of type List (which implements the equals method of course).

My suspicion is that it's because array are special objects that does not implement equals? (If that's case, I feel a little sad that array doesn't get as well documented as other standard library objects, at least not in the Java Docs for the standard lib)

Kun
  • 186
  • 2
  • 12
  • 3
    all Objects have an implementation of equals (it's inherited from Object). But it's not further implemented, hence they check whether the reference in the memory is the same, not the value – Stultuske May 18 '21 at 07:29
  • @Stultuske Got it, thanks. It's a little hard to find out cuz it's not in the standard lib's documentation. – Kun May 18 '21 at 07:32
  • It would work if you passed in the same array, but you pass in a different array with the same contents. Different arrays with the same contents are not equal in Java. – khelwood May 18 '21 at 07:32
  • @Kun what isn't? It's in the documentation of Object. It's the basics of the equals method. – Stultuske May 18 '21 at 07:33
  • It actually is in the docs. Since `equals()` is public the method is inherited by all objects and if they don't override that method the version in `Object` is used. And the JavaDoc on that one states: "This method returns true if and only if x and y refer to the same object(x == y has the value true)." – Thomas May 18 '21 at 07:34
  • @Stultuske The documentation of Object can't tell us if a subclass has overriden it. Only the documentation for that particular subcalss can show. – Kun May 18 '21 at 07:36
  • @Kun it's well known that Arrays don't have their own implementation. – Stultuske May 18 '21 at 07:37
  • Well-known to a Java guru, sure. But it's just not very beginner-friendly, that's all I'm saying. – Kun May 18 '21 at 07:39
  • I don't think this is a bad question but having it marked as a duplicated has banned me from asking further questions here. – Kun May 18 '21 at 07:41
  • 1
    @Kun further questions shouldn't be asked here, they should be asked in other questions. – Stultuske May 18 '21 at 07:44
  • I don't think it is a bad question either, but it is a duplicate. I can't tell if you have been banned from asking questions or not, but it you have it won't be a result of a single duplicate. (In fact I think that a dup with no + or - votes will be neutral wrt to question bans. The algorithms are not published ...) – Stephen C May 18 '21 at 07:44
  • @Stultuske How do you mean? Like in comments? – Kun May 18 '21 at 07:47
  • @Kun no. as in create a new question, just like you created this one – Stultuske May 18 '21 at 07:48
  • @user15793316 it is mentioned as 'inherited from' in the documentation of every class, but there is no documentations of int[] (for instance) – Stultuske May 18 '21 at 11:49

0 Answers0