Friday, January 5, 2018

Understand the difference among the Java cursors by using their properties

As we have discussed cursors defination ,  below are the differentiation among the cursors by using their properties.

Properties Enumeration Iterator ListIterator
1. where we can apply legacy classes only any collection object only for List object
2. Its legacy ? Yes(1.0 version) No(1.2 version) No(1.2 version)
3. Movement directioin single direction forward single direction forward both direction
4. Allowed opeation only read only read , remove read/remove/replace/add
5. how we can get ? by using elements() of Vector class by using iterator() of Collection interface by using listIterator() of List Interface
6. methods hashMoreElements() hashNext()  having 9 methods for both direction
nextElement() next()
remove()


Internal implementation of cursors Example :

package indrajeet.singh.cursors.imp;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Vector;

public class CursorsDemo {

public static void main(String[] args) {
Vector v=new Vector();
Enumeration e=v.elements();
Iterator itr=v.iterator();
ListIterator litr=v.listIterator();

System.out.println(e.getClass().getName());
System.out.println(itr.getClass().getName());
System.out.println(litr.getClass().getName());


}

}

Output:
java.util.Vector$1
java.util.Vector$Itr
java.util.Vector$ListItr

Thursday, January 4, 2018

Collection Cursors in Java

There are three types of cursors in Collection. which are used for getting element one by one.
 1. Enumeration -> Legacy classes
 2. Iterator          -> any collection (universal cursor)
 3. ListIterator    -> List

If we want to get objects one by one from the collection in the java then we should go for  above cursors.

 1. Enumeration :

We can use Enumeration to get objects one by one from the legacy classes.
We can enumerate objects by using Elements() method of Vector class.

public Enumeration elements();

Example : Enumeration e=v.elements().

 v is a vector object.

methods:
public Boolean hasMoreElements();
public object nextElement();

Example :

package indrajeet.list.exp;

import java.util.Enumeration;
import java.util.Vector;

public class VectorExp {

public static void main(String[] args) {


Vector v=new Vector<>();
System.out.println(v.capacity());
v.add("indraeet");
v.addElement("singh");
v.add(1, 4);
System.out.println(v);
v.removeElementAt(1);

System.out.println(v);
System.out.println(v.size());

Enumeration e=v.elements();
while(e.hasMoreElements())
{
System.out.println( e.nextElement());
}

}

}

Output:
10
[indraeet, 4, singh]
[indraeet, singh]
2
indraeet
singh
                         

Limitations of Enumeration:

1.  We can apply Enumeration only for legacy classes and its not a universal cursor.
 2. by using Enumeration , we can get read only access not remove operation.


to overcome above limitation we should go for Iterator cursor.

2. Iterator cursor.

 We can apply Iterator  cursor for any collection object So we can called Iterator cursor as a universal cursor.
We can perform both read and remove operations by using Iterator cursor.
We can create Iterator object by using iterator() method of Collection interface.
public Iterator iterator();

Example : Iterator i= c.iterator();
where c is a Collection object.

Methods:
 1. public boolean hasNext();
 2. public object next();
 3. public void remove();

package indrajeet.list.exp;

import java.util.*;

public class CursorsExp {

public static void main(String[] args) {

ArrayList al=new ArrayList<>();

al.add("anu");
al.add("indrajeet");
al.add("singh");
al.add("tomar");
al.add("golu");
al.add("singh");

Iterator itr=al.iterator();

for(Object s: al)
{
System.out.println(itr.next());

}

}

}


Limitations of Iterator cursor:

 1. We can only move forward direction by using Iterator cursor. We can not move towards backward       direction So its only single direction cursor not a bidirectional.
 2. By using Iterator , we can  perform only read/remove operations not replacement  and addition           operations of  new object operation.

So overcome above limitations we should go for ListIterator cursor.

3. ListIterator cursor: 

   1. We can move both direction forward and backward So we can say its a bidirectional cursor.
   2. We can perform read , remove , write  and replacement operation also and also addition of a new         object in a collection.

  public ListIterator listIterator();
  ListIterator ltr=l.listIterator();

  where l= any List object.

