Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Sunday, 30 April 2017

Java List

 

List Interface

Java List

List is an interface in java collection framework. List interface can contains duplicate elements. List interface can maintain the insertion order of elements.

In List, we can insert and delete elements on the basis of index by using list interface's methods.

List interface allows n number of null values.

There are 4 classes which implements List interface in java. These are

Methods of List Interface In Java

There are some useful methods of list interface in java.

1) void add(int index, Object element)

This method is used to insert the elements into the list at the index passed in the index.

2) boolean addAll(int index Collection c)

This method is used to insert all the elements of c into the list at the index passed in the index.

3) object get(int index)

Used to return the object which is stored at the specified index in the collection.

4) object set(int index, Object element)

Used to set the elements to specified index in the list.

5) object remove(int index)

It is used to remove the particular element at position index and return removed elements  from the list.

6) ListIterator listIterator()

It is used to return an iterator to the begging of the list.

7) ListIterator listIterator(int index)

It is used to return an iterator from the list that starts at specified index.


Java List Example

import java.util.*;
class ListExample
{
public static void main(String args[])
{
ArrayList<String> al = new ArrayList<String>();
al.add("ram");
al.add("hanuman");
al.add("ram");
al.add(1, "sita");//add sita at first index in the list
for(String s : al)
{
System.out.println(s);
}
}
}

output : ram
              sita
              hanuman
              ram


ListIterator Interface

ListIterator is an interface. ListIterator is a sub-interface of an iterator interface in java. ListIterator interface is used to iterate the elements in both direction i.e forward and backward direction.

We can traverse an elements of List and Set by using "iterator" interface but by using "ListIterator" interface we can traverse only "List" interface. 

ListIterator interface works  specially for List.


Methods of ListIterator Interface

1) boolean hasNext()

Returns true if the list iterator has more elements when traversing the list in the forward direction.

2) Object next()

Returns the next element in the list.

3) boolean hasPrevious()

Returns true if the list iterator has more elements when traversing the list in the reverse direction.

4) Object previous()

Returns the previous elements in the list.

5) Object previousIndex()

This method returns index of the element that could be returned by subsequent call to previous method.


Example of ListIterator Interface

There are simple example of ListIterator in collection . In this ListIterator program we will traverse  the list in backward and forward direction(both).

import java.util.*;
class ListIteratorExample
{
public static void main(String args[])
{
ListIterator<String> litr = null;
List<String> l = new ArrayList<String>();
l.add("siya");
l.add("sid");
l.add("simmu");
l.add("barkha");
//obtaining list iterator
litr = l.listIterator();
System.out.println("Traversing list in forward direction");
while(litr.hasNext())
{
System.out.println(litr.next());
}
System.out.println("Traversing list in backward direction");
while(litr.hasPrevious())
{
System.out.println(litr.previous());
}
}
}

output: Traversing list in forward direction
              siya
              sid
              simmu
              barkha
             Traversing list in backward direction
             barkha
             simmu
             sid
             siya
Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate