AdSense

AdSense3

Monday 31 August 2015

Java List Interface

List Interface is the subinterface of Collection.It contains methods to insert and delete elements in index basis.It is a factory of ListIterator interface.

Commonly used methods of List Interface:

  1. public void add(int index,Object element);
  2. public boolean addAll(int index,Collection c);
  3. public object get(int Index position);
  4. public object set(int index,Object element);
  5. public object remove(int index);
  6. public ListIterator listIterator();
  7. public ListIterator listIterator(int i);

Java ListIterator Interface

ListIterator Interface is used to traverse the element in backward and forward direction.

Commonly used methods of ListIterator Interface:

  1. public boolean hasNext();
  2. public Object next();
  3. public boolean hasPrevious();
  4. public Object previous();

Example of ListIterator Interface:

  1. import java.util.*;  
  2. public class TestCollection8{  
  3. public static void main(String args[]){  
  4.   
  5. ArrayList<String> al=new ArrayList<String>();  
  6. al.add("Amit");  
  7. al.add("Vijay");  
  8. al.add("Kumar");  
  9. al.add(1,"Sachin");  
  10.   
  11. System.out.println("element at 2nd position: "+al.get(2));  
  12.   
  13. ListIterator<String> itr=al.listIterator();  
  14.   
  15. System.out.println("traversing elements in forward direction...");  
  16. while(itr.hasNext()){  
  17. System.out.println(itr.next());  
  18.  }  
  19.   
  20.   
  21. System.out.println("traversing elements in backward direction...");  
  22. while(itr.hasPrevious()){  
  23. System.out.println(itr.previous());  
  24.  }  
  25. }  
  26. }
Output:element at 2nd position: Vijay
       traversing elements in forward direction...
       Amit
       Sachin
       Vijay
       Kumar
       traversing elements in backward direction...
       Kumar
       Vijay
       Sachin
       Amit

Friday 28 August 2015

Difference between ArrayList and LinkedList

ArrayList and LinkedList both implements List interface and maintains insertion order. Both are non synchronized classes.

But there are many differences between ArrayList and LinkedList classes that are given below.
ArrayListLinkedList
1) ArrayList internally uses dynamic array to store the elements.LinkedList internally uses doubly linked list to store the elements.
2) Manipulation with ArrayList is slow because it internally uses array. If any element is removed from the array, all the bits are shifted in memory.Manipulation with LinkedList is faster than ArrayList because it uses doubly linked list so no bit shifting is required in memory.
3) ArrayList class can act as a list only because it implements List only.LinkedList class can act as a list and queue both because it implements List and Deque interfaces.
4) ArrayList is better for storing and accessing data.LinkedList is better for manipulating data.

Example of ArrayList and LinkedList in Java

Let's see a simple example where we are using ArrayList and LinkedList both.
  1. import java.util.*;    
  2. class TestArrayLinked{    
  3.  public static void main(String args[]){    
  4.      
  5.   List<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.   List<String> al2=new LinkedList<String>();//creating linkedlist    
  12.   al2.add("James");//adding object in linkedlist    
  13.   al2.add("Serena");    
  14.   al2.add("Swati");    
  15.   al2.add("Junaid");    
  16.     
  17.   System.out.println("arraylist: "+al);  
  18.   System.out.println("linkedlist: "+al2);  
  19.  }    
  20. }    
Output:
arraylist: [Ravi,Vijay,Ravi,Ajay]
linkedlist: [James,Serena,Swati,Junaid]

Thursday 27 August 2015

Java LinkedList class

  • Java LinkedList class uses doubly linked list to store the elements. It extends the AbstractList class and implements List and Deque interfaces.
  • Java LinkedList class can contain duplicate elements.
  • Java LinkedList class maintains insertion order.
  • Java LinkedList class is non synchronized.
  • In Java LinkedList class, manipulation is fast because no shifting needs to be occurred.
  • Java LinkedList class can be used as list, stack or queue.
java LinkedList class in collection framework

Java LinkedList Example

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

Output:Ravi
       Vijay
       Ravi
       Ajay

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