<< 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.

Stipulations on SortedSet

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."

We will end with a quiz

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.

Question 1

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.

Question 2

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:

  • add(int index, Object element)
  • get(int index)

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.

Question 3

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."

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask