All for Joomla All for Webmasters

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.   ArrayList<String> al=new ArrayList<String>();//creating arraylist
  5.   al.add(“Ravi”);//adding object in arraylist
  6.   al.add(“Vijay”);
  7.   al.add(“Ravi”);
  8.   al.add(“Ajay”);
  9.   Iterator itr=al.iterator();//getting Iterator from arraylist to traverse elements
  10.   while(itr.hasNext()){
  11.    System.out.println(itr.next());
  12.   }
  13.  }
  14. }
 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.   ArrayList<Student> al=new ArrayList<Student>();//creating arraylist
  9.   al.add(s1);//adding Student class object
  10.   al.add(s2);
  11.   al.add(s3);
  12.   Iterator itr=al.iterator();
  13.   //traversing elements of ArrayList object
  14.   while(itr.hasNext()){
  15.     Student st=(Student)itr.next();
  16.     System.out.println(st.rollno+” “+st.name+” “+st.age);
  17.   }
  18.  }
  19. }
 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.   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(“Sonoo”);
  10.   al2.add(“Hanumat”);
  11.   al.addAll(al2);
  12.   Iterator itr=al.iterator();
  13.   while(itr.hasNext()){
  14.    System.out.println(itr.next());
  15.   }
  16.  }
  17. }
 Ravi
       Vijay
       Ajay
       Sonoo
       Hanumat

Example of removeAll() method

  1. import java.util.*;
  2. class TestCollection5{
  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.   al.removeAll(al2);
  12.   System.out.println(“iterating the elements after removing the elements of al2…”);
  13.   Iterator itr=al.iterator();
  14.   while(itr.hasNext()){
  15.    System.out.println(itr.next());
  16.   }
  17.   }
  18. }

 

       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.   al.retainAll(al2);
  12.   System.out.println(“iterating the elements after retaining the elements of al2…”);
  13.   Iterator itr=al.iterator();
  14.   while(itr.hasNext()){
  15.    System.out.println(itr.next());
  16.   }
  17.  }
  18. }
  iterating the elements after retaining the elements of al2...
       Ravi
PinIt
submit to reddit

Leave a Reply

Top