AdSense

AdSense3

Thursday, 27 August 2015

Java ArrayList class

  • Java ArrayList class uses a dynamic array for storing the elements.It extends AbstractList class and implements List interface.
  • Java ArrayList class can contain duplicate elements.
  • Java ArrayList class maintains insertion order.
  • Java ArrayList class is non synchronized.
  • Java ArrayList allows random access because array works at the index basis.
  • In Java ArrayList class, manipulation is slow because a lot of shifting needs to be occurred if any element is removed from the array list.

Java Non-generic Vs Generic Collection

Java collection framework was non-generic before JDK 1.5. Since 1.5, it is generic.
Java new generic collection allows you to have only one type of object in collection. Now it is type safe so typecasting is not required at run time.
Let's see the old non-generic example of creating java collection.
  1. ArrayList al=new ArrayList();//creating old non-generic arraylist  
Let's see the new generic example of creating java collection.
  1. ArrayList<String> al=new ArrayList<String>();//creating new generic arraylist  
In generic collection, we specify the type in angular braces. Now ArrayList is forced to have only specified type of objects in it. If you try to add another type of object, it gives compile time error.

Example of Java ArrayList class

  1. import java.util.*;  
  2. class TestCollection1{  
  3.  public static void main(String args[]){  
  4.    
  5.   ArrayList<String> al=new ArrayList<String>();//creating arraylist  
  6.   al.add("Ravi");//adding object in arraylist  
  7.   al.add("Vijay");  
  8.   al.add("Ravi");  
  9.   al.add("Ajay");  
  10.   
  11.   Iterator itr=al.iterator();//getting Iterator from arraylist to traverse elements  
  12.   while(itr.hasNext()){  
  13.    System.out.println(itr.next());  
  14.   }  
  15.  }  
  16. }  
       Ravi
       Vijay
       Ravi
       Ajay

Two ways to iterate the elements of collection in java

  1. By Iterator interface.
  2. By for-each loop.
In the above example, we have seen traversing ArrayList by Iterator. Let's see the example to traverse ArrayList elements using for-each loop.

Iterating the elements of Collection by for-each loop

  1. import java.util.*;  
  2. class TestCollection2{  
  3.  public static void main(String args[]){  
  4.   ArrayList<String> al=new ArrayList<String>();  
  5.   al.add("Ravi");  
  6.   al.add("Vijay");  
  7.   al.add("Ravi");  
  8.   al.add("Ajay");  
  9.   for(String obj:al)  
  10.     System.out.println(obj);  
  11.  }  
  12. }  
       Ravi
       Vijay
       Ravi
       Ajay

User-defined class objects in Java ArrayList

  1. class Student{  
  2.   int rollno;  
  3.   String name;  
  4.   int age;  
  5.   Student(int rollno,String name,int age){  
  6.    this.rollno=rollno;  
  7.    this.name=name;  
  8.    this.age=age;  
  9.   }  
  10. }  
  1. import java.util.*;  
  2. public class TestCollection3{  
  3.  public static void main(String args[]){  
  4.   //Creating user-defined class objects  
  5.   Student s1=new Student(101,"Sonoo",23);  
  6.   Student s2=new Student(102,"Ravi",21);  
  7.   Student s2=new Student(103,"Hanumat",25);  
  8.       
  9.   ArrayList<Student> al=new ArrayList<Student>();//creating arraylist  
  10.   al.add(s1);//adding Student class object  
  11.   al.add(s2);  
  12.   al.add(s3);  
  13.     
  14.   Iterator itr=al.iterator();  
  15.   //traversing elements of ArrayList object  
  16.   while(itr.hasNext()){  
  17.     Student st=(Student)itr.next();  
  18.     System.out.println(st.rollno+" "+st.name+" "+st.age);  
  19.   }  
  20.  }  
  21. }  
       101 Sonoo 23
       102 Ravi 21
       103 Hanumat 25

Example of addAll(Collection c) method

  1. import java.util.*;  
  2. class TestCollection4{  
  3.  public static void main(String args[]){  
  4.    
  5.   ArrayList<String> al=new ArrayList<String>();  
  6.   al.add("Ravi");  
  7.   al.add("Vijay");  
  8.   al.add("Ajay");  
  9.     
  10.   ArrayList<String> al2=new ArrayList<String>();  
  11.   al2.add("Sonoo");  
  12.   al2.add("Hanumat");  
  13.     
  14.   al.addAll(al2);    
  15.   
  16.   Iterator itr=al.iterator();  
  17.   while(itr.hasNext()){  
  18.    System.out.println(itr.next());  
  19.   }  
  20.  }  
  21. }  

       Ravi
       Vijay
       Ajay
       Sonoo
       Hanumat

Example of removeAll() method

  1. import java.util.*;  
  2. class TestCollection5{  
  3.  public static void main(String args[]){  
  4.    
  5.   ArrayList<String> al=new ArrayList<String>();  
  6.   al.add("Ravi");  
  7.   al.add("Vijay");  
  8.   al.add("Ajay");  
  9.     
  10.   ArrayList<String> al2=new ArrayList<String>();  
  11.   al2.add("Ravi");  
  12.   al2.add("Hanumat");  
  13.     
  14.   al.removeAll(al2);  
  15.   
  16.   System.out.println("iterating the elements after removing the elements of al2...");  
  17.   Iterator itr=al.iterator();  
  18.   while(itr.hasNext()){  
  19.    System.out.println(itr.next());  
  20.   }  
  21.   
  22.   }  
  23. }  

       iterating the elements after removing the elements of al2...
       Vijay
       Ajay
       

Example of retainAll() method

  1. import java.util.*;  
  2. class TestCollection6{  
  3.  public static void main(String args[]){  
  4.   ArrayList<String> al=new ArrayList<String>();  
  5.   al.add("Ravi");  
  6.   al.add("Vijay");  
  7.   al.add("Ajay");  
  8.   ArrayList<String> al2=new ArrayList<String>();  
  9.   al2.add("Ravi");  
  10.   al2.add("Hanumat");  
  11.   
  12.   al.retainAll(al2);  
  13.   
  14.   System.out.println("iterating the elements after retaining the elements of al2...");  
  15.   Iterator itr=al.iterator();  
  16.   while(itr.hasNext()){  
  17.    System.out.println(itr.next());  
  18.   }  
  19.  }  
  20. }  

       iterating the elements after retaining the elements of al2...
       Ravi

No comments:

Post a Comment