Interfaces of Collection Framework
The collection framework has a lot of Interfaces, setting the fundamental nature of various collection classes. Lets study the most important Interfaces in the collection framework.
The Collection Interface
- It is at the top of collection heirarchy and must be implemented by any class that defines a collection. Its general declaration is,
interface Collection < E >
- Following are some of the commonly used methods in this interface.
The List Interface
- It extends the Collection Interface, and defines storage as sequence of elements. Following is its general declaration,
interface List < E >
- Allows random access and insertion, based on position.
- It allows Duplicate elements.
- Apart from methods of Collection Interface, it adds following methods of its own.
Methods Description get( int index ) Returns object stored at the specified index set( int index, E obj) Stores object at the specified index in the calling collection indexOf( Object obj ) Returns index of first occurence of obj in the collection lastIndexOf( Object obj ) Returns index of last occurence of obj in the collection subList( int start, int end ) Returns a list containing elements between start and end index in the collection
The Set Interface
- This interface defines a Set. It extends Collection interface and doesn't allow insertion of duplicate elements. It's general declaration is,
interface Set < E >
- It doesn't define any method of its own. It has two sub interfaces, SortedSet and NavigableSet.
- SortedSet interface extends Set interface and arranges added elements in an ascending order.
- NavigabeSet interface extends SortedSet interface, and allows retrieval of elements based on the closest match to a given value or values.
The Queue Interface
- It extends collection interface and defines behaviour of queue, that is first-in, first-out. It's general declaration is,
interface Queue < E >
- There are couple of new and interestin methods added by this interface. Some of them are mentioned in below table.
Methods Description poll() removes element at the head of the queue and returns null if queue is empty remove() removes element at the head of the queue and throws NoSuchElementException if queue is empty peek() returns the element at the head of the queue without removing it. Returns null if queue is empty element() same as peek(), but throws NoSuchElementException if queue is empty offer( E obj ) Adds object to queue.
The Dequeue Interface
- It extends Queue interface and declares behaviour of a double-ended queue. Its general declaration is,
interface Dequeue < E >
- Double ended queues can function as simple queues as well as like standard Stacks.
No comments:
Post a Comment