Java - List Interface and Related Classes

List Interface

The List interface is an interface in the Java Collection Framework for handling ordered groups of objects. It inherits from the Collection interface, the top-level interface of the Java Collection Framework, and it can contain duplicate data. Subclasses that implement the List interface include ArrayList, LinkedList, Vector, and Stack.

  • List
    • ArrayList
    • LinkedList
    • Vector
      • Stack

List Methods

Method Description
void add(int index, E element) Adds an object(element) at index.
boolean addAll(int index, Collection<? extends E> c) Adds a collection object at index.
E get(int index) Returns the object at index in the List.
int indexOf(Object o) Searches forward from the first element of the List and returns the index(position) of object o. Returns -1 if the object is not in the List.
int lastIndexOf(Object o) Searches backward from the last element of the List and returns the index(position) of object o. Returns -1 if the object is not in the List.
ListIterator<E> listIterator() Returns a ListIterator that can access the objects in the List.
ListIterator<E> listIterator(int index) Returns a ListIterator that can access the objects in the List starting from index.
E remove(int index) Deletes the object at index and returns the deleted object.
Object set(int index, E element) Stores an object at index and returns the previous object that was at index.
List<E> subList(int fromIndex, int toIndex) Returns a List object from fromIndex(inclusive) to toIndex(exclusive).

ListIterator Interface

ListIterator is an interface that extends Iterator. The Iterator interface provided by Collection obtains information about objects while traversing a Collection in one direction. ListIterator is an interface provided by List, and it provides a way to retrieve objects while traversing all objects contained in a List in both directions. Using ListIterator, you can move in both directions, modify elements, and check the current position of the Iterator. When using a List, you can use not only the Iterator provided by Collection but also the more advanced ListIterator.

ListIterator Methods

Method Description
boolean hasNext() Returns whether an object exists in the next direction of the Iterator.
boolean hasPrevious() Returns whether an object exists in the previous direction of the Iterator.
Object next() Returns the next object in the iteration.
Object previous() Returns the previous object in the iteration.
void remove() Removes the last object returned by next() or previous().
void set(Object o) Replaces the last object returned by next() or previous() with the entered object(o).
int nextIndex() Returns the index of the next object in the iteration.
int previousIndex() Returns the index of the previous object in the iteration.
void add(Object o) Adds an object(o) to the List.

ArrayList Class

ArrayList is a class implemented as a resizable array. Except for its resizable nature, it can be used like a normal array and has similar performance. Because random access is possible, get()/set() performance is good, while add()/remove() performance is poor.

ArrayList changes size dynamically. When more data than the configured initial size is stored, its size automatically increases, and when stored data is deleted, its size automatically decreases.

ArrayList Constructors

Constructor Description
ArrayList() Default constructor. Creates an object with the default size.
ArrayList(Collection<? extends E> c) Creates an object from an entered Collection.
ArrayList(int initialCapacity) Creates an object with the specified capacity.

ArrayList Example

package com.devkuma.tutorial.java.util;

import java.util.ArrayList;
import java.util.List;

public class ArrayListClass {

    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();

        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");
        list.add("E");
        list.add("F");

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

        list.set(2, "CC");
        System.out.println("set : " + list);

        list.add(1, "AA");
        System.out.println("add : " + list);

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

        for (String str : list) {
            System.out.println("for : " + str);
        }
    }
}

The execution result is as follows.

list :[A, B, C, D, E, F]
size : 6
get : A
set : [A, B, CC, D, E, F]
add : [A, AA, B, CC, D, E, F]
remove : [A, AA, CC, D, E]
for : A
for : AA
for : CC
for : D
for : E

LinkedList Class

LinkedList is a class implemented as a doubly linked list. Because it is a List, its performance for get()/set() is not good.

Vector Class(Legacy)

Vector, a legacy class in java.util.Collection, is the same as ArrayList. The difference is that Vector is synchronized. Because of this, it is at a performance disadvantage compared with ArrayList. Many Java programmers use ArrayList instead of Vector, and even when synchronization is needed, they prefer explicitly synchronizing ArrayList. In other words, Vector is not used very often.

References

http://hochulshin.com/java-collection-list/
http://scarlett.tistory.com/entry/자바공부-5List-인터페이스와-ListIterator-인터페이스