<< Chapter < Page | Chapter >> Page > |
While you will ultimately need to understand how to use Generics, that is a very complex topic. An understanding of Generics is beyond the scope of thiscourse. Therefore, for purposes of this course, you can simply ignore those warnings.
I am going to begin this module with a quiz just to make sure that you are still awake. Is the following statement Trueor False?
The TreeSet class is a direct implementation of the Collection interface.
The answer is False.
The TreeSet class is not a direct implementation of the Collection interface. Rather, the TreeSet class is a direct implementation of the SortedSet interface. The SortedSet interface extends the Set interface, and the Set interface extends the Collection interface.
The interface hierarchy for the Java Collections Framework is shown below:
As you learned in an earlier module, the Java Collections Framework doesn't have a single root. As shown above, there are two distinct trees in theframework -- The Collection hierarchy and the Map hierarchy.
The Collection interface is the root of the collection hierarchy. The Java Collections Framework doesn't provide any direct implementations of the Collection interface. All of the implementations of the interfaces in the Collection hierarchy implement one of the sub-interfaces of the Collection interface.
Here is what the Oracle documentation has to say on the topic of the Collection interface:
"The SDK does not provide any direct implementations of this interface: it provides implementations of more specific sub-interfaces like Set and List . This interface is typically used to pass collections around and manipulate them wheremaximum generality is desired."
The Oracle documentation also states:
"Bags or multisets (unordered collections that may contain duplicate elements) should implement this interface directly."
Some implementations of Collection allow duplicate elements, and others do not. Implementations of the List interface (such as ArrayList ) allow duplicate elements. Implements of Set and SortedSet (such as TreeSet ) do not allow duplicate elements. This was illustrated in an earlier module.
A sample program in that earlier module created two collection objects and applied the polymorphic add method to add the same elements to each collection. One of the collection objects was of type ArrayList , and the other collection object was of type TreeSet . The elements added to each collection contained one pair of duplicate elements. The duplicate elementwas automatically excluded from the TreeSet object, but was retained in the ArrayList object.
According to Oracle, a Set is a "collection that contains no duplicate elements ... this interface models the mathematical set abstraction."
An object of type Set is typically used to model collections such as Social Security numbers, where duplicates are not allowed.
Also according to Oracle, a List is "An ordered collection (also known as a sequence). The user of this interface has precise control over where in thelist each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list."
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?