Showing posts with label Collection FrameWork. Show all posts
Showing posts with label Collection FrameWork. Show all posts

Monday, 2 January 2012

Vector

Vector

                An Vector is like an array ,which can grow in memory dynamically.it means that we can store elements into the Vector,depending on the number of elements,the memory is dynamically allotted and re-allotted accommodate all the elements. 
  • Duplicate objects are allowed
  • Insertion order preserved
  • Heterogeneous objects are allowed 
  • Null insertion is possible
  •  Vector is  synchronized .this means that when more than one thread acts simultaneously on the Vector object,the results  will be reliable
    Vector constructor:

   Vector v=new Vector();   creates an empty Vector object with default initial capacity 10.

if Vector reaches max capacity then a new Vector object will be  with

                        new   capacity=(current capacity*2)

Vector v=new Vector(int initialcapacity);creates an empty Vector object with initial capacity

Vector v=new Vector(Collection c);creates an equivalent Vector object with the given collection.

Vector Example:
 
import java.util.Vector;
import java.util.Enumeration;

public class Edemo {
 public static void main(String[] args) {
    //create a Vector object
    Vector v = new Vector();
 
    //populate the Vector
    v.add("One");
    v.add("Two");
    v.add("Three");
    v.add("Four");
   /Get Enumeration of Vector's elements using elements() method
    Enumeration e = v.elements();
  

    /*
      Enumeration provides two methods to enumerate through the elements.
      It's "hasMoreElements()" method returns true if there are more elements to
      enumerate through otherwise it returns false. Its" nextElement() "method returns
      the next element in enumeration.
    */
  
    System.out.println("Elements of the Vector are : ");
  
    while(e.hasMoreElements()){
      System.out.println(e.nextElement());


}
 

}
}



Output would be
Elements of the Vector are :
One
Two
Three
Four

ArrayList

what is arraylist in java with suitable example
ArrayList 

                An ArrayList is like an array ,which can grow in memory dynamically.it means that we can store elements into the ArrayList,depending on the number of elements,the memory is dynamically allotted and re-allotted accommodate all the elements.what is Arraylist in java with suitable example, what is arraylist in java with  program,java arraylist program easy
  • Duplicate objects are allowed
  •   Insertion order preserved
  • Heterogeneous objects are allowed 
  • Null insertion is possible
  •  ArrayList is not synchronized .this means that when more than one thread acts simultaneously on the ArrayList object,the results may be incorrect in some cases.what is arraylist in java with suitable example  

ArrayList constructor

   ArrayList al=new ArrayList();   creates an empty ArrayList object with default initial capacity 10.

if ArrayList reaches max capacity then a new ArrayList object will be  with

                        new   capacity=(current capacity*3/2)+1

ArrayList al=new ArrayList(int initialcapacity);creates an empty ArrayList object with initial capacity

ArrayList al=new ArrayList(Collection c);   creates an equivalent ArrayList object with the given collection.

ArrayList Example
what is arraylist in java with suitable example
import java.util.ArrayList;

Class ArrayListDemo{

                  public static void main(String args[]) {   


      ArrayList al = new ArrayList();// constructs a new empty ArrayList  

                          al.add("a");   //to add an  element

                         al.add("10");

                         al.add("A");

                         al.add("a");

                         al.add(null);     //inserting null value

                         System.out.println("ArrayList object ");

                          System.out.println(al);   

                           al.remove(2);     //removing specific element

                           System.out.println("ArrayList after remove an element");

                           System.out.println(al);

                            al.add(2,"a");

                            System.out.println("ArrayList after adding"); 

                            System.out.println(al); 



}

}
output:Arraylist object
 [a,10,A,a,null]
ArrayList after removing
[a,10,a,null]
ArrayList after adding 
[a,10,a,a,null] 










Collection Framework

9 key Interfaces of collection framework:
                                              
  • Collection
  •  Set
  •  List 
  •  SortedSet
  • NavigableSet
  • Queue
  • Map
  • SortedMap 
  • NavigableMap
Collection:
               If we want  represent a group of    individual objects  as a single entity then we should go for Collections

In general we can consider Collection as root interface of entire Collection FrameWork

Collection interface defines the most common methods which can be applicable for any Collection object
List:
              It is the child interface of collection.If we want to represent a group of individual objects  as single entity where duplicates are allowed and insertion order must be preserved then we should  go for  List interface
Set:
     It is child Interface of Collection.If we want represent a group of individual  objects as single entity where duplicates are not allowed and insertion order is not preserved .then we should go for Set interface
SortedSet:

      It is child interface of set .if we want represent a group of unique objects according to some sorting order then we should go for SortedSet  .

NavigableSet:

     It is child Interface of SortedSet.It provide several methods for Navigation purpose.




Queue:
     
It is child Interface of Collection.If we want  represent a group of    individual objects  priority processing   then we should   go for Queue concepts

Map:
         Map is not child interface of Collection.If we want represent a group of objects key and value pairs then we should go for Map.

Duplicate keys are not allowed but values can be duplicated



SortedMap:
                  
                               It is child Interface of Map..If we want represent a group of objects key and value pairs according to some sorting order then we should go for Sorted Map.



NavigableMap:

                      It is child Interface of SortedMap.It provide several methods for Navigation purpose.
     



















































Sunday, 1 January 2012

Difference between Collections and Arrays

Arrays:
  • An Array is an indexed collection of fixed number of  homogeneous data elements.
  • The main advantage of Array is we can represent huge no of elements by using single variable.so that readability of code will be improved   

 Limitation of Object Arrays:  

Arrays are fixed in size i.e once we create an array  there is no chance of   increasing or  decreasing   the size based on our requirement . To use Array concept compulsory we should know the size advance.which may not possible always
         
               Arrays can hold homogeneous data elements.
EX:

                           Student[] s = new Student[20] ;

                           s[0]=new   Student();

                           s[1] =new Customer();//compile time error:In compatible types. 

                                                                  found:customer    
                                                                  required:student                        

         But we can resolve this problem by using Object type Arrays

                            Object[] obj=new Object[20];

                            obj[0]=new   Student();

                            obj[1] =new Customer();


Array concept not implemented based on some data structures.Hence ready made support methods we can't expect.  so every requirement we have to write the code explicitly.


COLLECTIONS:
  • If we want represent the group a group of objects as single entity then   we should go for collections.  
  • Collections can hold both  homogenous and heterogeneous objects. 
  •  Collections are growable in nature.i.e based on our requirement we can increase or decrease the size.Hence memory point of Collection concept is recommended to use.  
  • Every collection class implemented based in some data structures,hence every requirement ready made methods support is available.Programmer can use this methods directly without writing the functionality   on our own.      
  Difference  between Arrays and Collections:

           ARRAYS                                                             COLLECTIONS
1.  Arrays are fixed in size                                                      1. collections are growable in nature

2.Memory point of view Arrays are not recommended       2.Memory point of view        
    Collections                                                                               recommended.

                   
3.Performance point of view Arrays                                      3.Performance point of Collections    
   are are  recommended                                                              not recommended        

4.Arrays can hold homogenous data elements                      4.collections can hold both 
                                                                                                      homogenous and heterogeneous  
                                                                                                      elements




5.There is no underlying data structure for Arrays             5.Every collection class is
                                                                                                       implemented  based on some data                                                                                                            structure                                                                                                                                                                                       

6.Arrays can hold both primitive and objects types              6.Collections can hold only Objects