Thursday, 12 March 2015

Collection API (Core JAVA)

·         It is group of java classes and interfaces. It is used for storing and managing dissimilar data
LIST
·         List is a type of collection
·         List elements are stored in index based
·         List is a auto index collection
·         List allows duplicate values
·         List allows null values
·         The elements of list are referred through index number. For e.g. Question paper
QUEUE
·         Queue is a type of collection
·         In queue elements are stored without index (it is not a index collection)
·         Queue is implemented in FIFO (First IN First Out)manner
·         Queue allows duplicate elements
·         Queue doesn’t allow null values. For e.g. Ticket Queue
SET
·         Set is type of collection
·         Set is not a index collection
·         Set doesn’t allows duplicate elements
·         Set allows null values. For e.g. Currency Collection
MAP
·         MAP elements are stored in form of key and value pair, where key should be unique  and value can be duplicated
·         Value can be referred using key. For e.g. Key and Lock
·         Each MAP type classes provide 2 methods
(1)    Keyset
(2)    Values
·         Keyset method returns set of keys whereas values method returns the collection of values
PRIORITY QUEUE
·         In Priority queue whenever an element is added, it compares and sort with the existing elements
·         Priority queue is auto sorted queue
·         Whenever we display element in priority queue, the elements are displayed in random manner
·         Poll is method of priority queue, which will remove topmost element of queue
·         Whenever Poll method is executed, the queue size gets reduced by 1
·         Peak method of priority queue returns the topmost element of the queue
COLLECTIONS
·         “Collections“class contains several methods which can be operated on any type f collection for e.g. sort is a static method which is used to sort the element of collection. This method stores the sorted element back in the same collection type
ITERATOR
·         Collection API provides iterator to iterate each element of any collection type.
·         Iterator is an inter face which provides three important methods:-
1.       next():- this method is used to navigate to the next element and return the value of next element. if there is no next element the method returns an exception “no such element exception”
2.       hashNext():- it is used to check weather next element exist or not. If element exist return true else false
3.       remove(): it removes the element from the collection. Once element is removed, the previous element should point to the element which exist after the removed element
·         Using iterator object we can iterates only once the element of collection.
·         This iterator can be used to iterate list, queue or set type of collection.
·          Java collection also provides list iterator which can be used only list type of collection. this iterator provides two additional methods :
1.       previous(): it moves iterator to the previous element and return element value.
2.       hashprevious(): it returns true or false based on the existence of previous element.
NOTE:- both iterator and list iterator cannot be used for map

·         Whenever element of collection are read from any collection type ,it will be in object form(object type),it should be downcasted to respective class to access sub class members

No comments:

Post a Comment