Friday, April 9, 2010

Collections in Jav

Collections: Data structure that holds objects and provides various utility methods to
manage the data.
Collections hierarchy
Three basic flavors of collections:
„« Lists - Lists of things (classes that implement List)
„« Sets - Unique things (classes that implement Set)
„« Maps - Things with a unique ID (classes that implement Map)
The sub-flavors:
„« Ordered - You can iterate through the collection in a specific order.
„« Sorted - Collection is sorted in the natural order (alphabetical for Strings).
„« Unordered
„« Unsorted
3
. List extends Collection
In a List, every element has a index associated with it and we can add, delete, modify the
objects in a List using the index.
Let’s discuss about the 3 List implementations.
1) ArrayList
2) Vector
3) LinkedList
ľ ArrayList extends AbstractList
implements List, Cloneable, java.io.Serializable:
o Resizable-array implementation of the List interface.
o Ordered collection.
o Should be considered when there is more of data retrieval than add/delete.
o Often used methods – add(), get(), remove(), set(), size().
Implementation details:
ArrayList uses arrays internally for data management. Whenever we invoke
methods on the ArrayList object, it performs respective action on the array object to
reflect the changes.
Constructing an ArryList: There are 3 ways in which one can construct an ArrayList i.e.
ArrayList provides 3 constructors.
- Constructor that takes an integer argument – When the constructor with the
integer argument is invoked, a single dimension array (which the ArrayList uses
internally to manage the elements) is constructed with length equal to the value
given in the constructor.
- Default constructor – When the default constructor is invoked, it in turn calls the
constructor with integer arguments with the size as ‘10’.
- Constructor that takes a Collection as an argument. – Calculates the size of the
Collection passed as constructs an array of length (size*110)/100.

No comments:

Post a Comment