Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Java » Vector in Java Collection, Constructors, Example Programs

  • International Labour's Day in Hindi | अंतर्राष्ट्रीय मजदूर दिवस 1 मई
    International Labour’s Day in Hindi | अंतर्राष्ट्रीय मजदूर दिवस 1 मई General Knowledge
  • Vedaant Madhavan Biography in Hindi, Family, School, Age
    Vedaant Madhavan Biography in Hindi, Family, School, Age Biography
  • Balkand Ramayana story in Hindi | रामायण बाल कांड राम का जन्म
    Balkand Ramayana story in Hindi | रामायण बाल कांड राम का जन्म Spiritual
  • Apj Abdul Kalam biography in Hindi, Life, Missile Man of India
    Apj Abdul Kalam biography in Hindi, Life, Missile Man of India Biography
  • World Earth Day in Hindi | पृथ्वी दिवस कब और क्यों मनाया जाता है?
    Earth Day in Hindi, Theme | पृथ्वी दिवस कब और क्यों मनाया जाता है? General Knowledge
  • Mahatma Gandhi Essay in Hindi | Gandhiji Biography
    Mahatma Gandhi Essay in Hindi | Gandhiji Biography Biography
  • Vat Savitri Vrat in Hindi, Vat Savitri Puja | वट सावित्री पूजा
    Vat Savitri Vrat in Hindi, Vat Savitri Puja | वट सावित्री पूजा Festival
  • Kapil Sharma Show, Comedy Show in Hindi
    Kapil Sharma Show, Comedy Show in Hindi Biography

Vector in Java Collection, Constructors, Example Programs

Posted on August 17, 2021September 5, 2022 By GeekCer Education No Comments on Vector in Java Collection, Constructors, Example Programs
Java Vector

Java Vector allows the creation of resizable arrays and implements the List interface. The vector object is thread safe, therefore the result will be accurate even if several threads interact with it simultaneously.

The elements of a vector can be retrieved using an integer index. In Java Vector, there are several inherited methods that are no longer part of the collections framework.

Vector is advisable in case of multiple threads because with a single thread, Vector becomes slow.

Table of Contents

  • Important points to remember about Java Vector
    • When should you go with Vector?
  • Constructors of Vector in Java
  • Generic Vector class in Java
    • Program using Java Vector
  • Difference between ArrayList and Vector

Important points to remember about Java Vector

  • Vector allows duplicate objects and preserves insertion order, so you don’t need to worry about order.
  • In Vector null insertion is possible, so you can insert any number of null.
  • Vector implements Serializable, Cloneable interface and RandomAccess interface, because it implements RandomAccess you can access element by index.
  • You can insert homogeneous as well as heterogeneous object into vector.
  • Vector class is synchronized, hence it is thread safe.

When should you go with Vector?

When your frequent operation is retrieval you should use vector because retrieval is faster in vector.

Note:
Default initial capacity of Vector is 10.
Once the vector reaches its maximum capacity, a new vector object will be ready with double the capacity.
new capacity = current capacity * 2

Constructors of Vector in Java


Vector vector = new Vector();

Vector vector = new Vector(int initialCapactity);

Vector vector = new Vector(int initialCapacity, int capacityIncrement);

Vector vector = new Vector(Collection c);

Generic Vector class in Java

The following are examples of generic vectors:


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

ArrayList<Integer> vect = new ArrayList<Integer>(30);


ArrayList<Integer> vect = new ArrayList<Integer>();

vect.add(10);

In the above example, the java compiler internally converts the int type (10) into the Integer type object. In other words, we can say that Vector also accepts elements in the form of object type only.

Program using Java Vector

                  import java.util.Vector;

public class VectorProgram {
  public static void main(String args[]) {
    Vector vector = new Vector();
    vector.add("Java");
    vector.add("C#");
    vector.add("PHP");
    vector.add("Oracle");

    System.out.println("Vector:  " + vector);
  }
}

You can retrieve vector elements using ListIterator as it iterates the vector in forward and reverse direction.

Difference between ArrayList and Vector

The ArrayList and Vector classes both implement the List interface, both implement the RandomAccess interface and follow insertion order.

There are many similarities between ArrayList and Vector but there are also some differences which are as follows:

#VectorArrayList
1Vector is thread-safe. But ArrayList is not thread-safe.
2Every method of Vector is synchronized.But no any method of ArrayList is synchronized.
3Only one thread is allowed to operate on Vector at a time. Multiple threads can access ArrayList simultaneously.
4Vector increases a waiting time of threads hence performance is low.Performance is high because threads are not required to wait.
5Vector is an legacy class so Vector’s many-method is the legacy method.But ArrayList is not legacy.
6Capacity is increases by new capacity = current capacity * 2 once Vector reaches to maximum capacity.Capacity is increases by new capacity = current capacity * 3/2 + 1 once ArrayList reaches to maximum capacity.
7Vector can use both Iterator or Enumerator interface to traverse through elements.ArrayList uses Iterator interface to traverse through elements.

Reference:
https://docs.oracle.com/javase/8/docs/api/java/util/V…r.html

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Java

Post navigation

Previous Post: Java LinkedList
Next Post: Multithreading in java, Thread, Runnable | Life Cycle of Thread

More Related Articles

What is Array in Java? Types of array in Java What is Array in Java? Types of array in Java Java
JDBC Driver in Java JDBC Driver in Java Java
Top Java Programs for Coder Top Java Programs for Coder Java
Iterate Map in Java Iterate Map in Java, Map.entrySet(), Iterators, foreach Loop Java
Immutable class in Java Immutable class in Java | How to create Immutable class in Java Important
Difference between Interface and Abstract class Difference between Interface and Abstract class Differences

Leave a Reply Cancel reply

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

  • Java Home
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Keywords
  • Java Operators
  • Java If-else Statement
  • Java Switch
  • Java Loop
  • Java Arrays
  • Method Overloading in Java
  • Java OOP
  • Java Collections
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Multithreading in java
  • Thread Synchronization
  • Exception Handling
  • Java JDBC Driver
  • Java Database Connectivity steps
  • Lambda Expressions
  • Concurrent Collections
  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved