Java Reference
In-Depth Information
* team.
Set getRoster();
* Add a player to the team.
void addPlayer(Player toAdd);
* Remove a player from the team.
void removePlayer(Player toRemove);
Having changed the name of the method that gets us the Player objects that are on the team
and the return type for that method, we now need to change our TeamImpl to reflect the change
in the interface. If we check the javadoc for the Set interface, we will find a number of classes
that implement that interface. Most of these implementations are for special sets of a partic-
ular type, but the HashSet will do nicely for our uses. As the name implies, a HashSet im-
plements the Set interface using a hash table as a backing store. The nice thing is, we don't
have to care. All we want is an implementation of the Set interface that we can use, and one
is supplied. So we can change our TeamImpl code to something like:
package org.oreilly.javaGoodParts.examples.impl;
import java.util.HashSet;
import java.util.Set;
import org.oreilly.javaGoodParts.examples.statistics.Player;
import org.oreilly.javaGoodParts.examples.statistics.Team;
* A second implementation of the Team interface, using
* a HashSet as the backing store for the Players on
* the team, after the interface has been changed to
* return a Set from the getPlayers() method.
public class TeamImpl implements Team {
private String name;
private HashSet players = new HashSet();
Search WWH ::

Custom Search