Collection framework


 It is introduced in java 2 before to that there are five legacy classes and one legacy interface
               Vector
               HashTable
               Properties             legacy classes                 Eenumeration                 legacy interface
               Dictionary
               Stack
 There are some problem in above listed legacy classes to avoid these problem Sun has introduced collection framework in Java 2.

In the collection frame work there are the three different category:--------
List :----- collection of object with duplicate.
Set :----- collection of object without duplicate.
Map :--- collection of key value pairs

List interface three concrete subclasses :-----------
  • Array list
  • Vector
  • Linked list
Array list :---------
  • Array list is subclass of list. Array list elements will be stored internally with indexing notation.
  • Because of implementing random access marker interface you can access the array list element randomly.
  • Because of implementing cloneable,  serializable marker interfaces ,array list object is eligible for cloning and serialization.
  • Array list objects are not synchronized by defaults.
  • Synchronization means  we can  enforce locking mechanism.
Let us try to understand with a example:




import java.util.*;
class altest
{
public static void main(String as[])
{
ArrayList al=new ArrayList();
al.add(new Integer(99));
al.add("JAVA");
al.add(new String("java@yahoo.com"));
System.out.println(al);
}
}




Output:
99, JAVA, java@yahoo.com

import java.util.*;
class al1
{
public static void main(String as[])
{
ArrayList al=new ArrayList();
al.add(new Integer(99));
al.add("JAVA");
al.add(new String("java@yahoo.com"));
System.out.println(al);
Iterator i=al.iterator();
while(i.hasNext())
{
String str=i.next().toString();
System.out.println(str);
}          }           }
/*
Output:
99, JAVA, java@yahoo.com
99
java
java@yahoo.com

 
Difference between array list and vector
Array list
  •   Array list is collection class. Which is implementing marker interface like cloneable, serializable and random access 
  •   Objects of array list are not synchronized by default, i.e. multiple threads can access array list objects concurrently 
  •   You can access array list elements using Iterator.
Vector
  • Vector is a legacy class which is also implementing same three interface. 
  •   Vector class object are synchronized by default i.e. only one thread can access vector class object at a time .Remaining threads as to wait until thread usage is completed. 
  •  You can access vector elements using Iterator or Enumeration.


Difference between iterator and list iterator
Iterator
  • Using iterator you can visit the elements of collection in the forward direction only 
  •  Using iterator you cannot add elements to collection 
  •   Using iterator you can not replace existing object with new object. 
  •   Using iterator you can not get index of elements
List Iterator
  • In list iterator you can visit the elements of collection in the forward and backward direction. 
  •   Using list Iterator you can add elements to collection. 
  •   Using iterator you can  replace existing object with new object. 
  •   Using list Iterator you get index of  elements  list

Note: ---using both we can remove the elements from the collection.




Difference between iterator and enumeration: --------------
Iterator
  • Iterator is a collection framework. 
  •   Using iterator you can read elements of collection and also you can add elements, update element, read elements and remove elements.
  • You can use iterator to access the elements of Array list, Vector, Linked list, HashSet ,TreeSet, LinkedHashSet.


Enumeration
  •  Enumeration is a legacy interface 
  •   Using enumeration only you can read the elements. 
  •   You can use enumeration to access the elements of vector only





class test_String
{
public static void main(String as[])
{
String s="collection";
char ch[]={'a','b','c','d','e','f'};

String s1=new String(ch);
System.out.println(s);
System.out.println(s1);
String s2=new String(ch,1,4);
System.out.println(s2);
byte by[]={'1','2','3','4','5','6','7','8','9'};
String s3=new String(by);
System.out.println(s3);
String s4=new String(by,5);    //deprecated
System.out.println(s4);
String s5=new String(by,1,5);
System.out.println(s5);
String s6=new String(by,1,3,5);  //deprecated
System.out.println(s6);
/*try{String s7=new String(by,1,5,"h");
System.out.println(s7);
String s8=new String(by,"h");
System.out.println(s8);}
catch(Exception e)
{e.printStackTrace();}*/
String s9=new String(s);
System.out.println(s9);
StringBuffer sb=new StringBuffer("test");
String s10=new String(sb);
System.out.println(s10);
byte by1[]=new byte[10];
by1=s.getBytes();
for(int i=0;i<by1.length;i++)
{
System.out.print(by1[i] + ".");
//System.out.print(by1 + ".");  //it will call toString()
}
System.out.println(" ");
/* try{by1=s.getBytes("h");
for(int i=0;i<by1.length;i++)
{
System.out.print(by1[i] + ".");
}}catch(Exception e){e.printStackTrace();} */
byte by2[]=new byte[10];
s.getBytes(1,4,by2,3);
for(int i=0;i<by2.length;i++)
{
char c=(char)by2[i];                      
System.out.print(c + ".");
}
System.out.println(" ");
}
}




0 comments :

Post a Comment