Show Mobile Navigation

Webring

Powered by WebRing.

Collections framework in Java :complete guide-part-1

Sachin R Kukale - 20:40
Legacy classes have some limitations as:
ü  No specific scenarios or conditions are defined for the use of particular legacy class.
ü  Most of the methods in legacy classes are defined as Serialized which makes it impossible for members of legacy classes to be accessed by multiple threads simultaneously.
These are some serious limitations of the legacy classes and these limitations restrict the use of such legacy classes in most real time scenarios. To overcome these limitations Java vendor introduced the collections framework from Java2 version.
Collections framework in java is the set of classes, methods and interfaces which makes a task of storing, searching, and manipulating the group of objects.
In java collections is referred to as the group of objects.
In collections framework in java you will find following:
·         Interfaces.
·         Classes.
·         Methods.
You will find following super interfaces under the collections framework in java:
·         Collection: Collection interface and its subclasses are used to store the collections of objects without any key-value format.
·         Map: Map interface and its subclasses are used to store the collections of objects with key-value format. So there will be one key at least for every value in this type.
·         Iterator: Iterator interface is mainly used to access the members of collections framework.
You will also find following sub-interfaces in collections framework:
·         List->>>>Abstract List->>>ArrayList,LinkedList,Vector.
·         Set->>>>AbstractSet->>>HashSet->>>LinkedHashSet.
     ->>>>SortedSet->>>TreeSet.
·         Queue. (Introduced from Java5.)


Collections Framework_2
List
List is interface which extends java.util.Collections which is another interface. List interface is available in java.util package.
Ø  All the subclasses of ‘List’ allow duplicate elements to be inserted in it.
Ø  All the subclasses of ‘List’ also maintains the order of elements in which they are inserted.
You will find following classes in ‘List’ interface:
        i.            ArrayList: ArrayList mostly used subclass in ‘List’ interface. It is implemented as resizable array internally. ‘ArrayList’ allows duplicate elements. It is fast to search elements from ‘ArrayList’ but inserting and deleting element from list is slow process.
      ii.            LinkedList: Internally ‘LinkedList’ implements doubly linked list and it also allows duplicate elements in it. Unlike ‘ArrayList’ it is slow to search an element from ‘LinkedList’  but inserting and deleting elements from ‘LinkedList’ is fast process.
    iii.            Vector: It is the list used subclass in ‘List’ interface. Vector data structure alos allows duplicate elements in it. Methods associated with vector sub-class are all synchronized methods and so elements of vector can not be accessed by multiple threads simultaneously. Elements in the vector will be stores using indexing notation internally.
Some important methods of ‘List’ interface:
        i.            Object get(int index): This method returns the elements at location, specified by ‘index’.
      ii.            Object remove(int index): This method removes the elements at location, specified by ‘index’.
    iii.            void add(int index,Object obj): This adds/inserts elements at location specified by ‘index’.
     iv.            int IndexOf(Object obj): This method returns the element at the first position of specified object.
       v.            int lastIndexOf(Object obj): This method returns the element at the last position of specified object.
     vi.            boolean addAll(int index,Collection col): This method adds the all elements in collection specified by ‘col’, at specified index.
   vii.            List subList(int startIndex,int endindex): This used to create the list from already available list. It returns the all the elements from location ‘startindex’ to ‘endindex’ in a new sub list.
 viii.            ListIterator listIteratir(): This method is used to return the  Iterator object pointing to the first records.
     ix.            ListIterator listIterator(int startindex): This method is used to return the  Iterator object pointing to the record at location specified by the index.
       x.            Object set(int index,Object obj): This method is used to replace the method at the specified location and location is specified by ‘index’.


Iterator Interface
Iterator interface is used to access the objects from collection. You can not modify the elements of Object while accessing those elements from a collection, otherwise JVM will throw a exception:
java. util.ConcurrentModificationException.
You can use methods described above to modify or remove the elements in collection.
ListIterator Interface
‘ListIterator’ is interface which is subtype of ‘Iterator’ interface. It can be used with only ‘List’ types of collection and you can not use ‘ListIterator’ interface with other collection types like Map,Set.
This interface provides some extra methods to manage the elements from list type collection only. You can perform following tasks using ListIterator interface with Lists:
ü  Accessing the elements in order in which they are inserted.
ü  Accessing the elements in forward direction.
ü  Accessing the elements in backward direction.
ü  Removing and replacing the elements from list from specified location.
ü  Accessing the elements from location specified by index.



0 comments:

Post a Comment