Methods:

1. Forward movement:

 1. public boolean hasNexr();
 2. public object next();
 3. int nextIndex();

2. backward movement:
  1. public boolean hasPrevious();
  2. public object previous();
  3. public int previousIndex();

3. Extra operations(Replacement and add)
    1. pubic void remove();
    2. public void add(object o);
    3. public void set(object o);

below is the Example for ListIterator :


package indrajeet.list.exp;

import java.util.*;

public class CursorsExp {

public static void main(String[] args) {

ArrayList al=new ArrayList<>();

al.add("anu");
al.add("indrajeet");
al.add("singh");
al.add("tomar");
al.add("golu");
al.add("singh");

System.out.println(al);
ListIterator li= al.listIterator(); // ListIterator code
while(li.hasPrevious())
{
String o=(String) li.previous();
if(o.equals("goluu"))
{
li.remove();

}
else
{
li.add("goluaa");
}
System.out.println(al);
}

}

}








Friday, December 29, 2017

Stack in java


Its a child class of Vector. it is designed the class for LIFO last in first out order.

Constructors :
  1. Stack s=new Stack();

                                                     
 
offset Stack index
1 D 3
2 C 2
3 B 1
4 A 0

methods:

 1. object push(object o)
      to insert an object into the stack.
 2. object pop()
     to remove an object and returns top of the stack object.
3.  object peek()
    to return top of the stack without removal.
4. Boolean empty()
   return true if the stack is empty.
5. int search(object o)
   returns offset if the element is available otherwise -1.

note : stack is extends vector so all methods of vector class are available for the stack too.


example :


package indrajeet.list.exp;

import java.util.Stack;

public class StackExp {

public static void main(String[] args) {

Stack s=new Stack<>();
s.push(3);
s.push("indajeet");
s.add(3);

System.out.println(s);
System.out.println(s.peek());// return top of the element without removal
System.out.println(s);
System.out.println(s.pop());//return top of the element with removal
System.out.println(s);
System.out.println(s.search(2));// returns offset if element is available otherwise -1


}

}

output :

[3, indajeet, 3]
3
[3, indajeet, 3]
3
[3, indajeet]
-1


Thursday, December 28, 2017

Vector in Java


Vector also implements List interface. So its also having same feature as ArrayList having like
underline data structure is re sizable array.  below are the same features which are followed.

1. insertion order is preserved.
2. duplicates are allowed.
3. heterogeneous objects are allowed.
4. null insertion is possible
5. Its implements serializable , clonable  and RandomAccess interfaces.

but one point to need to remember which is different from others every methods which are in the vector are synchronized So we can say vector objects are thread safe.

Vector Constructors :

 1. Vector v=new Vector();

as above creates empty vector object which is having default initial capacity 10.

once vector reaches its max capacity then a new vector object will be created with new capacity.

  new capacity= current capacity*2.

as above new capacity will be twice of current capacity.
if current capacity is 10 then new object capacity will be 20.

2. Vector v1=new Vector(int initial capacity);

creates an empty vector objects with specified initial capacity.
Vector v1= new Vector(100);

3. Vector v3=new Vector(int initial capacity, int increment capacity );
 Vector v3 new Vector(100, 5);

as above define vector object v3 which is having initial capacity 100  which will be incremented by 5.

4. Vector v4-new Vector(Collection c);
creates an equivalent object of Vector for the given collection object. this constructor means inter conversion.


Vector Specific Methods:

to add methods:
 1. addElement(object o) // Vector method
 2. add(object o) // Collection method
 3. add(int index, object o) // List

to remove methods:
 1.  remove(object o) // Collection method
 2. removeElement(object o) // Vector method
 3. remove(object o)    // List
 4. clear() // Collection
 5. removeAllElement() // Vector
to get methods:

 1. object get (int index) // LIst
 2. object elementAt(int index) //  Vector
 3. objet firstElement() // vector
 4. objet lastElement() // vector

other methods:

 1. int size();
2. int capacity();
3. Enumeration elements();



as below example we can use above methods and constructors.

package indrajeet.list.exp;

