Sunday, October 21, 2012

equals method of collection in java

import java.util.ArrayList;

public class equalsMethod {
    public static void main(String[] args) {

        ArrayList a1 = new ArrayList();
        a1.add("One");
        a1.add("Two");
        a1.add("Three");
        a1.add("Four");
        a1.add("Five");

        ArrayList a2 = new ArrayList();
        a2.add("10");
        a2.add("20");
        a2.add("Two");
        a2.add("Four");
        a2.add("5");
   
        boolean result;
       
        for(int i=0;i<a1.size();i++)
        {

            /*
               equals method of collection checks whether the index of one collection object has the 
               same value as the corresponding index of the other collection object.Here it checks for 
               whether a1.get(1) equals a2.get(1), a1.get(2) equals a2.get(2) or not...correspondingly 
               etc...
            */
            result = a1.get(i).equals(a2.get(i));
            System.out.println("a2's "+a2.get(i)+" and a1's "+a1.get(i)+" are equal ? " +result);
        }

    }
}

Answer
----------
a2's 10 and a1's One are equal ? false
a2's 20 and a1's Two are equal ? false
a2's Two and a1's Three are equal ? false
a2's Four and a1's Four are equal ? true

a2's 5 and a1's Five are equal ? false 

contains method of collection in java

import java.util.*;
import java.*;

public class containsMethodOfCollection {
    public static void main(String[] args) {       

        ArrayList a1 = new ArrayList();
        a1.add("One");
        a1.add("Two");
        a1.add("Three");
        a1.add("Four");
        a1.add("Five");
               
        ArrayList a2 = new ArrayList();
        a2.add("10");
        a2.add("20");
        a2.add("Two");
        a2.add("Four");
        a2.add("5");
   
        boolean result;
        /*
           From the result you can find out that  for each a2.get(i), index it does not check with the 
           corresponding index of a1, instead a2.get(i) only checks internally that whether all the 
           components of a2 index wise internally do have an component of the same in the a1 
           arraylist or not.
        */
        for(int i=0;i<a1.size();i++)
        {
            result = a1.contains(a2.get(i));
            System.out.println("a2's "+a2.get(i)+" and a1's "+a1.get(i)+" are equal ? " +result);
        }
        /*
          containsAll returns true only when all the componenets of a1 or a2 have a corresponding 
          equal component in the other collection object, even if one component mismatches the 
          boolean returns false 
        */
        for(int i=0;i<a1.size();i++)
        {
            result = a1.containsAll(a2);
            System.out.println("a2's "+a2.get(i)+" and a1's "+a1.get(i)+" are equal ? " +result);
        }
       
    }
}

Answer : 
------------

a2's 10 and a1's One are equal ? false
a2's 20 and a1's Two are equal ? false
a2's Two and a1's Three are equal
? true
a2's Four and a1's Four are equal ? true
a2's 5 and a1's Five are equal ? false


a2's 10 and a1's One are equal ? false
a2's 20 and a1's Two are equal ? false
a2's Two and a1's Three are equal ? false
a2's Four and a1's Four are equal ? false
a2's 5 and a1's Five are equal ? false

Saturday, October 20, 2012

addAll Method Example in java

import java.util.*;
public class addAll
{
    public static void main(String[] args)
    {
        // Create ArrayList and LinkedList collections:
        ArrayList a1 = new ArrayList();
        //LinkedList c1 = new LinkedList();
       
        TreeSet ts = new TreeSet();
        // Initialize aList:
        a1.add("BalajiA");
        a1.add("PadmaB");
        a1.add("BalajiB");
        a1.add("PadmaA");
       
        ts.add("BalajiC");
        ts.add("BalajiD");       
        ts.add("PadmaS");
        ts.add("PadmaT");       
       
        // Add all the elements of aList to lList:
        /*
        c1.addAll(a1);
        */
        boolean result = ts.addAll(a1);
        System.out.println("The Result of TreeList is " + ts + " and TreeList's size is " + ts.size());
        System.out.println("The Result of ArrayList is " + a1 + " and ArrayList's size is " + a1.size());
        System.out.println("True or False ? " + result);
       
        //Create ArrayList and LinkedList collections:
        Collection aList = new ArrayList();
        Collection lList = new LinkedList();
        //Initialize aList:
        aList.add("NewBalaji");
        aList.add("NewPadma");
        //Add all the elements of aList to lList:
        lList.addAll(aList);
        //Display lList:
        System.out.println("Collection  ArrayList is " + aList);
        System.out.println("Collection LinkedList is " + lList);
       
       
        List<Integer> listA = new ArrayList<Integer>();
        List<Integer> listB = new ArrayList<Integer>();

        listA.add(1);
        listA.add(2);
        listA.add(3);
        listA.add(4);

        listB.add(1);
        listB.add(2);


        listA.addAll(listB);

        for(Integer i : listA){
        System.out.print(i);
        }
       
    }
}

