Java Reference
In-Depth Information
the
get
method), then use the
ArrayList<T>
or
Vector<T>
class or a class derived from
one of these two classes. If you do not need efficient random access but need to efficiently
move sequentially through the list, then use the
LinkedList<T>
class or a class derived
from the
LinkedList<T>
class.
The interface
SortedSet<T>
and the concrete class
TreeSet<T>
are designed for
implementations of the
Set<T>
interface that provide for rapid retrieval of elements (efficient
implementation of the
contains
and similar methods). The implementation of the class is
similar to the binary tree class discussed in Chapter 15 but with more sophisticated ways to
do inserting that keep the tree balanced. We will not discuss the
SortedSet<T>
interface
or the
TreeSet<T>
class in this text, but you should be aware of their existence so you
know what to look for in the Java documentation should you need them.
SortedSet<T>
TreeSet<T>
Self-Test Exercises
4. Can an object that instantiates the
HashSet<T>
class contain multiple copies of
some element?
5. Suppose you want to define a class that orders its elements like a
List<T>
but
does not allow multiple occurrences of an element like a
Set<T>
. Would it be
better to make it a derived class of the
ArrayList<T>
class or a derived class of
the
HashSet<T>
class?
6. You would like to use the following class as the type in a
HashSet<T>
collection.
What is missing and how would you fix it?
public class Customer
{
private String name;
private String address;
public Customer(String newName, String newAddress)
{
name = newName;
address = newAddress;
}
public String toString()
{
return name + " : " + address;
}
}