import java.util.Enumeration;
import java.util.Vector;

public class VectorExp {

public static void main(String[] args) {


Vector v=new Vector<>();
System.out.println(v.capacity()); //10
v.add("indraeet");
v.addElement("singh");
v.add(1, 4);
System.out.println(v); //[indraeet, 4, singh]
v.removeElementAt(1);

System.out.println(v);  //[indraeet, singh]
System.out.println(v.size()); //2

Enumeration e=v.elements();
while(e.hasMoreElements())
{
System.out.println( e.nextElement());
}

}

}

Wednesday, December 27, 2017

LinkedList


LinkedList implements List interface. The underline data structure  is doubly linkedlist.

there are some point which are followed by linked list.

1. Insertion order is preserved.
2. duplicates are allowed.
3. heterogeneous is allowed.
4. Null insertion is possible.
5. LinkedList implements serializable and clonable interface but not randomAccess.
6. LinkedList is best choice if our frequent operation is insertion or deletion in the middle.
7. LinkedList the worst choice if our frequent operation is retrieval.


Constructors:

LinkedList l=new LinkedList();

creates empty linked list object.

LinkedList l1= new LinkedList(Collection c);


LinkedList Class specific  methods:

Usually we can use LinkedList to develop stack and queue to provide support for these requirement below are the methods.

1. void addFirst(Object o)
2. void AddLast(object o)
3. object getFirst()
4. object getLast()
5. object removeFirst()
6. object removeLast()


Example :

package indrajeet.list.exp;

import java.util.LinkedList;

public class LinkedListDemo {

public static void main(String[] args) {

LinkedList l=new LinkedList();
l.add("indar");
l.add(30);  //heterogeneous OBJECT
l.add(null);
l.add("indar"); // duplicate object
l.set(0,"jeet");
System.out.println(l); // ouput : [jeet, 30, null, indar]
l.removeFirst();
System.out.println(l); // output : [30, null, indar]


}

}


Tuesday, December 26, 2017

ArrayList



Arraylist implemented List interface. its having all features which is in List.

Its having the underline data structure  is re sizable array.
Duplicates are allowed.
Insertion order is preserved.
heterogeneous objects allowed.
Null insertion is possible.

constructors:

1.  arraylist l=new Arraylist();

above array list is empty and default initial capacity is 10.
Once arraylist reaches max capacity then new arraylist object will be creates with new capacity

like new capacity=(current capacity*3/2)+1

2. arraylist l1=new arrayList(int initial capacity)

 creates an empty arraylist object with specifies initial capacity.

3. Arraylist l3=new Arraylist(Collection c);

Creates am equivalent Arraylst object for given Collection.


Example:

package indrajeet.list.exp;
import java.util.*;

class ArrayListdemo{
public static void main(String args[]){

ArrayList l=new ArrayList();

   l.add("A");
   l.add(10);
   l.add(null);
   l.add("A");
 System.out.println(l);

}
}

output:  [A, 10, null, A]



ArrayList and Vector classes implements RandomAccess  interface so that ArrayList can access random elements. We can access with the same speed..

ArrayList is the best choice if our frequent operation is retrial operation(because its implements RandomAccess interface).
ArrayList is the worst choice if our frequent operation is insertion or deletion in the middle( because more shifting required).


By using array[index], you can access to any element while in a linked list you must navigate through all the list starting from fist until you get the element which you need.

access time for arrayList O(1)
   LinkedList O(n)

Wednesday, December 20, 2017

List



List is child interface of collection. If we want to represent a group of individual objects as a single entity where duplicates are not allowed in the insertion order , insertion order must be preserved then we should go for List.


We can preserve insertion order by the index and we can differentiate to duplicate objects by using index.

So index will play very important role in a List.

Below is the example:

B C D E A
0 1 2 3 4 5    INDEX

List interface defines the following specific methods.

1. void add(int index,object o)
2. Boolean addAll(int index, Collection C)
3. object get(int index) 
4. object set(int index, object value)
5. void remove(int index, object o)





List is implemented by the below classes.

1. ArrayList
2. LinkedList
3. Vector 
 4. Stack