eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

1. Introduction

The Vector class is a thread-safe implementation of a growable array of objects. It implements the java.util.List interface and is a member of the Java Collections Framework. While it’s similar to ArrayList, these classes have significant differences in their implementations.

In this tutorial, we’ll explore the Vector class and some of its most common operations and methods.

2. How Does Vector Work?

The Vector class is designed to function as a dynamic array that can expand or shrink according to the application’s needs. Thus, we can access the objects of the Vector using the indices. Additionally, it maintains the insertion order and stores duplicate elements.

Every Vector aims to enhance its storage handling by keeping track of both the capacity and the capacityIncrement. The capacity is nothing but the size of the Vector. The size of the Vector increases as we add elements to it. Therefore, the capacity remains consistently equal to or greater than the Vector‘s size.

Every time the length of a Vector reaches its capacity, the new length is calculated:

newLength = capacityIncrement == 0 ? currentSize * 2 : currentSize + capacityIncrement

Similar to ArrayList, the iterators of the Vector class are also fail-fast. It throws a ConcurrentModificationException if we try to modify the Vector while iterating over it. However, we can access the iterator’s add() or remove() methods to structurally modify the Vector.

3. How to Use Vector

Let’s see how to create a Vector and perform various operations on it.

3.1. Creating a Vector

We can create Vector instances using the constructor. There are four different types of Vector constructors.

The first is the default constructor, which creates an empty Vector with the initial capacity of 10 and a standard capacityIncrement of 0:

Vector<T> vector = new Vector<T>();

We can create an empty Vector by specifying its initial size (capacity). In this case, its capacityIncrement is also set to 0:

Vector<T> vector = new Vector<T>(int size);

There’s another constructor using which we can specify the initial capacity and capacityIncrement to create an empty Vector:

Vector<T> vector = new Vector<T>(int size, int capacityIncrement);

Finally, we can construct a Vector by passing another Collection to the constructor. The resultant Vector contains all the elements of the specified Collection:

Vector<T> vector = new Vector(Collection<T> collection);

3.2. Adding Elements to Vector

Let’s create a method that returns a Vector. We’ll use this method for future operations:

Vector<String> getVector() {
    Vector<String> vector = new Vector<String>();

    vector.add("Today");
    vector.add("is");
    vector.add("a");
    vector.add("great");
    vector.add("day!");

    return vector;
}

Here, we’ve created an empty Vector of Strings with a default constructor and added a few Strings.

Based on the needs, we can add elements at the end or a specific index to a Vector. For this, we can use the add() method which is overloaded to satisfy various needs.

Now, let’s add an element at a specific index to vector:

vector.add(2, "not"); // add "not" at index 2

assertEquals("not", vector.get(2)); // vector = [Today, is, not, a, great, day!]

Here, we use the add(int index, E element) method of the Vector class to add the Stringnot” at index 2. We should note that it shifts all the elements after the specified index to the right by one position. Thus, the Stringnot” is added between the Strings “is” and “a“.

Similarly, we can add all the elements of a Collection to a Vector using the method addAll(Collection c). Using this method, we can append all the elements of a collection to the end of the Vector in the same order as that of the Collection:

ArrayList<String> words = new ArrayList<>(Arrays.asList("Baeldung", "is", "cool!"));

vector.addAll(words);

assertEquals(9,  vector.size());
assertEquals("cool!", vector.get(8));

After the execution of the above code, if we print vector, we’ll get the following:

[Today, is, not, a, great, day!, Baeldung, is, cool!]

3.3. Updating Elements

We can use the set() method to update the elements of a Vector. The set() method replaces the element at the specified index with the new element while returning the replaced element. Consequently, it accepts two arguments – the index of the element to be replaced, and the new element:

Vector<String> vector = getVector();

assertEquals(5, vector.size());
vector.set(3, "good");
assertEquals("good", vector.get(3));

3.4. Removing Elements

We can use the remove() method to remove an element from a Vector. It has been overloaded according to various needs. As a result, we can remove a specific element or an element at a specific index, or all the elements of a Vector. Let’s see a few examples.

If we pass an object to the remove() method, the first occurrence of it is deleted:

Vector<String> vector = getVector();
assertEquals(5, vector.size());

vector.remove("a");
assertEquals(4, vector.size()); // vector = [Today, is, great, day!]

Similarly, if we now pass 2 to the remove() method above, it deletes the element, great, at index 2:

vector.remove(2);
assertEquals("day!", vector.get(2));

We should note that all the elements to the right of the deleted element shift to their left by one position. Moreover, the remove() method throws an ArrayIndexOutOfBoundsException if we provide the index beyond the range of the Vector, i.e., index < 0 or index >= size().

Finally, let’s remove all the elements of vector:

vector.removeAll();
assertEquals(0, vector.size());

3.5. Getting an Element

We can use the get() method to get an element of the Vector at a particular index:

Vector<String> vector = getVector();

String fourthElement = vector.get(3);
assertEquals("great", fourthElement);

The get() method also throws an ArrayIndexOutOfBoundsException if we provide the index beyond the range of the Vector.

3.6. Iterating Through a Vector

We can iterate through a Vector in multiple ways. However, one of the most common ways is the for-each loop:

Vector<String> vector = getVector();

for(String string : vector) {
    System.out.println(string);
}

We can also use the forEach() method to iterate through a Vector, especially when we want to perform a certain action on each element:

Vector<String> vector = getVector();
vector.forEach(System.out::println);

4. When to Use Vector

The first and obvious use of Vector is when we need a growable collection of objects. If we’re unsure about the size of the growing collection, but we know how frequently we’ll add or remove the elements, then we may prefer to use a Vector. In such cases, we can set capacityIncrement according to the situation.

However, since Vector is synchronized, it’s preferable to use it in case of multithreaded applications. In the case of single-threaded applications, ArrayList works much faster compared to Vector. Moreover, we can synchronize ArrayList explicitly using Collections.synchronizedList().

5. Conclusion

In this article, we had a look at the Vector class in Java. We also explored how to create a Vector instance and how to add, find, or remove elements using different approaches.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)