Game Development Reference
In-Depth Information
Interface Iterable<T>
> Interface Collection<E>
> Interface Set<E>
A Set<E> is an unordered Collection<E> and could also be thought of as a ran-
dom collection of objects in no particular order. A Set<E> collection may contains no
duplicate elements , and will throw an error, called an “exception,” if a duplicate ele-
ment is added to the Set<E> or if any “mutable” (elements than can change into
something else) element is changed into an element that duplicates another element
already in the Set<E>. This no duplicates rule also means that at the most, a Set<E>
can contain only one single null element. As all of you who are well versed in math-
ematics may have surmised already, this Set<E> interface is modeled after the math-
ematical set you learned about in school.
The Set<E> interface places additional stipulations beyond those that are “inher-
ited” from the Collection<E> super interface, on the “contracts” (requirements for all
of you non-legal types) for all constructor methods, as well as on the contracts (require-
ments) of the .add() , .equals() , and .hashCode() related methods.
The additional stipulations on these constructor methods is, according to rule, that
all constructors must create a Set<E> containing zero duplicate elements.
As mentioned earlier, you must be careful regarding what you are doing if mutable
(changeable) objects are used as elements in a Set<E> collection. The behavior of the
Set<E> is not specified if the value of an object is changed in a manner that affects the
.equals() method comparisons while the mutable object is an element in that Set<E>.
The special case of this prohibition is that it is not permissible for a Set<E> to contain
itself as an element, as a List<E> can.
The java.util HashSet Class: Using Unordered Sets of
Objects
Next, let's cover the public class HashSet<E> that is a member of the Java Collections
Framework as well. This class that provides a HashSet object container for the Set<E>
interface specification is similar to the way that the ArrayList<E> class creates an Ar-
rayList object container for the List<E> interface. The HashSet class can “ implement
or support the following Java Interfaces: Serializable, Cloneable, Iterable<E>, Collec-
tion<E>, and Set<E>. We will be using the Set<E>, or in our case, Set<Actor> inter-
face, in our CastingDirector.java class. The Set<E> class hierarchy is as follows:
Search WWH ::




Custom Search