Tải bản đầy đủ (.pdf) (14 trang)

Sets

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (64.41 KB, 14 trang )

Chapter 8: Sets
Overview
The Set interface is the first type of Collection we'll discuss. This interface represents a group of elements
without duplicates. There is nothing in the interface definition that forces the group to not have duplicates; it
is the actual implementations that enforce this part of the definition. The elements of the group have no
perceived order, though specific implementations of the interface may place an order on those items.
Figure 8−1 shows the class hierarchy diagram for the Set interface, along with the base abstract
implementation, the concrete implementation classes, and other interfaces implemented by these classes.
Figure 8−1: The Set class hierarchy.
Set Basics
In Java, a set represents a collection of unique elements. Not only must these elements be unique, but while
they are in the set, each element must not be modified. While there is no programmatic restriction preventing
you from modifying elements in a set, if an element were to change, it could become forever lost in the set.
While not immediately obvious in Table 8−1, the Set interface definition is the same as the Collection
interface definition shown in Chapter 7.
Table 8−1: Summary of Interface Set
VARIABLE/METHOD NAME VERSION DESCRIPTION
add() 1.2 Adds an element to the set.
addAll() 1.2 Adds a collection of elements to the set.
clear() 1.2 Clears all elements from the set.
contains() 1.2 Checks if the set contains an element
containsAll() 1.2 Checks if the set contains a collection of elements.
equals() 1.2 Checks for equality with another object.
93
hashCode() 1.2 Returns a computed hash code for the set.
isEmpty() 1.2 Checks if the set is empty.
iterator() 1.2 Returns an object from the set that allows all of the
set's elements to be visited.
remove() 1.2 Clears a specific element from the set.
removeAll() 1.2 Clears a collection of elements from the set.
retainAll() 1.2 Removes all elements from the set not in another


collection.
size() 1.2 Returns the number of elements in the set.
toArray() 1.2 Returns the elements of the set as an array.
The Collection Framework provides two concrete set implementations: HashSet and TreeSet. The HashSet
represents a set backed by a hash table providing constant lookup−time access to unordered elements. On the
other hand, the TreeSet maintains its elements in an ordered fashion within a balanced tree. Since the Set
interface is identical to the Collection interface, we'll immediately jump into the concrete implementations.
HashSet Class
The Collection Framework introduces the HashSet collection. This implementation is backed by a hash table
(HashMap, actually) for storing unique elements. The backing hash table ensures that duplicate elements are
avoided as each element is stored and retrieved through its hash code, providing constant retrieval time.
Note For more on hash codes, see Chapter 5. The HashMap is the framework's replacement to the
historical Hashtable class. It, too, provides storage for key−value pairs. HashSet just stores a
dummy 'present' object as the value for every key.
Most of the HashSet functionality is provided through the AbstractCollection and AbstractSet superclasses,
which HashSet shares with TreeSet. As Table 8−2 shows, the HashSet class only needs to provide
constructors and customize eight of its methods.
Table 8−2: Summary of the HashSet Class
VARIABLE/METHOD NAME VERSION DESCRIPTION
HashSet() 1.2 Constructs a hash set.
add() 1.2 Adds an element to the set.
clear() 1.2 Removes all elements from the set.
clone() 1.2 Creates a clone of the set.
contains () 1.2 Checks if an object is in the set.
isEmpty() 1.2 Checks if the set has any elements.
iterator() 1.2 Returns an object from the set that allows all of the
set's elements to be visited.
remove() 1.2 Removes an element from the set.
size() 1.2 Returns the number of elements in the set.
HashSet Class

