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
Difference between array list and vector
Array list
|
Vector
|
Difference between iterator and list iterator
Iterator
|
List Iterator
|
Note: ---using both we can remove the elements from the collection.
Difference between iterator and enumeration: --------------
Iterator
|
Enumeration
|
No comments:
Post a Comment