×
☰ See All Chapters

Java List

  • Every item in this list is numbered with an index. By calling the list and passing it a particular index value, a programmer can pull out any item placed into it. Unlike counting things that exist in the real world, index variables always begin with the number 0.      

  • List is an interface which extends Collection interface. 

  • List allows duplicates. 

  • ArrayList allows duplicate and dissimilar object. 

  • All classes implementing List interface store values using index position. 

  • List has many concrete sub class implementations. 

    1. Vector 

    2. ArrayList 

    3. LinkedList 

    4. Stack 

ArrayList

      • ArrayList uses array data structure internally to store the elements. 

      • ArrayList stores values sequentially, but the values can be accessed randomly. 

      • ArrayList can grow and shrink dynamically. 

      • ArrayList elements can be accessed randomly because it is implementing RandomAccess marker interface. 

      • Elements in ArrayList are ordered by index(In the order as inserted), but not sorted. 

      • ArrayList allows null. 

      • It provides very fast iteration. It is better to choose ArrayList over LinkedList when fast iteration is required. 

      • Insertion & deletion operation at a specified index in ArrayList is time consuming and more complicated. It is  better to choose LinkedList when too many insert and delete operations are being performed. 

Methods defined by ArrayList()

Method

Description

public void trimToSize()

Trims the capacity of this ArrayList instance to be the list's current size. An application can use this operation to minimize the storage of an ArrayList instance.

public void ensureCapacity(int minCapacity)

Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.

protected void removeRange(int fromIndex, int toIndex)

Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by (toIndex - fromIndex) elements. (If toIndex==fromIndex, this operation has no effect.)

 

java-list-0
 

import java.util.ArrayList;

import java.util.List;

 

public class ArrayListDemo {

        public static void main(String[] args) {

                // *****************create an ArrayList*****************

                ArrayList list1 = new ArrayList();

                ArrayList list2 = new ArrayList(5);

                ArrayList<String> list3 = new ArrayList<String>();

               

                // *****************adding elements or values to the ArrayList*****************

                list1.add("C");

                list1.add("A");

                list1.add("E");

                list1.add("B");

                list1.add("D");

                list1.add(4, "G");

                list2.add(list1);

                list3.addAll(list1);

                list3.addAll(4, list1);

               

                // can add duplicates and null values and dissimilar elements in ArrayList

                list1.add("A");// duplicates

                list1.add(null);// null value

                list1.add(40);// dissimilar elements

                System.out.println("Contents of ArrayList list1: " + list1);

                System.out.println("Contents of ArrayList list2: " + list2);

                System.out.println("Contents of ArrayList list3: " + list3);

               

                // *****************Replacing an element*****************

                list1.set(3, "R");

                System.out.println("Contents of ArrayList list1: " + list1);

               

                //***************** Getting the elements*****************

                Object e1 = list1.get(3);

                String s1 = (String) e1;

                System.out.println(s1);

                String s2 = (String) list1.get(3);

                System.out.println(s2);

 

                String s3 = list3.get(3);

                System.out.println(s3);// list3 is generic class

 

                Object o[] = list1.toArray();

                System.out.println(o[0]);

                String s4 = (String) o[5];

                System.out.println(s4);

 

                String s5[] = list3.toArray(new String[list3.size()]);// getting exactly same size as list3

                String s6[] = list3.toArray(new String[5]);// specifying size explicitly

               

                // *****************Searching*****************

                boolean b1 = list1.contains("A");

                System.out.println(b1);

                boolean b2 = list3.containsAll(list1);

                System.out.println(b1);

                int i1 = list1.indexOf("A");

                System.out.println(i1);

                int i2 = list1.lastIndexOf("A");

                System.out.println(i2);

               

                //***************** Removing elements*****************

                String s7 = list3.remove(2);

                System.out.println(s7);// possible only with generic lists

                boolean b3 = list1.remove("C");

                System.out.println(b3);

                // list1.removeRange(4,1);this is not possible here,because removeRange is with

                // protected access

                boolean b4 = list3.removeAll(list1);

                System.out.println(b4);

                boolean b5 = list3.retainAll(list1);

                System.out.println(b4);

                list2.clear();

                System.out.println("Contents of ArrayList list1: " + list1);

                System.out.println("Contents of ArrayList list2: " + list2);

                System.out.println("Contents of ArrayList list3: " + list3);

               

                //***************** Other Operations*****************

                int i3 = list1.size();

                System.out.println(i3);

                // trimToSize(),ensureCapacity() Not present in LinkedList and Vector

 

                list1.trimToSize();

                list1.ensureCapacity(15);

                boolean b6 = list2.isEmpty();

                System.out.println(b6);

                List list4 = list1.subList(0, 4);// return type of sublist() is of type List

        }

}

 

Output:

Contents of ArrayList list1: [C, A, E, B, G, D, A, null, 40]

Contents of ArrayList list2: [[C, A, E, B, G, D, A, null, 40]]

Contents of ArrayList list3: [C, A, E, B, C, A, E, B, G, D, G, D]

Contents of ArrayList list1: [C, A, E, R, G, D, A, null, 40]

R

R

B

C

D

true

true

1

6

E

true

true

true

Contents of ArrayList list1: [A, E, R, G, D, A, null, 40]

Contents of ArrayList list2: []

Contents of ArrayList list3: []

8

true

LinkedList

      • LinkedList uses linked list data structure internally i.e. it uses nodes. 

      • LinkedList is double linked. 

      • LinkedList stores values non-contiguously or randomly. 

      • LinkedList elements can be accessed sequentially only. 

      • It consumes lot of memory. 

      • The main advantage of LinkedList is insertion and deletion operation is very fast and can be performed without affecting other elements. 

      • LinkedList can be used for implementing basic stack and queue data structure. 

      • Iteration in LinkedList is bit slow as compared to ArrayList. 

      • Values are accessed sequentially in LinkedList, hence it is time consuming. 

      • LinkedList consumes more memory 

      • Values will be stored in the same order as inserted. 

      • LinkedList elements cannot be accessed randomly. 

      • LinkedList allows null. 

Entire operations of ArrayList are same for LinkedList, but

      • It has extra method specified by Deque and Queue Interfaces. 

      • Methods Specified by ArrayList are absent. 

      • Has only two Constructors, constructor with initial capacity argument is absent. 

Vector

      • Vector is same as ArrayList but Vector is synchronized and ArrayList is not synchronized. 

      • Vector is a legacy class (Java 1.0) and represents array data structure. 

      • Vector is re-engineered to fit into collection framework and is implementing List interface and hence supports both 1.0 method and 1.2 methods. 

Stack

      • Stack is a legacy class. 

      • It is also re-engineered like Vector to implement List interface. Stack is a sub class of Vector. 

      • It is synchronized. 

      • It is generally used when we want to retrieve the elements of the collection in FILO order (First In Last Out). 

Arrays class

      • Arrays class is present in java.util package. 

      • This class provides various methods like search, sort, fill etc that are useful when working with arrays. 

      • We can convert an array to ArrayList by using Arrays.asList(intArray). 

Integer intArray[] = new Integer[5];

intArray [0] = new Integer(102);

intArray [1] = new Integer(104);

intArray [2] = new Integer(103);

List list = Arrays.asList(intArray);

System.out.println("Value in list" + list);

      • We can do sorting in arrays by using the sort() method of class Arrays. (Sorting will be studied in next chapter.) 

 


All Chapters
Author