Working With List Interface of Collection Frame Work in Java

Introduction

 
In this article first, we introduce the collection framework. Since we have also written an article previously about the collection framework, in this article we only provide a definition of the collection framework, its hierarchy structure and details of its List Interface.
 
 collectionsnew.jpg
 
The Collections Framework provides a set of interfaces and classes for storing and manipulating groups of data as a single unit; a collection. And it provides a convenient API to many of the abstract data types data structure curricula: maps, sets, lists, trees, arrays, hashtables and other collections. Because of their object-oriented design, the Java classes in the Collections Framework encapsulate both the data structures and the algorithms associated with these abstractions.
 

List Interface

 
The Java Collection framework is mainly divided into interfaces and abstract classes so the List is an interface and it contains the following abstract methods:
 
 UMLList.gif
 
Note: The List interface extends the Collection interface to define an ordered collection, permitting duplicates. The interface adds position-oriented operations, as well as the ability to work with just a part of the list.
 
Example 1
  1. import java.util.*;   
  2. public class ListDemo  
  3.  {  
  4.   public static void main(String args[])  
  5.     {  
  6.     List list = new ArrayList();  
  7.     list.add("abhishek");  
  8.     list.add("Mani mishra");  
  9.     list.add("Neha");  
  10.     list.add("Arun");  
  11.     list.add("Aditya");  
  12.     System.out.println(list);  
  13.     System.out.println("2: " + list.get(2));  
  14.     System.out.println("0: " + list.get(0));  
  15.     LinkedList queue = new LinkedList();  
  16.     queue.addFirst("Omji");  
  17.     queue.addFirst("abhishek");  
  18.     queue.addFirst("Arun");  
  19.     queue.addFirst("subham");  
  20.     queue.addFirst("Maneesh");  
  21.     System.out.println(queue);  
  22.     queue.removeLast();  
  23.     queue.removeLast();  
  24.     System.out.println(queue);  
  25.   }  
  26. }  
OUTPUT
 
linkdemo.jpg
 
Example 2
  1. import java.util.List;  
  2.  import java.util.ArrayList;  
  3.  import java.util.Iterator;  
  4.  import java.util.ListIterator;  
  5.  import java.util.Collections;  
  6.  import java.util.Random;  
  7.  public class ArrayListExample   
  8.  {  
  9.   public static void main(String[] args)  
  10.        {  
  11.              List arraylistA = new ArrayList();  
  12.              List arraylistB = new ArrayList();  
  13.              for (int i = 0; i < 5; i++) {  
  14.              arraylistA.add(new Integer(i));}  
  15.              arraylistB.add("abhishek");  
  16.              arraylistB.add("kumar");  
  17.              arraylistB.add("Dubey");  
  18.              arraylistB.add("MCA");  
  19.              arraylistB.add("IMS");  
  20.              arraylistB.add("work at"):  
  21.              arraylistB.add("MCN Solution");  
  22.              Iterator i1 = arraylistA.iterator();  
  23.              System.out.print("ArrayList arraylistA --> ");  
  24.              while (i1.hasNext()) {  
  25.              System.out.print(i1.next() + " , ");  
  26.              }  
  27.              System.out.println();  
  28.              System.out.print("ArrayList arraylistA --> ");  
  29.              for (int j = 0; j < arraylistA.size(); j++) {  
  30.              System.out.print(arraylistA.get(j) + " , ");  
  31.              }  
  32.             System.out.println();  
  33.             Iterator i2 = arraylistB.iterator();  
  34.             System.out.println("ArrayList arraylistB --> ");  
  35.             while (i2.hasNext()) {  
  36.             System.out.print(i2.next() + " , ");  
  37.              }  
  38.             System.out.println();  
  39.             System.out.println();  
  40.             System.out.println("Using ListIterator to retrieve ArrayList     Elements");  
  41.             System.out.println();  
  42.             ListIterator li1 = arraylistA.listIterator();  
  43.             System.out.println("ArrayList arraylistA --> ");  
  44.             while (li1.hasNext()) {  
  45.             System.out.print(li1.next() + " , ");  
  46.             }  
  47.             System.out.println();  
  48.             int index = arraylistB.indexOf("Dubey");  
  49.             System.out.println("'Dubey' was found at : " + index);  
  50.             int lastIndex = arraylistB.lastIndexOf("Dubey");  
  51.             System.out.println("'Dubey' was found at : " + lastIndex + " from the last");  
  52.             System.out.println();  
  53.             List subList = arraylistA.subList(3, arraylistA.size());  
  54.             System.out.println("New Sub-List(arraylistA) from index 3 to "  
  55.             + arraylistA.size() + ": " + subList);  
  56.             System.out.println();  
  57.             System.out.print("Sorted ArrayList arraylistA --> ");  
  58.             Collections.sort(arraylistA);  
  59.             System.out.print(arraylistA);  
  60.             System.out.println();  
  61.             Collections.reverse(arraylistA);  
  62.             System.out.println(arraylistA);  
  63.             System.out.println();  
  64.             System.out.println("Is arraylistA empty?   " + arraylistA.isEmpty());  
  65.             System.out.println();    
  66.             ArrayList arraylistC = new ArrayList(arraylistA);  
  67.             System.out.println("arraylistA.equals(arraylistC)? "  
  68.                          + arraylistA.equals(arraylistC));  
  69.             System.out.println();  
  70.             Collections.shuffle(arraylistA, new Random());  
  71.             System.out.print("ArrayList arraylistA after shuffling its elements--> "):  
  72.             i1 = arraylistA.iterator();  
  73.             while (i1.hasNext()){  
  74.             System.out.print(i1.next() + " , ");  
  75.             }  
  76.            System.out.println();  
  77.            System.out.println();  
  78.            Object[] array = arraylistA.toArray();  
  79.            for (int i = 0; i < array.length; i++) {  
  80.            System.out.println("Array Element [" + i + "] = " + array[i]);  
  81.            }  
  82.            System.out.println();  
  83.            arraylistA.clear();  
  84.            System.out.println("arraylistA after clearing  : " + arraylistA);  
  85.            System.out.println();  
  86.           }  
  87.     }   
OUTPUT
 
listcmdnew.jpg
  


Similar Articles