Collection Framework
Collection Framework
Collection Framework
Queue:- special type of list where element is removed only from the from the head(FIFO)
Set: - collection of unique elements
List : - to handle sequence (list of objects) use index to access the elements
Dequeue extends the queue interface
Sorted set extends the set interface : - collection of sets which is sorted
Navigable set extends the sorted set interface: - retrieval of elements is based upon the
closest match search
Collections method:-
add:- it will take an object as a parameter and returns true if object is added to the collection
otherwise false
addAll: - it will take collection as a parameter and returns true if all the object of collection is
added to the collection otherwise false
remove: - it will take an object as a parameter and returns true if one instance of object is
removed from the collection otherwise false
removeAll: - it will take a collection as a parameter and returns true if all the elements of the
collection are removed from the invoking collection otherwise false
retainAll: - - it will take a collection as a parameter and remove all the elements from the
invoking collection except the elements in parameter collection. Returns True if collection
changed otherwise false
contains: - take object as a parameter and return true if object is the part of the collection
containsAll:- take collection as a parameter and return true if all the elements of the
collection are part of the invoking collection
isEmpty: - returns True if the invoking collection is empty
equals:- take object as a parameter and returns true if invoking collection and object are
equal
size
clear : - remove all elements from the collection
iterator: - returns an iterator for the invoking collection
toArray: - returns an array that contains all the elements of the collection
List
The List interface extends Collection and declares the behavior of a collection that stores
a sequence of elements. Elements can be inserted or accessed by their position in the list,
using a zero-based index. A list may contain duplicate elements
List Methods
List used the add and addAll method of collection framework to add the elements at the end
of the list
Void add(index, element) :- insert the element at the specified index. Any pre-existing
element beyond the point of insertion are shifted up, so no elements are overwritten
Bollean addAll(index,collection): - Inserts all elements of c into the invoking list at the index
passed in index. Any pre-existing elements at or beyond the point of insertion are shifted up.
Thus, no elements are overwritten. Returns true if the invoking list changes and returns false
otherwise
get(index): - return the element which is at the specified index
E set(index, object): - assign the object at the specified index and returns the old value
indexof(object obj): - return the index of the first instance of object obj in the list, if obj is
not the member of the list -1 is returned
lastindexof(object obj): - return the index of the last instance of object obj in the list, if obj is
not the member of the list -1 is returned
E remove(index): - Removes the element at position index from the invoking list and returns
the deleted element. The resulting list is compacted. That is, the indexes of subsequent
elements are decremented by one
ListIterator():- return the list iterator to the start of the invoking list
listIterator(int index): - return the list iterator that start with the specified index
sublist(startindex, endindex): - returns a list that includes elements from startindex to
endindex-1.
sort(comparator): - Sort the list using the comparator specified in the parameter(added in
java 8)
Set
The Set interface defines a set. It extends Collection and specifies the behaviour of a collection that
does not allow duplicate elements. Therefore, the add( ) method returns false if an attempt is made
to add duplicate elements to a set. It does not specify any additional methods of its own.
In the set we can get the elements using iterator only. As set doesn’t have any additional method
and collection interface defines no method to get the object apart from using iterator
Sorted Set
The SortedSet interface extends Set and declares the behaviour of a set sorted in ascending order.
Navigable Set
The NavigableSet interface extends SortedSet and declares the behaviour of a collection that
supports the retrieval of elements based on the closest match to a given value or values
NavigableSet methods
E Ceiling(E obj): - returns the smallest element e such that e >= obj, otherwise return Null
E Floor(E obj) : - returns the largest element e such that e <= obj, otherwise return Null
descedingIterator: - Returns an iterator that moves from the greatest to least. In other
words, it returns a reverse iterator.
descendingSet( ): - Returns a NavigableSet that is the reverse of the invoking set
headset(upperbound, boolean incl): - it will return a navigable set that contains element
from start to less than upperbound. Value of upperbound is included or not is depend upon
the incl Boolean parameter.
tailSet(lowerbound, boolean incl): - it will return a navigable set that contains element from
lowerbound to end. Value of lowerbound is included or not is depend upon the incl Boolean
parameter.
lower
higher
subset
pollFirst
polllast
Queue Interface
The Queue interface extends Collection and declares the behaviour of a queue, which is often a first-
in, first-out list. However, there are types of queues in which the ordering is based upon other
criteria.
Queue methods:-
offer(E obj): - Attempts to add obj to the queue. Returns true if obj was added and false
otherwise. If fixed length queue is there and queue is full, this method fail and return false.
element():- return the element but doesn’t remove it. throws NoseuchElementException if
queue is empty
peek(): - return the element but doesn’t remove it. . Return Null if queue is empty
poll():- return the element and remove the element in the process. Return Null if queue is
empty
remove(): - return the element and remove the element in the process. throws
NoseuchElementException if queue is empty
Deque Interface: -
The Deque interface extends Queue and declares the behaviour of a double-ended queue. Double-
ended queues can function as standard, first-in, first-out queues or as last-in, first-out stacks.
offerFirst
offerlast
addFirst
addLast
All 4 methods add the element either at the head of the queue or at the tail of the deque. offerFirst
and offerLast method return false if deque is full. addFirst and addlast method throws
illegalStateException if deque is full.
getFirst
getLast
peekFirst
peeklast
All 4 methods obtain the element from the head or tail of the deque. peekfirst and peeklast method
returns null if deque is empty. getFirst and getLast method returns noSuchElementException if
Deque is empty
pollFirst
polllast
removeFirst
removeLast
All 4 methods obtain the element from the head or tail of the deque and remove the element in the
process. pollfirst and polllast method returns null if deque is empty. removeFirst and removeLast
method returns noSuchElementException if Deque is empty.
Pop: - obtain the element from the head of the deque and remove the element in the
process. Returns noSuchElementException if Deque is empty.
So pop and removeFirst