Tuesday, 4 July 2023

List(Interface) & Child classes of List Interface

List Interface 



  • We can preserve Insertion order via Indexed.
  • We can differentiate Duplicate order via Indexed.
  • Index will play very important role in List.
  • List Interface defines the following Specific Methods.
8 Methods are present in List 
  • void add(int index, Object o).
  • boolean addAll(int index, Collection c).
  • object get(int index).
  • object remove(int index)
  • object set(int index, Object new)[To replace the element present at specified index with provided Object & returns Old objects]
  • int indexOf (Object O)[Returns index of first occurrence of 'o']
  • int lastIndexOf(Object o)
  • ListIterator listIterator()
  1. ArrayList

  • The underlying data structure is Resizable Array & Growable Array.
  • Duplicates are allowed.
  • Insertion Order Preserved.
  • Heterogeneous Objects are allowed[It means Different type of Objects].
  • Null Insertion is Possible.
Constructor

In ArrayList 3 types of Constructor are available
  a).ArrayList l = new ArrayList();
  • It means Create empty ArrayList object with default intial Capacity.
  • for eg:- The capacity is 10.
  • One's ArrayList reaches Max Capacity than a new ArrayList object will be created
   new Capacity = (current Capacity*3/2)+1


      b. ArrayList l = new ArrayList(int intialCapacity);
  • Creates an empty ArrayList Objects with Specified Intial Capacity.
  c.ArrayList l = new ArrayList(Collection c);
  • Creates an equivalent ArrayList object for the given Collection.
Note:-
  • Usually we use Collection to hold underlying objects from One Location to another Location[Container]. To provide Support for this requirement every collection class by default implements Serializable and the Cloneable Interface.
  • ArrayList & Vector class implements Random Access Interface so, that any Random element with the access with same speed.
  • Random Access Interface present in Java.util Package. Doesn't contain any Methods. It is Marker Interface Required ability will be provided by JVM.
 When ArrayList is Worst Choice?
  • If our Frequent Operation is Insertion & Deletion in a middle. 
 When ArrayList is Best Choice and Why?
  • If Our Frequent Operation is Retrieval Operation.
  • Because ArrayList implements Random Access Interface
How to get Synchronized Version of ArrayList Object?
  • By default ArrayList is Non-Synchronized but we can get Synchronized version of ArrayList object.
  • By using SynchronizedList Method of Collection Class.
  • For eg:
     ArrayList l = new ArrayList();
     List l1 = collections.synchronizedList(l);

2.LinkedList
  • The Underline data structure(Double/Doubly LinkedList).
  • Insertion Order is Preserved.
  • Duplicates are allowed.
  • Heterogeneous Objects allowed.
  • Null Insertion is Possible.
  • LinkedList implements Searlizable & Cloneable Interfaces.
  • Not Implements Random Access Interface.
Constructor 

     a. LinkedList l = new LinkedList();
  • Creates an Empty LinkedList Object.
  b. LinkedList l = new LinkedList(collection c);
  • Creates an Equivalent LinkedList object for the given Collection.
LinkedList Specific Methods

  • void addFirst(object o);
  • void addLast(object o);
  • Object getFirst();
  • Object getLat();
  • Object removeFirst();
  • Object removeLast();
Usually We can use LinkedList to implements Stack & Queue.

What is Difference B/w LinkedList & ArrayList?



3.Vector
  • The Underlying data Structure is Resizable Array & Growable Array.
  • Insertion Order Preserved.
  • Duplication are allowed.
  • Heterogeneous Objects are allowed.
  • Null Insertion is possible.
  • It Implements Serializable, cloneable & Random Access Interface.
  • Every Method present in the Vector is Synchronized and Vector Object is Thread-Safe.

Constructor

a. Vector v = new Vector();
  • Creates an empty vector Object with default initial Capacity 10.
  • Once, Vector reaches it's Max Capacity than, New Vector Object will be created with 
  •      New capacity=current capacity*2 

b. Vector v = new Vector(int initial capacity);
  • Creates an empty Vector Object with Specified initial Capacity.
c.  Vector v = new Vector(int initialcapacity , int                 incrementalcapacity).
  • for eg: Vector v = new v (1000, 5);

d. Vector v =new Vector(collection c);
  •   Creates an equivalent vector object for the given collection these constructor meant for interconversion B/w Collection Objects. 
  
What is Difference B/w ArrayList & Vector?


Methods
  • int Size();
  • int Capacity();
  • Enumeration element();
  • addElement(Object o);
  • removeElement(object o);
  • object get(int index); 



Stack
  • It is Child Class of Vector.
  • It is Specially Designed Class Last In First Out.(LIFO).
Constructor

Stack s = new Stack();

Methods

  • Object push(object o);
     To Insert an Object into Stack.
  • Object pop();
     To Remove & Return Top of the Stack.
  • Object peek();
     To return Top of the Stack without Removal.
  • Boolean empty();
     Returns true If the Stack is Empty.
  • int Search(object o);
     Return Offset if the element is available otherwise return -1.















No comments:

Post a Comment

Servlet in AEM/Sling Servlet

  What is Servlet? A servlet is a Java class that runs on the server side where the application resides. It acts as a middle layer between t...