List implementations

List
A List represents a data structure which allows to dynamically add, access and remove objects of the same type. Adding objects to the list is usually done via the add() method. The get(int i) method allows to retrieve the element at position i.
List implementations are grouped into general-purpose and special-purpose implementations.
1. General-Purpose List Implementations: - There are two general-purpose List implementations — ArrayList and LinkedList. ArrayList, offers constant-time positional access and is just plain fast. It does not have to allocate a node object for each element in the List, and it can take advantage of System.arraycopy when it has to move multiple elements at the same time. Think of ArrayList as Vector without the synchronization overhead.
ArrayList has one tuning parameter — the initial capacity, which refers to the number of elements the ArrayList can hold before it has to grow. LinkedList has no tuning parameters and seven optional operations, one of which is clone. The other six are addFirst, getFirst, removeFirst, addLast, getLast, and removeLast. LinkedList also implements the Queue interface.
Example: - 1. Implement Array List Structure in Java:-
/*
Swap elements of Java ArrayList example
This java example shows how to swap elements of Java ArrayList object using
swap method of Collections class.
*/
import java.util.ArrayList;
import java.util.Collections;
class SwapElementsOfArrayListExample {
public static void main(String[] args) {
//create an ArrayList object
ArrayList arrayList = new ArrayList();
//Add elements to Arraylist
arrayList.add("A");
arrayList.add("N");
arrayList.add("U");
arrayList.add("J");
arrayList.add("B");
System.out.println("Before swaping, ArrayList contains : " + arrayList);
/*
To swap elements of Java ArrayList use,
static void swap(List list, int firstElement, int secondElement)
method of Collections class. Where firstElement is the index of first
element to be swapped and secondElement is the index of the second element
to be swapped.
If the specified positions are equal, list remains unchanged.
Please note that, this method can throw IndexOutOfBoundsException if
any of the index values is not in range.
*/
Collections.swap(arrayList,0,4);
System.out.println("After swaping, ArrayList contains : " + arrayList);
}
}
Output: -
C:\java\word>javac SwapElementsOfArrayListExample.java
C:\java\word>java SwapElementsOfArrayListExample
Before swaping, ArrayList contains : [A, N, U, J, B]
After swaping, ArrayList contains : [B, N, U, J, A]
Example: - 2. Implement Linked List Structure in Java:-
import java.util.*;
import java.lang.*;
class LinkedListDemo {
public static void main(String args[]) {
// create a linked list
LinkedList ll = new LinkedList();
// add elements to the linked list
ll.add("F");
ll.add("B");
ll.add("D");
ll.add("E");
ll.add("C");
System.out.println("Original contents of ll: " + ll);
// remove elements from the linked list
ll.remove(2);
System.out.println("Contents of ll after deletion: "+ ll);
}
}
Output: -
C:\java\word>javac LinkedListDemo.java
C:\java\word>java LinkedListDemo
Original contents of ll: [F][B][D][E][C]
Contents of ll after deletion: [F][D][E][C]
2.Special-Purpose List Implementations: - CopyOnWriteArrayList is a List implementation backed up by a copy-on-write array. This implementation is similar in nature to CopyOnWriteArraySet. No synchronization is necessary, even during iteration, and iterators are guaranteed never to throw ConcurrentModificationException. This implementation is well suited to maintaining event-handler lists, in which change is infrequent, and traversal is frequent and potentially time-consuming.
If you need synchronization, a Vector will be slightly faster than an ArrayList synchronized with Collections.synchronizedList. But Vector has loads of legacy operations, so be careful to always manipulate the Vector with the List interface or else you won't be able to replace the implementation at a later time.
If your List is fixed in size — that is, you'll never use remove, add, or any of the bulk operations other than containsAll — you have a third option that's definitely worth considering.
Example: -
import java.util.Iterator;
import java.util.Vector;
class VectorIterator {
public static void main(String a[]){
Vector vct = new Vector();
//adding elements to the end
vct.add("First");
vct.add("Second");
vct.add("Third");
vct.add("Random");
Iterator itr = vct.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
Output: -
C:\java\word>javac VectorIterator.java
C:\java\word>java VectorIterator
First
Second
Third
Random


Free Web Hosting