Output of the above program:

IteratorDemo.Java Output

Limitation of Iterator

It is unidirectional
cursor hence can only move forward.

ListIterator

ListIterator is given to
provide facilities like moving backward and forward in a list, adding an object, replacing
an object etc. This cursor can be operated on any List Object. It is a bi-directional cursor.

Methods of ListIterator Interface

public boolean hasNext()

These methods are provided to facilitate
the forward direction relation operation.

public Object next()

public int lastIndex()

public boolean hasPrevious()

These methods are provided to facilitate
the backward direction relation operation.

public Object previous()

public int previousIndex()

public void add(Object obj)

These methods are provided to facilitate
misc. Operations.

public void set(Object obj)

public void remove()

How to get ListIterator object ?

To get a ListIterator object we should use listIterator() method of List Interface.

Tuesday, September 23, 2014

Stack is the subclass of Vector and implements Serializable, Clonable, Collection and List interface. Stack is the representation of LIFO data structure in Java.

Underlined data structure is Stack (LIFO).

Heterogeneous data elements are allowed.

Null insertion is allowed.

Duplicates are allowed.

Insertion order is preserved.

Constructor

Stack s=new Stack() //Creates an empty Stack

Methods

push(Object obj)

Insert an element on top of Stack

pop()

Remove and return an element from the top
of Stack.

peek()

Return an element from the top of Stack.

search(Object o)

Return the offset if the passed object is
found in Stack, -1 otherwise.

Example:

import java.util.*;
class StackDemo
{public static void main(String[] args){Stack s=new Stack();for(int i=1;i<=5;i++){s.push(i);}System.out.println(s.peek()); //Returns the element at the top of stackSystem.out.println(s.pop()); //Remove and returns the element from top of StackSystem.out.println(s.search(3));System.out.println(s.search(10));}
}

Output of the above program :

Diagrammatic representation of the above program :

Note

If there are duplicate elements in the stack search(Object ob) method will return the offset of the element which is nearest to top of stack.

When to use Stack

Stack is useful when our requirement is to retrieve the element which came last or which added last. In other terms we can say that if we want to retrieve the latest inserted element then we should go for Stack.

Did you know from java 1.8 you can define a method inside an interface?

As per the explanation from Oracle

An example that involves manufacturers of
computer-controlled cars who publish industry-standard interfaces that describe
which methods can be invoked to operate their cars. What if those
computer-controlled car manufacturers add new functionality, such as flight, to
their cars? These manufacturers would need to specify new methods to enable
other companies (such as electronic guidance instrument manufacturers) to adapt
their software to flying cars. Where would these car manufacturers declare
these new flight-related methods? If they add them to their original
interfaces, then programmers who have implemented those interfaces would have
to rewrite their implementations. If they add them as static methods, then
programmers would regard them as utility methods, not as essential, core
methods.

Default methods enable you to add new functionality to the
interfaces of your libraries and ensure binary compatibility with code written
for older versions of those interfaces.

Few important points.

To make a method as default method it must be
started with the keyword default

You need not to override default method in the implementation
class.

Default methods are by default available in the implementation
class and can be invoked using an object of that class.

Heterogeneous data elements are allowed.

Null insertion is allowed.

Duplicates are allowed.

Insertion order is preserved.

Constructor In ArrayList

Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

Methods In ArrayList
In addition to methods which are available in List, Collection interface, there are several LinkedList specific methods which you can be used only on LinkedList object.

Void addFirst(Object o) //Adds a new node at first place

Void addLast(Object o) //Adds a new node at last place

Object removeFirst() //remove and return the object at first node

Object removeLast() //remove and return the object at last node

Object getFirst() //return the object at first node

Object getLast() //return the object at last node

//Sample Program For LinkedList

import java.util.*;

class LinkedListDemo

{

public static void main(String [] args)

{

LinkedList l=new LinkedList();

l.add("10");

l.add("10");

l.add(null);

l.add(1,100);

l.set(1,"DHT");

l.addFirst("10");

System.out.println(l);

}

}

Output of the above program

Scenarios When To Choose LinkedList And When Not To.

LinkedList is best choice when our main function is insertion at middle or deletion from middle. LinkedList could be worst choice if our main function is retrial of data from middle of the List.

More details:

If we want to insert new node at 2 place, we need not to perform shift operation.

If you want to go to Node 2 of the list , it will first go to node 1 since node 1 has the address of node 2 then it will go to node 2.

Think of the same problem when we have 10 millions elements in the LinkedList and at some point if you want to retrieve some data element at 10000 position, it will have to go through 9999 nodes starting from node 1.

Retrieval of data from the middle of list is a performance violating task in LinkedList.