Pages

Java Collections Deep Concept





Java.util package

This package contains a lot of utility classes for working with various data structures.
It also contains other classes like Date, Calendar, Locale etc
This is one of the vast packages (other vast packages are javax.swing , java.awt and related packages )



Collection.

A collection is a group of objects
Operations that can be performed on an collection

(i) adding new object into the collection
(ii)removing existing objects
(iii)modify existing objects
(iv)view all objects


Types of collections

Lists, Sets, Maps //interface

(i)Lists.
  These collections permit duplicate objects into the collection



(ii)Sets.
   These collections do not permit duplicate objects


(iii)Maps
   These collections store objects in terms of a key/value pair



Collection Framework.

A framework is a set of classes and interfaces
Collection framework
It is a set of collection classes and interfaces 


**java's collection framework is a part of "java.util" package


(i)the "java.util.Collection" interface
This interface is at the top of the collection framework. It has two child interfaces "java.util.List" and "java.util.Set"

(ii) the "java.util.List" interface.
  this interface and its classes are used to work with "List" model of collection


(iii) the "java.util.Set" interface
this interface and its classes are used to work with "Set" model of collection
few methods of "Collection" interface


(i) add(Object ob)
(ii)remove(Object ob)
(iii)iterator()
(iv)toArray()
(v)size()
(vi)isEmpty()



ArrayList
 
The "java.util.ArrayList" class

This class implements the "java.util.List" interface

This class is used to work with dynamic arrays.

Insertions into ArrayList are slower.
Retrievals and deletions from ArrayList are faster
ArrayList is used when data amount is known in advance,i.e when there are less no of insertions

import java.util.*;
class ArrayListDemo
{
    public static void main(String args[])
    {
        ArrayList al = new ArrayList();

        al.add("java");
        al.add("programming");
        al.add("java");
        al.add("programming");
        System.out.println(al);
    }
}




LinkedList
 
The "java.util.LinkedList" class

This class implements the "java.util.List" interface

This class is used for working with DOUBLY LINKED LISTS

Insertions into the LinkedList are faster
Retreivals and deletions are slower  
Linkedlist is used when there are many insertions

Ex:
import java.util.*;
class LinkedListDemo
{
    public static void main(String args[])
    {
        LinkedList al = new LinkedList();

        al.add("java");
        al.add("programming");
        al.add("java");
        al.add("programming");
        System.out.println(al);
        al.remove("java");
        al.remove("programming");
        System.out.println(al);

        int size = al.size();
        boolean b = al.isEmpty();
        System.out.println(size+":"+b);
        al.clear();
        size = al.size();
        b = al.isEmpty();
        System.out.println(size+":"+b);
    }
}



TreeSet

The "java.util.TreeSet"  class

This class implements the  "java.util.Set" interface

This is used to work with binary tree

import java.util.*;
class TreeSetDemo
{
    public static void main(String args[])
    {
        TreeSet al = new TreeSet();    
        al.add("java");
        al.add("programming");
        al.add("java");
        al.add("programming");
        System.out.println(al);
    }
}


Elements in TreeSet are automatically Sorted because "TreeSet" implements "SortedSet" interface which is a child of "Set" interface


HashSet

The "java.util.HashSet" class

This class implements "Set" interface

This class is used in working with hashing
Elements in "HashSet" are NOT SORTED and also NOT PLACED ACCORDING TO add() sequence, i.e elements in HashSet ARE NOT ORDERED

import java.util.*;
class HashSetDemo
{
    public static void main(String args[])
    {
        HashSet al = new HashSet();    
        al.add("A");
        al.add("B");
        al.add("C");
        al.add("D");
        System.out.println(al);
    }
}



Iterator

The "java.util.Iterator" interface
This interface contains the methods that are used to loop across the objects in a collection
This interface and its methods are available to all the children of "java.util.Collection" interface
 
Its methods are

    boolean  hasNext()
    Object   next()
    void     remove()

Getting a reference to "Iterator" interface
this is done by "iterator()" method of the "Collection" inteface

    Iterator iterator()
     Iterator itr= ll.iterator();
       Object ob= itr.next();
    String s = (String) ob;

import java.util.*;
class LinkedListDemo
{
    public static void main(String args[])
    {
        LinkedList al = new LinkedList();

        al.add("java");
        al.add("programming");
        al.add("java");
        al.add("programming");

        Iterator  itr = al.iterator();
        while(itr.hasNext())
        {
            String s = (String) itr.next();

            System.out.println(s.toUpperCase()); 
        }     
    }
}


