Johanna
Johanna

Reputation: 27660

What does this statement mean?

I am a beginner in Java and do not understand what if(!s.add(a)) means in this code excerpt:

Set<String> s = new HashSet<String>();
for(String a:args) {
    if(!s.add(a)) System.out.println("Duplicate detected:"+a);
}

Upvotes: 1

Views: 1561

Answers (11)

John Hascall
John Hascall

Reputation: 9416

Presented for your consideration:

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

public class s1 {
    public static final boolean DUPLICATE = false;
    public static void main( String[] args ) {
        Set<String> s = new HashSet<String>();

        if (!s.add("Hello Bub")) System.out.println("Duplicate detected(1)");

        if (s.add("Hello Bub") == DUPLICATE) System.out.println("Duplicate detected(2)");
    }
}

Which seems clearer?

Upvotes: 0

David M
David M

Reputation: 72890

The add method returns a boolean indicating whether or not the add succeeded. Might be clearer with indenting:

Set<String> s = new HashSet<String>();
for(String a:args)
    if(!s.add(a))
        System.out.println("Duplicate detected:"+a);

Or even better with braces:

Set<String> s = new HashSet<String>();
for(String a:args) {
    if(!s.add(a)) {
            System.out.println("Duplicate detected:"+a);
    }
}

The message is displayed if the add failed.

Upvotes: 2

Tom
Tom

Reputation: 45124

As others have answered, add returns a boolean indicating whether a has been added to the set. Its equivalent to

Set<String> s = new HashSet<String>();
for(String a:args) {
    if (s.contains(a)){
      System.out.println("Duplicate detected:"+a);
    }
    else{
       s.add(a);
    }
}

From the javadoc for the Set interface.

contains

boolean contains(Object o)

     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)).

Upvotes: 2

ATorras
ATorras

Reputation: 4303

No Set object allow duplicates. The HashSet object don't allow duplicates too, but the get() method ( put() and contains() too ) runs in constant time O(k) so it's a good way to check for duplicates.

Upvotes: 0

Neal Donnan
Neal Donnan

Reputation: 1733

The Set data structure can only contain unique values.

The method add(Object) will return false if the object could not be added (i.e. It is a duplicate entry).

The method will return true if the object was added to the set.

Upvotes: 0

Kieveli
Kieveli

Reputation: 11085

"!" is the symbol for 'NOT'.

Read it as:

'if not add argument a to hashset s, then...'

OR

'if adding argument a to hashset s returns false, then...'

Upvotes: 0

geowa4
geowa4

Reputation: 41833

From the docs:

Adds the specified element to this set if it is not already present

If this set already contains the element, the call leaves the set unchanged and returns false.

Upvotes: 0

Tim Hoolihan
Tim Hoolihan

Reputation: 12396

if collection s already has item a, then the add method will return false. The ! is a "not" operator, turning that false into true. So, if the item is already in the collection, you will see the println result.

Upvotes: 6

oxbow_lakes
oxbow_lakes

Reputation: 134310

s.add(a) will only add a to the set if it is not already contained in the set (by equality). The add method returns true iff the operation causes the set to be modified.

hence if a was already in the Set, the add method would not add it again, therefore not modify the set, therefore return false.

Upvotes: 1

Michael Myers
Michael Myers

Reputation: 192005

add is specified in the Collection interface to return a boolean indicating whether the addition was successful. From the Javadocs:

Returns true if this collection changed as a result of the call. (Returns false if this collection does not permit duplicates and already contains the specified element.)

This code prints out a message if the addition was unsuccessful, which happens when there is a duplicate in the set.

Upvotes: 10

Matthew Steeples
Matthew Steeples

Reputation: 8078

s.add() will return a boolean depending on whether the item was added to the collection (true) or not (false)

Upvotes: 3

Related Questions