Collection Interfaces:
- Collection
- Set
- SortedSet
- NavigableSet
- List
- Queue
- BlockingQueue
- TransferQueue
- Deque
- Map
- SortedMap
- NavigableMap
- ConcurrentMap
- ConcurrentNavigableMap
Main interfaces:
- Collection:
- Root interface in Collection hierarchy. It represents group of objects. Java does not provide direct implementation of this interface. It provides implementation of more specific interfaces by extending this interface like Set, List.
- This interface defines basic operations for group of objects.
- There are also some methods which are tagged as optional operations which may not be applicable for specific type of collection. E.g. For read only collections, add(E), remove(Object) methods are not required, so implementation classes may just throw UnsupportedOperationException in method implementation. If the invocation would have no effect on the collection, then it may or may not throw exception.
- Set:
- Collection that contains no duplicate elements and at most one null element. (some implementations does not allow null)
- You have to be careful when mutable objects are added as elements of set. Set behaviour is not specified if value of object which is in set is changed which affects equals comparison.
- Set provides additional constraint on add method to prevent duplicates.
- List:
- List is ordered collection. List may contain duplicates.
- List provides special iterator, called ListIterator, which allows element insertion, bidirectional access.
- List provides additional constraint over iterator, add, remove methods to maintain sequence of elements.
- Queue:
- Queue typically, but not necessarily, order elements in FIFO manner. Among the exceptions are priority queues, which order elements according to a supplied comparator or the elements' natural ordering, and LIFO queues (or stacks) which order the elements LIFO (last-in-first-out)
- Queue provides additional methods for insertion, removal, inspection operations which returns special value (null or false) if operation fails. Collection methods add, remove, element throws exception if method fails.
- Queue generally does not allow null. (Except LinkedList). Null should not be inserted into queue because null is returned by poll method to indicate that queue is empty.
- Map:
- Object that contains key-value pair. Map interface does not extends Collection.
- Map cannot contain duplicate key. So special care should be taken when mutable objects are used as keys.
Additional Interfaces:
- SortedSet:
- Ordered collection.
- Elements are by default in natural order or as per Comparator provided at creation time.
- All elements inserted into a sorted set must implement the Comparable interface.
- General implementation of SortedSet should provide 4 standard constructors.
- Void constructor with no parameter
- Constructor with parameter as Comparator
- Constructor with parameter as Collection
- Constructor with parameter as SortedSet
- NavigableSet:
- It extends SortedSet and provides navigation methods to find closest match for given target.
- Navigation methods are lower, floor, ceiling, higher.
- pollFirst and pollLast methods that return and remove the lowest and highest element, if one exists, else returning null.
- Deque:
- Double ended queue. Extends Queue.
- Element insertion and removal at both ends.
- Interface defines methods (add, remove, get) to access elements at both end of queue. All methods have 2 forms. One throws exception if operation fails and other returns special value (null or false).
- BlockingQueue:
- Interface of concurrent package. Use for Producer-Consumer type operation.
- While inserting element in queue, it waits if for space to be available when queue is full.
- While retrieving element from queue, it waits if queue is empty.
No comments:
Post a Comment