Java Reference
In-Depth Information
element in the class to occur more than once. Classes that implement the List<T>
interface have their elements ordered on a list, so there is a zeroth element, a first
element, a second element, and so forth. A class that implements the List<T> interface
allows elements to occur more than once. The ArrayList<T> class implements the
List<T> interface.
The Set<T> interface has the same method headings as the Collection<T>
interface, but in some cases the semantics (intended meanings) are different. For
example, the semantics of adding new elements to the set do not allow duplicates. The
add methods are described in Display 16.3. A complete list of the Set<T> interface is
given in Appendix 5 .
Display 16.3
Adding Elements in the Set<T> Interface
The Set<T> interface is in the java.util package.
The Set<T> interface extends the Collection<T> interface and has all the same method headings
given in Display 16.2. However, the semantics of the add methods vary as described below.
public boolean add(T element) (Optional)
If element is not already in the calling object, element is added to the calling object and true is
returned. If element is in the calling object, the calling object is unchanged and false is returned.
public boolean addAll(Collection<? extends > collectionToAdd) (Optional)
Ensures that the calling object contains all the elements in collectionToAdd . Returns true if the
calling object changed as a result of the call; returns false otherwise. Thus, if collectionToAdd
is a Set<T> , then the calling object is changed to the union of itself with collectionToAdd .
The List<T> interface has more method headings than the Collection<T> interface,
and some of the methods inherited from the Collection<T> interface receive somewhat
different semantics. For example, the semantics of adding new elements to the set allow
duplicates, and rules must be made about which element should be removed when there
are duplicates. These methods, along with new method definitions, are described in
Display 16.4 . A complete list of the List<T> interface is given in Appendix 5 .
Display 16.4
Selected Methods in the List<T> Interface (part 1 of 3)
The List<T> interface is in the java.util package.
The List<T> interface extends the Collection<T> interface.
ADDING AND REMOVING ELEMENTS
public boolean add(T element) (Optional)
Adds element to the end of the calling object's list. Normally returns true . Returns false if the
operation failed, but if the operation failed, something is seriously wrong and you will probably
get a run-time error anyway.
 
 
Search WWH ::




Custom Search