*Collections:  

Array:

       An array is collection of elements which are stared in continuous  memory locations.

Limitations of array (or) disadvantages of array:

1.    The size of an array is fixed; it can’t be increased or decreased. Some times. The memory may be wasted or memory may not be sufficient.

2.    To perform the operations like Insertion, Deletion, Searching, Sorting etc. the program has to write there own logic.

3.    Because of the problems in an array the java people or java s/w people java people have come it collection framework introduction java 1.2 versions.

Collection object:

         An object is said to be collection object if it holds or stores a group of other objects.

Collection class:


               A collection class is a class whose object can store group of other objects.



The objects performed by s1,s2,s3 and s4 are stored multiple times there by wasting the memory with in the JVM.

 To save the memory with in the jvm when the objects are stored in the collection object, the jvm stores the references of the objects with in the collection objects inside of storing the objects directly.


The collections are designed to store only objects that is the collections can not store primitive type values.

All the collection classes are available in “java.util” (utility) package.

All the collection interfaces and collection class and together as collection frame work.

All the collection classes are categorized into three groups’ i.e.

1.    List: this category is used to store group of individual elements where the elements can be duplicated. List is an Interface. “Array list, Linked list and vector” are the implementations class of list interfaces.

2.    Set: this category is used to store a group of individual elements. But they elements can’t be duplicated. Set is an interface.
Hash set, Linked Hash set and Tree set implementations of set interface.

3.    Map: this category is used to store the element in the form key value pairs where the keys can’t be duplicated, values can be duplicated.
Map is an interfaces “Hash Map, Linked Hash Map, Tree Map and Hash table are the implementation class of Map interface.

Array List: this class is an implementation class of list interface. This class is similar to an Array but its size can be increased or decreased. This class is used to store individual elements which can be duplicated. This class is “not synchronized”.

If an object is synchronized then only one thread can use the object at the same time, if an object is a not synchronized then multiple threads can access the object at the same time, which may lead to data inconsistency problems.

Creation of Array List:

Syntax: Array List<E> al = new Array List<E>();
Array List<E> al = new Array List <E>(int initialcapacity);
Here, E represents element data type
Methods of Array List:

1.    Boolean add(Element obj): this method is used to place the specified element to the end of List.
2.    Void add(int position, Element obj): this method is used to insert and element at the specified position.
3.    Boolean remove(Element obj): this method is used to remove the 1st occurrence of the specified element.

4.    Element remove(int position): this method is used to remove and Element from the specified position.

5.    Void clear(): this method remove all the elements available in the Array List.

6.    Int Size(): this method will return the count of the no.of elements available in the Array List.

7.    Boolean contains(element obj): this method returns true if the specified element is available in the Array List.

8.    Element get(int position): this method is used to access an Element that is available in the specified position.

9.    Element set(int position, Element obj): this method is used replace an element in the specified position with the specified element.


Sample Collection Program:


Import java.util.*;
class ArrayList demo{

Public static void main(string[] args){

//creation of ArrayList
ArrayList<String> al = new ArrayList<String>();

//adding elements to the ArrayList
al.add(“Nokia”);

al.add(“Samsung”);
al.add(“Sony”);

al.add(“Celkon”);
al.add(“HTC”);

//insert an element into ArrayList
al.add(3,”Motorla”);

//displaying the elements
System.out.println(“List:”+al);
//deleting the elements
al.remove(“HTC”);
al.remove(1);

System.out.println(“List:”+al);
//displaying the size
System.out.println(“size:”+al.size());

//displaying the elements using iterator
Iterator it = al.iterator();
while(it.hasNext()){
System.out.println(it.next());
}//end of while
       }//end of main
             }//end of class


Note: Iterator to using the display of elements one by one. (iterator is a interface)

0 comments:

Post a Comment