1

I'm trying to learn Java, and I reached the HashSet part, Long story short I'm testing the contain method.

public static void main(String[] args) {
    HashSet<Integer> firstSet = new HashSet<>();
    firstSet.add(3);

    HashSet<Integer> secondSet = new HashSet<>();
    secondSet.add(3);

    boolean var = firstSet.contains(secondSet);

    System.out.println(var);

}

I believe this piece of code should return true, while it is returning false. Any help!

T. Marmash
  • 23
  • 3
  • 7
    [There is a difference between `contains` and `containsAll`](https://stackoverflow.com/a/14511683/7366707). The first set definitely does not _contain_ the second set itself, while it _does_ contain all of the _elements_ of the second set. – Salem Dec 08 '17 at 21:05
  • You are checking if your first HashSet is containing not the values of second HashSet but HashSet itself, if instead of firstSet.add(3); you did firstSet.add(secondSet), and your generic parameter for first set was > than your result would be true. – whatamidoingwithmylife Dec 08 '17 at 21:06
  • 1
    Yes, yes, I should have used containsAll instead of contains, Thank you.. – T. Marmash Dec 08 '17 at 21:12
  • 1
    If you want to check for a superset use `containsAll` as people have noted, but also note that for your two HashSets `firstSet.equals(secondSet)` will be `true` – Stephen P Dec 08 '17 at 21:25

2 Answers2

4

Let's look at the docs for contains:

Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that (o==null ? e==null : o.equals(e)).

Since we all know that there is no null values here, we can say that contains will return true if o.equals(e).

o is the parameter you pass to contains, which is a HashSet. e is the element of the first set, which should be an Integer. Since Integer and HashSet are unrelated types, it is very unlikely that they will equal.

You seem to have made a logical error here. To check if a set is a superset of another, call containsAll instead:

Returns true if this collection contains all of the elements in the specified collection.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • 1
    You could then ask why `contains` takes an `Object` instead of an `E`, which would make this kind of mistake impossible: that's because of historical reasons and backward compatibility, because `HashSet` already existed before generics were added to Java. They couldn't change the `contains` method to take an `E` instead of an `Object`, because that would break backward compatibility. – Jesper Dec 08 '17 at 21:25
2
boolean var = firstSet.contains(secondSet);

This line is causing the problem, because you don't check presence of specific element from secondSet, you check if your firstSet stores entire secondSet in itself, which is obviously false.

You should have checked, for example, if it . contains(secondSet.get(0)) or .contains(3).

If your intention was to check if firstSet contains all elements that are present in the secondSet, you could use firstSet.containsAll(secondSet).

Przemysław Moskal
  • 3,551
  • 2
  • 12
  • 21