Skip to main content

Collection(Vector,Stack)

Vector:-Vector is a group of individual objects it is also called legacy class. Vector implements List Interface. Like ArrayList it also maintains insertion order but it is rarely used in non-thread environment as it is synchronized and due to which it gives poor performance in searching, adding, delete and update of its elements.

Three ways to create vector class object:

Method 1:-
Vector v = new Vector();
It creates an empty Vector with the default initial capacity of 10. It means the Vector will be re-sized when the 11th elements needs to be inserted into the Vector. Note: By default vector doubles its size. i.e. In this case the Vector size would remain 10 till 10 insertions and once we try to insert the 11th element It would become 20 (double of default capacity 10).
Method 2:
Syntax: Vector object= new Vector(int initialCapacity)
Vector v = new Vector(3);
It will create a Vector of initial capacity of 3.
Method 3:
Syntax:
Vector object= new vector(int initialcapacity, capacityIncrement)
Vector v= new Vector(4, 6)
Here we have provided two arguments. The initial capacity is 4 and capacityIncrement is 6. It means upon insertion of 5th element the size would be 10 (4+6) and on 11th insertion it would be 16(10+6).

Example1:-VectorExample.java

import java.util.*;
 
public class VectorExample {
 
   public static void main(String args[]) {
      Vector v = new Vector ();
 
      v.addElement("Apple");
      v.addElement("Orange");
      v.addElement("Mango");
      v.addElement("Fig");
 
      System.out.println("Size is: "+v.size());
      System.out.println("Default capacity increment is: "+v.capacity());
 
      v.addElement("fruit1");
      v.addElement("fruit2");
      v.addElement("fruit3");
 
      System.out.println("Size after addition: "+v.size());
      System.out.println("Capacity after increment is: "+v.capacity());
 
      Enumeration en = v.elements();
      System.out.println("\nElements are:");
      while(en.hasMoreElements())
         System.out.print(en.nextElement() + " ");
   }
}
Output:
Size is: 4
Default capacity increment is: 4
Size after addition: 7
Capacity after increment is: 8
 
Elements are:
Apple Orange Mango Fig fruit1 fruit2 fruit3
 

methods of Vector Class:

1.     void addElement(Object element): It inserts the element at the end of the Vector.
2.     int capacity(): This method returns the current capacity of the vector.
3.     int size(): It returns the current size of the vector.
4.     void setSize(int size): It changes the existing size with the specified size.
5.     boolean contains(Object element): This method checks whether the specified element is present in the Vector. If the element is been found it returns true else false.
6.     boolean containsAll(Collection c): It returns true if all the elements of collection c are present in the Vector.
7.     Object elementAt(int index): It returns the element present at the specified location in Vector.
8.     Object firstElement(): It is used for getting the first element of the vector.
9.     Object lastElement(): Returns the last element of the array.
10.    Object get(int index): Returns the element at the specified index.
11.    boolean isEmpty(): This method returns true if Vector doesn’t have any element.
12.    boolean removeElement(Object element): Removes the specifed element from vector.
13.    boolean removeAll(Collection c): It Removes all those elements from vector which are present in the Collection c.
14.    void setElementAt(Object element, int index): It updates the element of specifed index with the given element.
Stack:-Stack is a special designed class which is sub class of vector class where insertion and deletion perform from single end.
Methods of stack class are:-
public E push(E);
 public synchronized E pop();
 public synchronized E peek();
 public boolean empty();
 public synchronized int search(java.lang.Object);
Example1:-TestStack.java
import java.util.*;
class TestStack
{
public static void main(String[]args)
{
Stack s=new Stack();
s.push("A");
s.push("B");
s.push("C");
s.push("D");
System.out.println(s);
Iterator itr=s.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
System.out.println("poped element is :"+s.pop());
System.out.println(s);
System.out.println("peeked element is :"+s.pop());
System.out.println(s);
System.out.println(s.empty());
System.out.println(s.search("A"));
System.out.println(s.search("D"));
}
}
Output:-
C:\JAVATECH>javac TestStack.java
C:\JAVATECH>java TestStack
[A, B, C, D]
A
B
C
D
poped element is :D
[A, B, C]
peeked element is :C
[A, B]
false
2

-1

Comments

Popular posts from this blog

inheritance

INHERITANCE:- Inheritance is the process of code re-usability, using this property software or applications can develop in less time or can develop rapidly. Inheritance increases the readability of code and decrease redundancy or repetition of code or reduces size of code. Use of previously developed code in future any number of times is called inheritance. Through inheritance variables and methods of class can inherited. Types of Inheritance:- 1)         Single or Single level inheritance 2)         Multilevel inheritance 3)         Hierarchical inheritance Java does not support to multiple inheritance. Single or single level inheritance:- In a single inheritance a class is inherited by a single class. In a single or single level inheritance a sub class having only one super class directly or indirectly. Syntax: -        class Subclass ex...

Inheritance-4

Example10:- class Student { Student() { System.out.println("This is super class Zero argument constructor"); } Student(String s) { System.out.println("This is super class One argument constructor"); } } class Test extends Student { Test() { super(null); System.out.println("This is sub class Zero argument constructor"); } public static void main(String[]args) { Test t=new Test(); } } Output:- C:\JAVATECH>javac Test.java C:\JAVATECH>java Test This is super class One argument constructor This is sub class Zero argument constructor Example11:- class Student { Student() { System.out.println("This is super class Zero argument constructor"); } Student(String s) { System.out.println("This is super class One argument constructor"); } } class Test extends Student { Test() { super(); super(null); System.out.println("This is su...

Abstraction,Encapsulation And Inheritance

Abstraction  is a one of the great feature of Oops,in this feature hide entire details of product, show only requirement specifications(what product will do). The main purpose behind abstraction is to increase productivity or use of product without knowledge of product implementation. in this case the person don't know how product  was implemented but know how product works or action of product. In java abstraction is achieved by abstract class and interface,here interface provide 100% abstraction and abstract class provide partial abstraction. For example: - ATM machine is example of abstraction.  every technical and non-technical person use facility of ATM without knowing the entire process. they know if they click on withdraw button then they can withdraw money from ATM machine but they don't know when they click on withdraw button the request is send to the server and after go into database and so on. Here ATM machine show only requirement specificat...