E Lists In Java – Code Example

Total
0
Shares

Introduction

E lists are better referred to as Interface List <E> where E is the type of elements in this list. Classes that implement the Interface List <E> include: AbstractList, AbstractSequentialList, ArrayList, AttributeList, CopyOnWriteArrayList, LinkedList, RoleList, RoleUnresolvedList, Stack, and Vector.

Code Example

// Java Program to traverse the list both in forward and
// backward direction using listIterator

import java.util.*;

class Elist {

	public static void main(String[] args)
			{
	    //ArrayList Class Example
		//Creating a List
		List<String> list= new ArrayList<String>();
		//Adding elements in the List
		
		list.add("Mango");
		list.add("Apple");
		list.add("Banana");
		list.add("Grapes");
		//Iterating the List element using for-each loop
		for(String fruit:list)
			System.out.println(fruit);
			
		//LinkedList Class Example	
		// list of names
		List<String> names = new LinkedList<>();
		names.add("Walk");
		names.add("Study");
		names.add("Practice Java");

		// Getting ListIterator
		ListIterator<String> listIterator
			= names.listIterator();

		// Traversing elements
		System.out.println("Forward Direction Iteration:");
		while (listIterator.hasNext()) {
			System.out.println(listIterator.next());
		}

		// Traversing elements, the iterator is at the end
		// at this point
		System.out.println("Backward Direction Iteration:");
		while (listIterator.hasPrevious()) {
			System.out.println(listIterator.previous());
		}
	}
}

The code above presents two class Examples: the ArrayList and the LinkedList class.

In the Array List, a list of fruit names is created and the add method is used. Subsequently, the list element is iterated using the forEach loop.

On the Linked List, a list of activities is created and the add method is used. The ListIterator method is then used to rearrange the activities using the bidirectional property.

Digging In;

The advantage of using the interface is that the user has precise control over where in the list each element is inserted. The user can therefore access elements by their integer index (position in the list), and search for elements in the list.

Lists allow duplicate elements. More formally, lists typically allow pairs of elements e1 and e2 such that e1.equals(e2), and they typically allow multiple null elements if they allow null elements at all. It is not inconceivable that someone might wish to implement a list that prohibits duplicates, by throwing runtime exceptions when the user attempts to insert them, but we expect this usage to be rare.

The List interface includes normal collection stipulations such as add, remove, equals, and hashCode methods. Declarations for other inherited methods are also incorporated.

The List interface provides four methods for positional (indexed) access to list elements. Lists like Java arrays are zero-based. Note that these operations may execute in time proportional to the index value for some implementations (the LinkedList class, for example). Thus, iterating over the elements in a list is typically preferable to indexing through it if the caller does not know the implementation.

The List interface provides a special iterator, called a, ListIterator that allows element insertion and replacement, and bidirectional access in addition to the normal operations that the Iterator interface provides. A method is provided to obtain a list iterator that starts at a specified position in the list.

In many implementations, the list interface will perform costly linear searches.

The List interface provides two methods to efficiently insert and remove multiple elements at an arbitrary point in the list.

However, Attempting to add an ineligible element throws an unchecked exception, which typically is NullPointerException or ClassCastException.

Live Demo

Open Live Demo