The List interface in java

List interface

List are like sets. They store a group of elements. But lists allows duplicate values to be stored.

1. Stack class

A stack represents a group of elements stored in LIFO (Last in First Out) order.This means that the element which is stored as a last element into the stack will be the first element to be removed from the stack. Inserting elements into the stack is called ‘push’ operation and removing elements from stack is called ‘pop’ operation. Searching for an element in the stack is called ‘peep’ operation.

We write a stack class as:

class stack<E>

where E stand for element type. We can create a stack object as:

Stack<Integer> obj = new Stack<Integer>();

Stack class methods 

Stack class has the following methods:

boolean empty()
It tests stack is empty or not. It returns true if the stack is empty, and returns false if the stack contains elements.

element peek( )
It returns the element on the top of the stack without remove it.

element pop( )
It returns the element on the top of the stack and return it.

element push(element obj)
This method pushes the element onto the stack and return that element.

int search(Object obj)
This method is used for search the element in the stack. If found, its offset from the top of the stack is returned. Otherwise, -1 is returned.

Write a program to perform different operation on stack
import java.io.*;
import java.util.Stack;
class test
{
public static void main(String args[]) throws Exception
{
Stack st = new Stack();
int ch=0;
int position, element;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(ch<4)
{
System.out.println(“Stack Operations”);
System.out.println(“1.Push an element”);
System.out.println(“2.Pop an element”); System.out.println(“3.Search an element”); System.out.println(“4.Exit”);
System.out.println(“Enter your choice”);
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println(“Enter element”); element=Integer.parseInt(br.readLine());
st.push(element);
break;
case 2:
int obj = st.pop();
System.out.println(“Poped element”+obj);
break;
case 3:
System.out.println(“which element”);
element=Integer.parseInt(br.readLine());
position=st.search(element);
if(position == -1)
System.out.println(“Element not found”);
else
System.out.println(“Position =”+position);
break;
default:
return;
}
System.out.println(“Stack contents:”+st);
}
}
}

Output:
Stack Operations
1.Push an element
2.Pop an element
3.Search an element
4.Exit
Enter your choice
1
Enter element
25
Stack contents:[25] Stack Operations
1.Push an element
2.Pop an element
3.Search an element
4.Exit
Enter your choice
1
Enter element
10
Stack contents:[25, 10] Stack Operations
1.Push an element
2.Pop an element3
.Search an element
4.Exit
Enter your choice
3
which element
25
Position =2
Stack contents:[25, 10] Stack Operations
1.Push an element
2.Pop an element
3.Search an element
4.Exit
Enter your choice
4

2. LinkedList class

A LinkedList represent a group of elements in the form of nodes. Each node will have three fields.

LinkedList is very convenient to store data. LinkedList provides adding or removing an element at the end & adding & removing an element in the middle.

A LinkedList is written in the form of:

class LinkedList<E>

We can create an object of LinkedList as:

LinkedList<String> ll = new LinkedList<String>();

LinkedList class methods

void add(int position, element obj)
This methos inserts the specified element at the specified position in this list.

boolean add(element obj)
This method appends the specified element to the end of this list.

void addFirst(element obj)
This method inserts the given element at the beginning of this list.

void addLast(element obj)
This method appends the given element to the end of this list.

void clear()
This method removes all of the elements from this list.

element get(int position)
It returns the element at the specified position in this linked list.

element getFirst()
It returns the first element from the list.

element getLast()
It returns the last element from the list.empty.

int indexOf(Object obj)
It returns the index of the first occurrence of the specified elementin the list, or -1 if the list does not contain this element.

int lastIndexOf(Object o)
It returns the index of the last occurrence of the specified element in the list, or -1 if the list does not contain this element.

element remove(int position)
It removes the element at the specified position in this list.

element removeFirst()
It removes and returns the first element from this list.

element removeLast()
It removes and returns the last element from this list.

element set(int postion, element obj)
It replaces the element at the specified position in this list with the specified element.

int size()
It returns the number of elements in this list.

Object[] toArray()
This method converts the linked list into an array of object class type.

Write a program that shows the use of LinkedList class
import java.io.*;
import java.util.LinkedList;
import java.util.Iterator;
class test
{
public static void main(String args[]) throws IOException
{
LinkedList ll = new LinkedList();
ll.add(“India”);
ll.add(“America”);
ll.add(“Singapur”);
System.out.println(“List is”+ll);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int position, ch=0;
String element;
while(ch<4)
{
System.out.println(“LinkedList Operations”);
System.out.println(“1.Add an element”);
System.out.println(“2.Remove an element”);
System.out.println(“3.Change an element”);
System.out.println(“4.Exit”); System.out.println(“Enter your choice”);
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println(“Enter element”);
element=br.readLine();
System.out.println(“Enter position”);
position=Integer.parseInt(br.readLine());
ll.add(position-1, element);
break;
case 2:
System.out.println(“Enter position”);
position=Integer.parseInt(br.readLine());
ll.remove(position-1);
break;
case 3:
System.out.println(“Enter position”);
position=Integer.parseInt(br.readLine());
System.out.println(“Enter new element”);
element=br.readLine();
ll.set(position-1,element);
break;
default:
return;
}
System.out.println(“List contents:”+ll);
Iterator it = ll.iterator();
while(it.hasNext())
System.out.println(it.next()+””);
}
}
}

Output:
List is[India, America, Singapur] LinkedList Operations
1.Add an element
2.Remove an element
3.Change an element
4.Exit
Enter your choice
1
Enter element
Japan
Enter position
1
List contents:[Japan, India, America, Singapur] Japan
India
America
Singapur
LinkedList Operations
1.Add an element
2.Remove an element
3.Change an element
4.Exit
Enter your choice
2
Enter position
4
List contents:[Japan, India, America] Japan
India
America
LinkedList Operations
1.Add an element
2.Remove an element
3.Change an element
4.Exit
Enter your choice
3
Enter position
3
Enter new element
Germany
List contents:[Japan, India, Germany] Japan
India
Germany
LinkedList Operations
1.Add an element
2.Remove an element
3.Change an element
4.Exit
Enter your choice
4

3. ArrayList class

An arrayList is like an array, which can grow dynamically. It means that when we store elements into the ArrayList, depending on the number of elements, the memory is dynamically allotted and re-allotted to accommodate all the elements. ArrayList is not synchronized.

The ArrayList class class can be written as :

class ArrayList<E>

We can create an object to ArrayList as:

ArrayList<String> arl = new ArrayList<String>();

ArrayList class methods

void add(int position, element obj)
It inserts the specified element at the specified position in ArrayList.

boolean add(element obj)
It appends the specified element to the end of ArrayList.

void clear()
It removes all of the elements from this list.

boolean contains(element obj)
It returns true if this list contains the specified element.

element get(int position)
It returns the element at the specified position in ArrayList.

int indexOf(Object obj)
It returns the position of the first occurrence of the specified element in the ArrayList, or -1 if the List does not contain this element.

int lastIndexOf(Object obj)
It returns the position of the last occurrence of the specified element in the ArrayList, or -1 if the list does not contain this element.

Object remove(int position)
It removes the element at the specified position in this list.

Object set(int position, element obj)
It replaces the element at the specified position in this list with the specified element.

int size()
It returns the number of elements in the ArrayList.

Object[] toArray()
It returns an array containing all of the elements in the ArrayList in the correct order.

Write a program to create an ArrayList with various operation on it
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
class test
{
public static void main(String args[]) throws IOException
{
ArrayList arl = new ArrayList();
arl.add(“India”);
arl.add(“America”);
arl.add(“Singapur”);S
ystem.out.println(“ArrayList is “+arl);
arl.remove(2);
arl.remove(“Singapur”);
System.out.println(“ArrayList after removing”+arl);
System.out.println(“Size of ArrayList is “+arl.size());
System.out.println(“Extracting using Iterator”);
Iterator it = arl.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}

Output:
ArrayList is [India, America, Singapur] ArrayList after removing[India, America] Size of ArrayList is 2
Extracting using Iterator
India
America

4. Vector class

A Vector also stores elements similer to ArrayList, but Vector is synchronized. It means even if several threads act on Vector object simultaneously.

We can write Vector class as:

class Vector<E>

We can create object as:

Vector<float> v = new Vector<float>();

Vector class methods

boolean add(element obj)
It appends the specified element to the end of the Vector.

void add(int position, element obj)
It inserts the specified element at the specified position in the Vector.

element remove(int position)
It removes the element at the specified position in the vector.

boolean remove(Object obj)
It removes the first occurrence of the specified element in the vector, If the vector does not contain the element, it remains unchanged.

void clear()
It removes all of the elements from the vector.

boolean contains(Object obj)
It returns true if the Vector contains the specified element obj.

Object set(int position, element obj)
It replaces the element at the specified position in the vector with the specified element.

Object get(int position)
It returns the element at the specified position in the vector.

int indexOf(Object obj)
It searches for the first occurence of the given argument, testing for equality using the equals method.

int lastIndexOf(Object obj)
It returns the index of the last occurrence of the specified object in the vector.

int size()
It returns the number of components in this vector.

Object[] toArray()
It returns an array containing all of the elements in the vector in the correct order.

int capacity()
It returns the current capacity of the vector.

Write a program that shows the use of Vector class
import java.util.ListIterator;
import java.util.Vector;
class test
{
public static void main(String args[])
{
Vector v = new Vector();
int x[]={10,20,30,40,50};
for(int i=0; i<x.length; i++)
{
v.add(x[i]);
}
System.out.println(“Vector elements “);
for(int i=0; i<v.size(); i++)
{
System.out.println(v.get(i));
}
System.out.println(“Element using ListIterator”);
ListIterator lit = v.listIterator();
System.out.println(“In forward direction”);
while(lit.hasNext())
{
System.out.print(lit.next()+”\t”);
}
System.out.println(“\nIn backward direction”);
while(lit.hasPrevious())
{
System.out.print(lit.previous()+”\t”);
}
}
}

Output:
Vector elements
10
20
30
40
50
Element using ListIterator
In forward direction
10 20 30 40 50
In backward direction
50 40 30 20 10

Leave a Reply

Your email address will not be published. Required fields are marked *