Answer : 
-----------
The Result of TreeList is [BalajiA,BalajiB,BalajiC,BalajiD,PadmaA,PadmaB,PadmaS,PadmaT]  
and TreeList's size is 8
The Result of ArrayList is [BalajiA, PadmaB, BalajiB, PadmaA] and ArrayList's size is 4
True or False ? true
Collection  ArrayList is [NewBalaji, NewPadma]
Collection LinkedList is [NewBalaji, NewPadma]
123412

clear method Example in java

import java.util.*;
public class clearMethod {

    public static void main(String[] args) {
       
        ArrayList a1 = new ArrayList();
        a1.add(1);
        a1.add(2);
        a1.add(3);
        a1.add(4);
        a1.add(5);
       
        System.out.println("ArrayList size is " + a1.size());
        /*
          After the addition of 5 numbers to the ArrayList the size becomes 5
        */
        a1.clear();
        /*
          now after the clear method is invoked in the ArrayList its size becomes zero
        */
        System.out.println("ArrayList size is now " + a1.size());

    }

}


Answer : 
------------
ArrayList size is 5
ArrayList size is now 0

Saturday, October 13, 2012

Sum total in oracle

Lets say you are running sql plus and some records come, now you have to calculate the sum of a column.


compute sum of column on report
break on report

/


compute sum on report
break on report
/

For break on report you dont necessarily have to type until the last letter k, even if you just type brea
it will work.

Tuesday, October 2, 2012

IndexOutOfBoundsException Example in Java

IndexOutOfBoundsException occurs whenever the number(index) variable increases the ArraySize limit.

(a)
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
public class IndexOutOfBoundsException {
    public static void main(String args[]){       
        ArrayList a1 = new ArrayList();
           Employee obj = new Employee();
           a1  = (ArrayList)obj.displayRecord();
           Iterator itr = a1.iterator();
           int variable = 0;
           if(a1.size() > 0)
           {
                System.out.println(a1.size());
               /*
               The Arraysize is 10. that is the total number of records are 10., but the ArrayList 
               length starts from 0 until 9, that is internally the index in (b) class ArrayList                                      displayRecord() {} is from 0  to 9 but when it comes to (a) class for{} loop , the for                            loop is  set from 0 to 10....which means that when the for loop  in class(a) starts at                              0 it would be going to the first record in the ArrayIndex  ---  starting at 0(1st record), 
               and hence when the for loop reaches 9 The ArrayIndex  would have reached its full                       ull              strength of 9 (or 10th record) and when the                   class (a)'s for loop reaches 10, obj=(Employee)a1.get(i); will search for the (11th record !!)
               which is not available and hence will throw an IndexOutOfBoundsException
               */

               for(int i=0;i<=a1.size();i++)
               {   
                   obj=(Employee)a1.get(i);
                   variable = obj.getEmployeeNumber();
                   System.out.println(variable);
               }
           }
    }
}

(b)

import java.util.*;
public class Employee {
 
   private int employeeNumber=1;
   public int getEmployeeNumber()
   {
       return employeeNumber;
   }
   public void setEmployeeNumber(int employeeNumber)
   {
       this.employeeNumber=employeeNumber;
   }   
   public ArrayList displayRecord()
   {
       ArrayList showRecord=new ArrayList();
       /*Array Index for loop starts here*/
       for(int i=1;i<=10;i++)
       {
            Employee empObj=new Employee();
            empObj.setEmployeeNumber(i);
            showRecord.add(empObj);
       }
       return showRecord;        
       /*Array Index for loop ends here*/
    }

 }

Answer
----------
10
1
2
3
4
5
6
7
8
9
10
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 10, Size: 10
    at java.util.ArrayList.rangeCheck(ArrayList.java:604)
    at java.util.ArrayList.get(ArrayList.java:382)
    at pack1.IndexOutOfBoundsException.main(IndexOutOfBoundsException.java:19)

