Likes

Java Collection Framework


Java Collection Framework

Collection Framework provides an architecture to store and manipulate the group of objects.
All the operations that you perform on a data such as searching, sorting, insertion, deletion etc. can be performed by Java Collection Framework.
Collection simply means a single unit of objects.
What is Collection?
Collection represents a single unit of objects i.e. a group.
What is framework?
  • provides readymade architecture.
  • represents set of classes and interface.
  • is optional.
Hierarchy of Collection Framework
Let us see the hierarchy of collection framework.The java.util package contains all the classes and interfaces for Collection framework.




Methods of Collection interface
There are many methods declared in the Collection interface. They are as follows:
No.
Method
Description
1
public boolean add(Object element)
is used to insert an element in this collection.
2
public boolean addAll(collection c)
is used to insert the specified collection elements in the invoking collection.
3
public boolean remove(Object element)
is used to delete an element from this collection.
4
public boolean removeAll(Collection c)
is used to delete all the elements of specified collection from the invoking collection.
5
public boolean retainAll(Collection c)
is used to delete all the elements of invoking collection except the specified collection.
6
public int size()
return the total number of elements in the collection.
7
public void clear()
removes the total no of element from the collection.
8
public boolean contains(object element)
is used to search an element.
9
public boolean containsAll(Collection c)
is used to search the specified collection in this collection.
10
public Iterator iterator()
returns an iterator.
11
public Object[] toArray()
converts collection into array.
12
public boolean isEmpty()
checks if collection is empty.
13
public boolean equals(Object element)
matches two collection.
14
public int hashCode()
returns the hashcode number for collection.



Iterator interface
Iterator interface provides the facility of iterating the elements in forward direction only.
Methods of Iterator interface
There are only three methods in the Iterator interface. They are:

1.    public boolean hasNext() it returns true if iterator has more elements.
2.    public object next() it returns the element and moves the cursor pointer to the next element.
3.    public void remove() it removes the last elements returned by the iterator. It is rarely used.
ArrayList class:
·         uses a dynamic array for storing the elements.It extends AbstractList class and implements List interface.
·         can contain duplicate elements.
·         maintains insertion order.
·         random access because array works at the index basis.
·         manipulation slow because a lot of shifting needs to be occurred.

Example of ArrayList:
import java.util.*;  
class Simple{  
 public static void main(String args[]){  
   
  ArrayList al=new ArrayList();  
  al.add("Ravi");  
  al.add("Vijay");  
  al.add("Ravi");  
  al.add("Ajay");  
  
  Iterator itr=al.iterator();  
  while(itr.hasNext()){  
   System.out.println(itr.next());  
  }  
 }  
}  
Output:Ravi
       Vijay
       Ravi
       Ajay


List Interface:
List Interface is the subinterface of Collection.It contains methods to insert and delete elements in index basis.It is a factory of ListIterator interface.




Difference between List and Set:
List can contain duplicate elements whereas Set contains unique elements only.



HashSet class:
  • uses hashtable to store the elements.It extends AbstractSet class and implements Set interface.
  • contains unique elements only.

Example of HashSet class:
import java.util.*;  
class Simple{  
 public static void main(String args[]){  
   
  HashSet al=new HashSet();  
  al.add("Ravi");  
  al.add("Vijay");  
  al.add("Ravi");  
  al.add("Ajay");  
  
  Iterator itr=al.iterator();  
  while(itr.hasNext()){  
   System.out.println(itr.next());  
  }  
 }  
}  
Output:Ajay
       Vijay
       Ravi


Sorting
We can sort the elements of:
  1. String objects
  2. Wrapper class objects
Collections class provides static methods for sorting the elements of collection.If collection elements are of Set type, we can use TreeSet.But We cannot sort the elements of List.Collections class provides methods for sorting the elements of List type elements.
Method of Collections class for sorting List elements
public void sort(List list): is used to sort the elements of List.List elements must be of Comparable type.
Note: String class and Wrapper classes implements the Comparable interface.So if you store the objects of string or wrapper classes, it will be Comparable.
Example of Sorting the elements of List that contains string objects
import java.util.*;  
class Simple12{  
public static void main(String args[]){  
  
ArrayList al=new ArrayList();  
al.add("Viru");  
al.add("Saurav");  
al.add("Mukesh");  
al.add("Tahir");  
  
Collections.sort(al);  
Iterator itr=al.iterator();  
while(itr.hasNext()){  
System.out.println(itr.next());  
 }  
}  
}  
Output:Mukesh
       Saurav
       Tahir
       Viru
      
Example of Sorting the elements of List that contains Wrapper class objects
1.    import java.util.*;  
2.    class Simple12{  
3.    public static void main(String args[]){  
4.      
5.    ArrayList al=new ArrayList();  
6.    al.add(Integer.valueOf(201));  
7.    al.add(Integer.valueOf(101));  
8.    al.add(230);//internally will be converted into objects as Integer.valueOf(230)  
9.      
10. Collections.sort(al);  
11.   
12. Iterator itr=al.iterator();  
13. while(itr.hasNext()){  
14. System.out.println(itr.next());  
15.  }  
16. }  
17. }  
Output:101
       201
       230
             

No comments: