Monday, 6 November 2017

Java Collection Framework



Collection framework represents group of classes and interfaces which can be used to represent a group of similar and non-similar type of object as a single entity and provides readymade methods support to store and manipulate collection objects.
                    Collection API is present in java.util package. Collection’s subclasses are used to manage the group of elements without key and value pair.   I -Interface c--class   Methods of Collection interface public boolean add(Object ob):   This method is used to add an object to collection object and List object. This method returns true when we add elements for Collection and List object because they allow  duplicate elements.
This method returns false when we add elements for Set and SortedSet object if the new element is  duplicate element. java/j2ee classes bangalore

Set and SortedSet object does not allow duplicates. public boolean addAll(Collection c):   This method is used to add all the elements of specified collection object at the end of current  collection because insertion order is preserved in case of collection and List. This method returns true when we add elements for Collection and List object because they allow  duplicate elements.
This method returns false when we add elements for Set and SortedSet object if the new element is  duplicate element.Set and SortedSet object does not allow duplicates.
  public int size(): This methods returns the number of elements present in Collection object.   public boolean isEmpty(): This method checks whether collection is empty or not. It returns true when Collection object is  empty otherwise return false.
public boolean contains(object ob): This method checks whether given object is present in collection or not.   public boolean containsAll(Collection c): This method checks whether current collection contains all the elements of specified collection.   ppublic boolean remove(Object ob): This method is used to remove first existence of specified object from collection.
  public boolean removeAll(Collection c): This method is used to remove similar elements of specified collection from current collection.   public boolean retainAll(Collection c): This method is used to remove dissimilar elements of specified collection from current collection.   public boolean clear(): This method is used to clear all the elements from collection.  public object[] toArray(): This method is used to convert collection objects into array elements. 
public boolean equals(Object obj): This method is used to compare two collection objects.  public Iterator iterator(): This method returns Iterator object that points before the first record.  Iterator interface: Iterator interface provides the facility for traversing the collection objects in forward direction.  Iterator interface has three methods.
   public boolean hasNext(): This method is used to check whether next element is available or not in the collection object. If next element present in the collection object then it returns true otherwise it returns false.   public Object next(): This method is used to retrieve the next element in the collection object.   public void remove(): This method is used to remove the current element from collection object. List  List interface is available in java.util package and extends Collection interface. Best core java training in Bangalore

It provides the following functionalities to sub classes. The elements are stored based on index. We can access any element directly based on specified index. List subclasses allows duplicates and null values. Reverse direction traversing is possible. We can insert elements either at the end of List object or at specific index.   Commonly used methods of List interface: public object get(int index): This method is used to get element from the specified index.  public object set(int index, Object element):    This method is used to replace the existing element from List with given element from specified index. 
 public void add(int index,Object element):  This method is used to insert the element at specified index.  public void addAll(int index,Collection c):  This method is used to insert the elements of specified collection object to current object at specified index.  public void remove(int index):  This method is used to remove the element at specified index.  public int indexOf(Object element):  This method is used to return the first occurrence of specified element. 
public int lastIndexOf(Object element):  This method is used to return the last occurrence of specified element.  public ListIterator listIterator():  This method is used for accessing the data from List object either in forward or in backward.   public ListIterator listIterator(int index):  This method is used for accessing the data from List object that points to specified index. 
 ListIterator interface: ListIterator interface provides the facility for traversing the List objects either in forward or in  backward direction.    ListIterator interface has following methods public boolean hasNext():  This method is used to check whether next element is available or not in the List object.  If next element present in the List then it returns true otherwise it returns false.   public Object next(): This method is used to retrieve the next element in the collection object. 
 public boolean hasPrevious(): This method is used to check whether previous element is available or not in the List object.  If previous element present in the List then it returns true otherwise it returns false.  public Object previous(): This method is used to retrieve the previous element in the collection object. 

No comments:

Post a Comment