Use of Add Method in ArrayList

(a)

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListServeletforAddMethod {
    public static void main(String args[]){
       
           ArrayList a1 = new ArrayList();
           Employee obj = new Employee();
           a1  = (ArrayList)obj.displayRecord();
           Iterator itr = a1.iterator();
           int variable = 0;
           if(a1.size() > 0)
           {
               System.out.println(a1.size());
               for(int i=0;i<a1.size();i++)
               {   
                   obj=(Employee)a1.get(i);
                   variable = obj.getEmployeeNumber();
                   System.out.println(variable);
               }
           }

    }
}

(b)
import java.util.*;
public class Employee {
   
    private int employeeNumber=1;
   
    public int getEmployeeNumber()
    {
        return employeeNumber;
    }
    public void setEmployeeNumber(int employeeNumber)
    {
        this.employeeNumber=employeeNumber;
    }   
       
    public ArrayList displayRecord()
    {
        ArrayList showRecord=new ArrayList();
        for(int i=1;i<=10;i++)
        {
            Employee empObj=new Employee();
            empObj.setEmployeeNumber(i);
            showRecord.add(empObj);
        }
        return showRecord;       
    }

}


Answer
--------

10
1
2
3
4
5
6
7
8
9
10

OutOfMemoryError in ArrayList, Example

import java.util.*;
public class OutofMemoryErrorInArrayList {
    public static void main(String[] args) {
        ArrayList a1 = new ArrayList();
        for(int i=1;i<=99999999;i++)
        {
            try
            {
                a1.add("a");
            }
            catch(Exception e)
            {
                System.out.println("Array Size is " + a1.size());
            }
        }       
    }
}

Answer
--------
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:2245)
    at java.util.Arrays.copyOf(Arrays.java:2219)
    at java.util.ArrayList.grow(ArrayList.java:213)
    at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:187)
    at java.util.ArrayList.add(ArrayList.java:411)
    at OutofMemoryErrorInArrayList.main(OutofMemoryErrorInArrayList.java:9)

ArrayIndexOutOfBoundsException Example in Java

import java.util.*;
public class ArrayIndexOutOfBoundsException {

    public static void main(String[] args) {
           
            String []array = new String[0];
            array[0] = "0";
            System.out.println("String []array ? " + array[0]);

    }

}

Answer
--------
The following error occurs

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at ArrayIndexOutOfBoundsException.main(ArrayIndexOutOfBoundsException.java:7)

ArrayStoreException Example in Java

import java.util.*;
public class ArrayStoreException {
    public static void main(String[] args) {

        Object x[] = new String[3];
        x[0] = 0;       
        /*  
        List<String> myList = new ArrayList<String>(); 
        myList.add("Hello"); 
        myList.add("World"); 
        String[] myStrings = myList.toArray(new String[0]); 
        System.out.println("myStrings is " + myStrings.length + " long");
        */
   
    }
}

Answer
---------

On compiling the following error occurs

Exception in thread "main" java.lang.ArrayStoreException: java.lang.Integer
    at ArrayStoreException.main(ArrayStoreException.java:6)

Difference between Standard Arrays and ArrayList ?

import java.util.*;
public class StandardArrays_Vs_ArrayList {

    public static void main(String[] args) {
        /*
        We Cannot Define it this [] (i.e) we cannot give any empty declaration, declaring such will

        throw an CompiletimeException we have to start the declaration Starting from Numeric 
        Value 1, (i.e) we cannot declare as new String[0] as it will throw an RuntimeException
        String []array = new String[];
       
*/
        String []array = new String[1];
        array[0] = "0";
        System.out.println("String []array " + array[0]);
       
        /*
        The below programme proves that an ArrayList grows dynamically...
       
*/
        ArrayList a1 = new ArrayList();
        a1.add("One");
        /*
        ... as you can see here the ArrayList size has not been specified and the ArrayList size is 

        now 1
       
*/
        System.out.println("The Initial Size is " + a1.size());
        a1.add("Two");
        a1.add("Three");
        /*
        and here after adding 2 records the ArrayList size increases to 3.it can keep increasing to 

        the point of when there is an OutOfMemoryError due to insufficient heapspace.
       
*/
        System.out.println("Now The Size is " + a1.size());
    }

}