JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com





Get Best Tutorial on all major programing language

Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Collection Framework:

Collections Framework is a unified architecture for managing collections








What is a Collection?

  • A Collection (also known as container) is an object that contains a group of
    objects treated as a single unit.
  • Any type of objects can be stored, retrieved and manipulated as elements of collections.
Basic collection operations:

  • Check if collection is empty
  • Check if an object exists in collection.
  • Retrieve an object from collection
  • Add object to collection
  • Remove object from collection
  • Iterate collection and inspect each object

Each operation has a corresponding method implementation for each collection type


Collections Characteristics

  • Ordered
    Elements are stored and accessed in a specific order
  • Sorted
    Elements are stored and accessed in a sorted order
  • Indexed
    Elements can be accessed using an index
  • Unique
    Collection does not allow duplicates

Iterator

  • Iteration is the process of retrieving every element in a collection.
  • An Iterator of a Set is unordered.
  • A ListIterator of a List can be scanned forwards (using the next method) or backwards
    (using the previous method).
Syntax:
Iterator <variable> = <CollectionObject>.iterator();

The Iterator Interface Hierarchy




List Interface:
  • A List interface extends Collection interface.
  • A List interface contain two main Class
    • ArrayList
    • LinkedList
  • The main functionality of list interface is allow the user to add the duplicate value.
Example:
Using LinkedList Interface
  • In this example, create a linkedlist object.
  • and store three different value.


import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class list1 {

  public static void main(String[] args) {

    LinkedList l1 = new LinkedList();
    l1.add("mohan");
    l1.add(new Integer(100));
    l1.add(500);
        
    
    System.out.println(l1);
    for (int i = 0; i < l1.size(); i++) {
      System.out.println(l1.get(i));
    }

    for (Object ob : l1) {
      System.out.println(ob);
    }
    
    Iterator  i1=l1.iterator();
    
    while (i1.hasNext()) {
        
      System.out.println(i1.next());
      
    }
    

  }

}

Output:

javac list1.java
java list1
[mohan, 100, 500]
mohan
100
500
mohan
100
500
mohan
100
500

Download the code


Set Interface:
  • Set Interface extends collection interface.
  • The Set Interface doesn't allow duplicate value.
  • If we store duplicate value then delete previous value.
  • A Set interface contain two main Class
    • HashSet
    • TreeList


import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class set1 {

  public static void main(String[] args) {
    Set<String> s=new HashSet<String>();
    
    s.add("aaa");
    s.add("bbb");
     
    for (Object object : s) {
      System.out.println(object);
    }
    
    Iterator i1=s.iterator();
    
    while (i1.hasNext()) {
    System.out.println(i1.next());
    }
    
    System.out.println(s);
    Float f1=new Float("11.2f");
    System.out.println(f1);
  }
  
}

Output:

javac set1.java
java set1
aaa
bbb
aaa
bbb
[aaa, bbb]
11.2

Download the code

Map Interface:
  • Map Interface define inside java.utill package
  • But its not part of collection framework because its not extends collection interface.
  • Map Interface are work on key, value pair.
  • In map interface key must be unique and value can be duplicate.
  • A Map interface contain two main Class
    • HashMap
    • TreeMap


Example:
  • In this example, create TreeMap object
  • Store three value with help of key, value

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class map1 {

  public static void main(String[] args) {
     Map map1=new TreeMap();
         
     map1.put("a","apple" );
     map1.put("c","china" );
     map1.put("b""boy");
     
     
     System.out.println(map1);
     System.out.println(map1.get("a"));
      
     Set s= map1.keySet();
     
     
     for (Object ob : s) {
    System.out.println(ob+"-->"+map1.get(ob));
  }
  }
}

Output:

javac map1.java
java map1
{a=apple, b=boy, c=china}
apple
a-->apple
b-->boy
c-->china

Download the code

Enumeration Interface:
  • Store multiple value in single data type.
  • Enumeration work two method
    • hasMoreElements()
    • nextElement()
Example:
  • In this example, we have create enum object Season.
  • Store 4 value.


enum Season{
  spring,summer,autumn,winter;
   Season() {
    // TODO Auto-generated constructor stub
  }
  }
public class myseason {

  
  
  public static void main(String[] args) {
     Season se=Season.winter;
    System.out.println("my  fav season is "+Season.spring);
    System.out.println("enum ordinal for summer  is "+Season.summer.ordinal());
    System.out.println("enum name is "+Season.summer.name());
    System.out.println(se);
     
    Season se1=Season.valueOf("summer");
        
    System.out.println(se1);
    
    Season season[]=Season.values();
    
    for (Season season2 : season) {
      System.out.println(season2);
    }
    
  }
  
}

Output:

javac myseason.java
java myseason
my  fav season is spring
enum ordinal for summer  is 1
enum name is summer
winter
summer
spring
summer
autumn
winter

Download the code