94
While only eight methods are shown here, we should look at all those defined within the Set interface. These
methods can, again, like Collection, be broken down into eight groups with HashSet adding a ninth for its
constructors. The methods of the eight groups allow you to add and remove elements as well as perform
operations on the set as a whole.
Creating a HashSet
The HashSet class provides four constructors broken into two sets. The first three constructors create empty
sets of varying sizes:
public HashSet()
public HashSet(int initialCapacity)
public HashSet(int initialCapacity, int loadFactor)
If unspecified, the initial set size for storing elements will be the default size of a HashMap, which happens to
be eleven or 101, depending upon what Java version you are using. When the set capacity reaches full and a
new element is added, the internal structure will double in size before adding the new element (and copying in
the old elements). If you don't like the 75%−full aspect, you can provide the constructor with a custom load
factor.
Tip When creating any collection, it is always best to have the local variable be of the interface type, as in Set
set = new HashSet(). That way, if you later decide to change the set to a TreeSet or some other set
implementation, you won't have to change any code as you'll only be using the methods of the Set
interface.
The fourth constructor acts as a copy constructor, copying the elements from one set into the newly created
set:
public HashSet(Collection col)
You cannot provide a custom initial capacity or load factor. Instead, the internal map will be sized at twice the
size of the collection, or eleven if the collection is small (five or less elements), keeping the default load factor
of 75%.
Note If the original collection had duplicates, only one of the duplicates will be in the final created set.
An easy way to initialize a set without manually adding each element is to create an array of the elements,
create a List from that array with Arrays.asList(), then call this constructor with the list as the collection:
String elements[] = {"Irish Setter", "Poodle", "English Setter",

"Gordon Setter", "Pug"};
Set set = new HashSet(Arrays.asList(elements));
Adding Elements
When you need to add elements to a set, you can either add a single element or a group of elements.
Adding Single Elements
To add a single element, call the add() method:
public boolean add(Object element)
Creating a HashSet
95
The add() method takes a single argument of the element to add. If the element is not in the set, it is added and
true is returned. If the element happens to be in the set already, because element.equals(oldElement) returns
true (for some element in the set), then the new element replaces the old element in the collection and false is
returned. If the old element has no other references, it becomes eligible for garbage collection. See Figure 8−2
to help you visualize this replacement scenario.
Figure 8−2: Adding a contained element to a set.
If you are working with a set that was made to be read−only, or if adding elements is not supported, then an
UnsupportedOperationException will be thrown.
Tip If you need to modify an element in a set, you should remove it, modify it, and then re−add it. If you
don't, you can consider the object lost as there is no way of finding the object without manually digging
through all the objects in the set. This is true when the change affects the results of hashCode(). If the
change doesn't affect the method results, the change can be made. However, you should then question
why the hashCode() results didn't change.
Adding Another Collection
You can add a group of elements from another collection to the set with the addAll() method:
public boolean addAll(Collection c)
Each element in the collection passed in will be added to the current set via the equivalent of calling the add()
method on each element. If the underlying set changes, true is returned. If no elements are added, false is
returned. As with add(), if equal elements are in both sets, true is returned with the new element replacing the
old element in the set.
An UnsupportedOperationException will be thrown when working with a set that doesn't support adding

elements.
Removing Elements
You can remove elements from a set in four different ways.
Removing All Elements
The simplest removal method, clear(), clears all of the elements from the set:
public void clear()
Adding Elements
96
While there is no return value, you can still get an UnsupportedOperationException thrown when working
with a read−only set.
Removing Single Elements
Use the remove() method if you wish to remove a single element at a time:
public boolean remove(Object element)
Determining whether the element is in the set is done via the equals() method of the element. If the element is
found, the element is removed from the set and true is returned. If not found, false is returned. If removal is
not supported, you'll get an UnsupportedOperationException thrown whether the element is present or not.
Removing Another Collection
The third way to remove elements is with removeAll():
public boolean removeAll(Collection c)
The removeAll() method takes a Collection as an argument and removes from the set all instances of each
element in the Collection passed in. The Collection passed in can be a Set or some other Collection
implementation. For instance, if the original set consisted of the following elements:
{"Irish Setter", "Poodle", "English Setter", "Gordon Setter", "Pug"}
and the collection passed in was
{"Poodle", "Pug", "Poodle", "Pug", "Poodle", "Pug", "Pug", "Pug"}
the resulting set would have every instance of "Poodle" and "Pug" removed. Since a set cannot have
duplicates, it would remove one for each:
{"Irish Setter", "English Setter", "Gordon Setter"}
As with most of the previously shown set methods, removeAll() returns true if the underlying set changed,
and false or an UnsupportedOperationException, otherwise. And again, the equals() method is used to check

for element equality.
Retaining Another Collection
The retainAll() method works like removeAll(), but in the opposite direction:
public boolean retainAll(Collection c)
In other words, only those elements within the collection argument are kept in the original set. Everything else
is removed, instead.
Figure 8−3 should help you visualize the difference between removeAll() and retainAll(). The contents of the
starting set are the five dogs listed previously (Irish Setter, Poodle, English Setter, Gordon Setter, Pug). The
acting collection consists of the elements Pug, Poodle, and Samoyed. The Samoyed element is shown to
demonstrate that in neither case will this be added to the original collection. Remember that sets are
Removing Elements
97

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×