<< Chapter < Page | Chapter >> Page > |
Thus, the contract for the add method of an object of a class that implements the Set interface is more specialized than the contract for the add method of an object of a class that implements the Collection interface.
An additional stipulation on the constructor for a Set object is that all constructors must create a set that contains no duplicate elements.
The SortedSet interface extends the Set interface. The SortedSet interface contains the following stipulation that makes it more specialized than a Set .
"A set that further guarantees that its iterator will traverse the set in ascending element order, sorted according to the natural ordering of itselements (see Comparable ), or by a Comparator provided at sorted set creation time."
I'm going to finish this module with several questions in the form of a quiz. To ensure that this is a learning experience, I will provide an explanation inaddition to the answer for each question.
True or False? A collection that implements the List interface maintains its elements in ascending alphanumeric order.
The answer to question 1 is False. Unlike collections that implement the SortedSet interface, the order of the elements in a collection that implements the List interface is not based on the values of the objects referred to by the elements in the list.
True or False? A collection that implements the List interface is an unordered collection.
The answer to question 2 is also False. A collection that implements the List interface is an ordered collection (also known as a sequence) . According to Oracle, "The user of the interface has precise control over where in the list each element is inserted." Elements can be inserted and retrieved on the basis of their integer index (position in the list) using the following methods:
Valid index values are positive integers that begin with zero. When the add method is used to insert an element at a specific position in the sequence, the element currently at that position (if any) and any subsequent elements are shifted toward higher index values to make room for thenew element.
Another version of the add method takes a reference to an object as an incoming parameter and appends the specified element to the end of thecollection.
The get method simply returns the element at the specified position in the collection.
The List interface also declares various other methods that can be used to manipulate the contents of the collection.
True or False? A collection that implements the List interface is allowed to contain duplicate values.
The answer to question 3 is True. Unlike a collection that implements the Set interface, a collection that implements the List interface is typically allowed to contain duplicate values. More formally, accordingto Oracle, "lists typically allow pairs of elements e1 and e2 such that e1.equals(e2), and they typically allow multiple null elements if they allownull elements at all."
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?