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 curriculum: 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 in 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

import java.util.*; 

public class ListDemo

 {

  public static void main(String args[])

    {

    List list = new ArrayList();

    list.add("abhishek");

    list.add("Mani mishra");

    list.add("Neha");

    list.add("Arun");

    list.add("Aditya");

    System.out.println(list);

    System.out.println("2: " + list.get(2));

    System.out.println("0: " + list.get(0));

    LinkedList queue = new LinkedList();

    queue.addFirst("Omji");

    queue.addFirst("abhishek");

    queue.addFirst("Arun");

    queue.addFirst("subham");

    queue.addFirst("Maneesh");

    System.out.println(queue);

    queue.removeLast();

    queue.removeLast();

    System.out.println(queue);

  }

}

OUTPUT

linkdemo.jpg

Example 2

import java.util.List;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.ListIterator;
 import java.util.Collections;
 import java.util.Random;
 public class ArrayListExample
 {
  public static void main(String[] args)
       {
 
            List arraylistA = new ArrayList();
             List arraylistB = new ArrayList();
 
            for (int i = 0; i < 5; i++) {
             arraylistA.add(new Integer(i));}
             arraylistB.add("abhishek");
             arraylistB.add("kumar");
             arraylistB.add("Dubey");
             arraylistB.add("MCA");
             arraylistB.add("IMS");
             arraylistB.add("work at"):
             arraylistB.add("MCN Solution");
 
            Iterator i1 = arraylistA.iterator();
             System.out.print("ArrayList arraylistA --> ");
             while (i1.hasNext()) {
             System.out.print(i1.next() + " , ");
             }
             System.out.println();
             System.out.print("ArrayList arraylistA --> ");
             for (int j = 0; j < arraylistA.size(); j++) {
             System.out.print(arraylistA.get(j) + " , ");
             }
            System.out.println();
            Iterator i2 = arraylistB.iterator();
            System.out.println("ArrayList arraylistB --> ");
            while (i2.hasNext()) {
            System.out.print(i2.next() + " , ");
             }
            System.out.println();
            System.out.println();
            System.out.println("Using ListIterator to retrieve ArrayList     Elements");
            System.out.println();
            ListIterator li1 = arraylistA.listIterator();
            System.out.println("ArrayList arraylistA --> ");
            while (li1.hasNext()) {
            System.out.print(li1.next() + " , ");
            }
            System.out.println();
 
           int index = arraylistB.indexOf("Dubey");
            System.out.println("'Dubey' was found at : " + index);
            int lastIndex = arraylistB.lastIndexOf("Dubey");
            System.out.println("'Dubey' was found at : " + lastIndex + " from the last");
            System.out.println();
 
           List subList = arraylistA.subList(3, arraylistA.size());
            System.out.println("New Sub-List(arraylistA) from index 3 to "
 
           + arraylistA.size() + ": " + subList);
            System.out.println();
 
           System.out.print("Sorted ArrayList arraylistA --> ");
            Collections.sort(arraylistA);
            System.out.print(arraylistA);
            System.out.println();
 
           Collections.reverse(arraylistA);
            System.out.println(arraylistA);
            System.out.println();
 
           System.out.println("Is arraylistA empty?   " + arraylistA.isEmpty());
            System.out.println();  
            ArrayList arraylistC = new ArrayList(arraylistA);
            System.out.println("arraylistA.equals(arraylistC)? "
 
                        + arraylistA.equals(arraylistC));
            System.out.println();
            Collections.shuffle(arraylistA, new Random());
            System.out.print("ArrayList arraylistA after shuffling its elements--> "):
            i1 = arraylistA.iterator();
            while (i1.hasNext()){
            System.out.print(i1.next() + " , ");
            }
           System.out.println();
           System.out.println();
           Object[] array = arraylistA.toArray();
           for (int i = 0; i < array.length; i++) {
           System.out.println("Array Element [" + i + "] = " + array[i]);
           }
           System.out.println();
           arraylistA.clear();
           System.out.println("arraylistA after clearing  : " + arraylistA);
           System.out.println();
          }
    }

OUTPUT

listcmdnew.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all