28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10 ■ Directing the Cast of Actors: Creating a Casting Director Engine and Creating the Bagel Actor Class<br />

Finally, the List interface provides three methods that manipulate the entire List using a single method. Since<br />

we are using the List to manage all of the Actor objects currently in the scene, we will primarily be using these methods,<br />

which were mentioned earlier in the ArrayList section of the chapter and include .addAll(), .removeAll(), and .clear().<br />

We will also be using the .add(E element) method to add a single Actor object to our CURRENT_CAST List.<br />

Finally, while it is technically permissible for a List to contain itself as an element, this is not viewed as being<br />

a “good” programming practice, so I do not recommend doing this. You should use extreme caution if you are going to<br />

try doing this, because the equals and hashCode methods will no longer be “well defined” in such a List.<br />

Set and HashSet: Using java.util Unordered Sets<br />

The Set public interface is also a member of the <strong>Java</strong> Collections Framework. The <strong>Java</strong> public interface Set<br />

extends the Collections public interface, which extends the Iterable public interface. Thus, the super interface<br />

to sub interface hierarchy for Set is the same as it is for List and looks like the following interface hierarchy:<br />

Interface Iterable<br />

> Interface Collection<br />

> Interface Set<br />

A Set is an unordered Collection and could also be thought of as a random collection of objects in<br />

no particular order. A Set collection may contains no duplicate elements, and will throw an error, called an<br />

“exception,” if a duplicate element is added to the Set or if any “mutable” (elements than can change into<br />

something else) element is changed into an element that duplicates another element already in the Set. This no<br />

duplicates rule also means that at the most, a Set can contain only one single null element. As all of you who are<br />

well versed in mathematics may have surmised already, this Set interface is modeled after the mathematical set<br />

you learned about in school.<br />

The Set interface places additional stipulations beyond those that are “inherited” from the Collection<br />

super interface, on the “contracts” (requirements for all of you non-legal types) for all constructor methods, as well as<br />

on the contracts (requirements) of the .add(), .equals(), and .hashCode() related methods.<br />

The additional stipulations on these constructor methods is, according to rule, that all constructors must create a<br />

Set containing zero duplicate elements.<br />

As mentioned earlier, you must be careful regarding what you are doing if mutable (changeable) objects are used<br />

as elements in a Set collection. The behavior of the Set is not specified if the value of an object is changed in<br />

a manner that affects the .equals() method comparisons while the mutable object is an element in that Set. The<br />

special case of this prohibition is that it is not permissible for a Set to contain itself as an element, as a List can.<br />

The java.util HashSet Class: Using Unordered Sets of Objects<br />

Next, let’s cover the public class HashSet that is a member of the <strong>Java</strong> Collections Framework as well. This<br />

class that provides a HashSet object container for the Set interface specification is similar to the way that the<br />

ArrayList class creates an ArrayList object container for the List interface. The HashSet class can “implement”<br />

or support the following <strong>Java</strong> Interfaces: Serializable, Cloneable, Iterable, Collection, and Set. We will be<br />

using the Set, or in our case, Set interface, in our CastingDirector.java class. The Set class hierarchy is<br />

as follows:<br />

java.lang.Object<br />

> java.util.AbstractCollection<br />

> java.util.AbstractSet<br />

> java.util.HashSet<br />

212<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!