Storing user defined objects in a collection

import java.util.*;
class Student
{
    int studentNo;
    Address a1;
    Student(int studentNo, Address a1)
    {
        this.studentNo = studentNo;
        this.a1 = a1;
    }
    void printStudent()
    {
        System.out.println("Student No : " +studentNo);
    }     


}

class Test
{
    public static void main(String args[])
    {
        LinkedList ll = new LinkedList();

        ll.add(new Student(30, new Address()));
        ll.add(new Student(10));
        ll.add(new Student(20));

        Iterator itr = ll.iterator();
        while(itr.hasNext())
        {
            Student temp = (Student) itr.next();
            temp.printStudent();

        }
    }
}



HashMap


The "java.util.HashMap" class
This class implements the "AbstractMap" interface
This class is used to work with collections which store objects in the form of key and value pairs
few methods of this class
    void put(Object key,Object value)
    Object get(Object key)
    Set  keySet()

import java.util.*;
class HashMapDemo
{
    public static void main(String args[])
    {
        HashMap hm = new HashMap();

        hm.put("tn","chennai");
        hm.put("mh","mumbai");
        hm.put("wb","kolkatta");
        hm.put("ap","hyderabad");


        Set s = hm.keySet();
        Iterator itr = s.iterator();
        while(itr.hasNext())
        {
            String s1 = (String)itr.next();
            String s2=(String)hm.get(s1);
            System.out.println(s1+":"+s2);
        } 
    }
}




TreeMap


The "java.util.TreeMap" class
This class implements the "SortedMap" interface
This works in the same way as "HashMap" but the elements are sorted automatically



Vector


The "java.util.Vector" class
This class same as "ArrayList", but its methods are synchronized



Enumeration


The "java.util.Enumeration" interface
This interface is same as "Iterator", but it is used with "Vector" class
its methods are
(i)    boolean hasMoreElements()
(ii)   Object nextElement()

 
import java.util.*;
class VectorDemo
{
    public static void main(String args[])
    {
        Vector v = new Vector();
        v.add("b");
        v.add("a");
        v.add("c");

        Enumeration e = v.elements();
        //(Iterator itr = al.iterator() )
        while(e.hasMoreElements() ) 
        {
            String s = (String) e.nextElement();
            System.out.println(s.toUpperCase() );
        } 
    }
}




Collections
The "java.util.Collections" class
It is a utility class which contains numerous number of static methods for different uses
Ex.
     Collections.sort(Il);
Assuming that "ll" is an object of "LinkedList" or "ArrayList" , the sort() method of "Collections" class sorts the objects of the "ll"


Arrays


The "java.util.Arrays" class
This is same like "Collections" but used for working with various operations on arrays like searching for an element in an array
the "java.util.Hashtable" class ('t' small letter)

This works in the same way as "HashMap" but the methods of this class are synchronized and "HashMap" methods are NOT SYNCHRONIZED the "java.util.StringTokenizer" class
This class is used to split a string into different words (tokens)
its methods are
    boolean hasMoreTokens()
    String nextToken()

The programming style of this class is same as "Enumeration"
Generics

A generic is a collection that supports storage of homogenous elements in a collection
Generics are introduced in JDK1.5

Syntax
 
  LinkedList<String> ll = new                 LinkedList<String>();


Enhanced for loop.
This loop is used to loop across the generics
Syntax cum example
    for(String s: ll)
    Sop(s.toUpperCase() )

HashMap<String,String> hm = new HashMap<String,String>();


Autoboxing.

It is a feature where we can add primitives to a collection. then they are automatically converted (boxed ) into its equivalent wrapper class type
   Ex.
   

    LinkedList ll = new LinkedList();
    ll.add(10); //error upto JDK1.4

    ll.add(10); // correct from JDK 1.5
                 // AUTO BOXING TAKES PLACE

Additional classes from "java.util" package which may be used on need basis
(i) Queue
(ii)PriorityQueue
(iii)Stack
(iv)LinkedHashMap
(v)WeakHashMap



Date

The  "java.util.Date" class
This class is used to work with system date
Ex.
import java.util.*;
class DateDemo
{
    p s  v m (String args[])
    {
        Date d  = new Date();
        Sop(d);
    }
}

No comments:

Post a Comment