List In Java

List is a sequence of elements. In Java list can be implemented using List interface.

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

List In Java

In this section we will read about the List data structure in Java and how the List data structure can be implemented in Java.

List data structure is implemented in Java using List interface. List interface allows to insert the element at the specified index as well as to access the element from specified index. Using this interface the elements can also be searched into list. A list may contain the duplicate elements. The List interface provides methods for performing operations like add elements, remove elements, iterate over elements etc. List is indexed based implementation like an array. LinkedList class is one of the implementation among most of the implementation of List interface, it provides an iterator called ListIterator to access elements bi-directional, and allows for inserting and replacing the elements.

Methods Of List Interface

  • boolean add(E e) : This method adds the specified element at end of the list.

    Syntax : boolean add(E e)
     
  • void add(int index, E element) : This method adds the given element at the given position in the list.

    Syntax : void add(int index, E element)
     
  • boolean addAll(Collection<? extends E> c) : This method adds the all the elements in the given Collection at the end of the list.

    Syntax : boolean addAll(Collection<? extends E> c)
     
  • boolean addAll(int index, Collection<? extends E> c) : This method adds all the elements in the given Collection at the given position in the list.

    Syntax : boolean addAll(int index, Collection<? extends E> c)
     
  • void clear() : This method clears all the elements from the list.

    Syntax : void clear()
     
  • boolean contains(Object o) : This method is used to check whether the given object is present in the list or not. It returns true if element is present otherwise, false.

    Syntax : boolean contains(Object o)
     
  • boolean containsAll(Collection<?> c) : This method is used to check for all the elements of a collection that whether they are present in the list or not. It returns true if all elements are present otherwise, false.

    Syntax : boolean containsAll(Collection<?> c)
     
  • boolean equals(Object o) : This method checks the given element that whether, it is equal to any of the element in a list.

    Syntax : boolean equals(Object o)
     
  • E get(int index) : This method is used to find the element at the given index in the list.

    Syntax : E get(int index)
     
  • int hashCode() : This method gives the hash code of list.

    Syntax : int hashCode()
     
  • int indexOf(Object o) : This method gives the position of first occurrence of the given element in the list or -1 if the element is not present.

    Syntax : int indexOf(Object o)
     
  • boolean isEmpty() : This method is used to check whether the list is contains element or not.

    Syntax : boolean isEmpty()
     
  • Iterator<E> iterator() : This method is used to iterate over the element in proper sequence.

    Syntax : Iterator iterator()
     
  • int lastIndexOf(Object o) : This method is used to find the position of last occurrence of the given element in the list or -1 if the element is not present.

    Syntax : int lastIndexOf(Object o)
     
  • ListIterator<E> listIterator() : This method is used to get the list iterator over element in the list.

    Syntax : ListIterator<E> listIterator()
     
  • ListIterator<E> listIterator(int index) : This method is used to get the list iterator over element in the list starts from the given index.

    Syntax : ListIterator<E> listIterator(int index)
     
  • E remove(int index) : This method is used to remove elements at the given position from the list.

    Syntax : E remove(int index)
     
  • boolean remove(Object o) : This method is used to remove the given elements from the list.

    Syntax : boolean remove(Object o)
     
  • boolean removeAll(Collection<?> c) : This method is used to remove all the elements from list contained by a given collection.

    Syntax : boolean removeAll(Collection<?> c)
     
  • boolean retainAll(Collection<?> c) : This method is used to remove all elements from the list except the elements contained by a given collection.

    Syntax : boolean retainAll(Collection<?> c)
     
  • E set(int index, E element) : This method replaces the given element at the given position.

    Syntax : E set(int index, E element)
     
  • int size() : This method gives the number of available elements in the list.

    Syntax : int size()
     
  • List<E> subList(int fromIndex, int toIndex) : This method is used create a sublist from the list started from the given index and ends with the given index. In the sublist the starting index element is inclusive whereas, the toIndex is exclusive.

    Syntax : List subList(int fromIndex, int toIndex)
     
  • Object[] toArray() : This method is used to convert a list to an array that contains all the elements in proper sequence.

    Syntax : Object[] toArray()
     
  • T[] toArray(T[] a) : This method is used to convert a list to an array that contains all the elements in proper sequence.

    Syntax : T[] toArray(T[] a)
     

Example

Here an example is being given which will demonstrate you about how to list data structure can be implemented in Java. In this example we will create a Java class where I will use the List interface and its implementing class LinkedList. Then we will insert some elements to list and then we will use methods on the list object. We will fetch element at the specified index from the list, iterate over the list, replace the specified list and many more task we will perform on the list.

ListInJavaExample.java

import java.util.List;
import java.util.LinkedList;
import java.util.ListIterator;
public class ListInJavaExample {

	public static void main(String args[])
	{
		List list = new LinkedList();
		//add elements to the list.
		list.add("Java");
		list.add(".Net");
		list.add("C");
		list.add("C++");
		
		//iterate over the element
		ListIterator litr = list.listIterator();
		System.out.println("Elements In List In Forward Order:");
		while(litr.hasNext())
		{
			//iterate in forward direction
			System.out.print(litr.next()+",");
		}
		System.out.println("\nElements In List In Backward Order");
		while(litr.hasPrevious())
		{
			System.out.print(litr.previous()+",");
		}
		//insert new element at the specified index
		list.add(3, "Fortran");
		ListIterator litr1 = list.listIterator();
		System.out.println("\nElements In List After adding new element at specified index:");
		while(litr1.hasNext())
		{
			//iterate in forward direction
			System.out.print(litr1.next()+",");
		}
		//replace element at the specified index
		list.set(3, "Pascal");
		
		ListIterator litr2 = list.listIterator();
		System.out.println("\nElements In List After replacing new element at specified index:");
		while(litr2.hasNext())
		{
			//iterate in forward direction
			System.out.print(litr2.next()+",");
		}		
		//check for the availability of elements
		boolean bol = list.contains("Pascal");
		System.out.println("\nWhether The Element 'Pascal' Is Available In List : "+bol);
	}
}

Output

When you will compile and execute the above example you will get the output as follows :

Download Source Code

Advertisements

Share on Google+Share on Google+

List In Java

Posted on: August 13, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: List In Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads