Name

Set<E>

Synopsis

This interface represents an unordered Collection of objects that contains no duplicate elements. That is, a Set cannot contain two elements e1 and e2 where e1.equals(e2), and it can contain at most one null element. The Set interface defines the same methods as its superinterface, Collection. It constrains the add( ) and addAll( ) methods from adding duplicate elements to the Set. In Java 5.0 Set is a generic interface and the type variable E represents the type of the objects in the set.

An interface cannot specify constructors, but it is conventional that all implementations of Set provide at least two standard constructors: one that takes no arguments and creates an empty set, and a copy constructor that accepts a Collection object that specifies the initial contents of the new Set. This copy constructor must ensure that duplicate elements are not added to the Set, of course.

As with Collection, the Set methods that modify the contents of the set are optional, and implementations that do not support the methods throw java.lang.UnsupportedOperationException. See also Collection, List, Map, SortedSet, HashSet, and TreeSet.

java.util.Set<E>

Figure 16-54. java.util.Set<E>

public interface Set<E> extends Collection<E> {
// Public Instance Methods
     boolean add(E o);  
     boolean addAll(Collection<? extends E> c);  
     void clear( );  
     boolean contains(Object o);  
     boolean containsAll(Collection<?> ...

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.