Java Vector isEmpty() Method

Java Vector


isEmpty()The method is a commonly used method in JavaVectorprovided by the class, used to check whether the Vector is empty. This method belongs tojava.util.Vectorclass, and is part of the Java collections framework.

Method Syntax:

public boolean isEmpty()

Method Function

isEmpty()The main function of the method is to determine whether the currentVectorobject does not contain any elements. This method returns a boolean value:

  • If theVectorcontains no elements (i.e.,size() == 0), then it returnstrue
  • If theVectorcontains one or more elements, then it returnsfalse

Usage Examples

Below we demonstrate through several examplesisEmpty()the usage of the method.

Example 1: Check an Empty Vector

Example

import java.util.Vector;

public class VectorExample {
    public static void main(String[] args) {
        // Create an empty vector
        Vector<String> vector = new Vector<>();
       
        // Check whether the vector is empty
        if (vector.isEmpty()) {
            System.out.println("The vector is empty");
        } else {
            System.out.println("The vector is not empty");
        }
    }
}

Output:

Vector is empty

Example 2: Check a Non-empty Vector

Example

import java.util.Vector;

public class VectorExample {
    public static void main(String[] args) {
        // Create a vector and add elements
        Vector<Integer> numbers = new Vector<>();
        numbers.add(10);
        numbers.add(20);
       
        // Check whether the vector is empty
        if (numbers.isEmpty()) {
            System.out.println("The vector is empty");
        } else {
            System.out.println("The vector is not empty, it contains " + numbers.size() + " elements");
        }
    }
}

Output:

向量不为空,包含 2 个元素

Method Implementation Principle

In Java'sVectorclass,isEmpty()the implementation of the method is very simple. It simply checks the vector'selementCount(element count) to see if it is 0:

Example

public boolean isEmpty() {
    return elementCount == 0;
}

whereelementCountYesVectoris an internal field of the class, used to record the number of elements actually stored in the current vector.


Comparison with Other Methods

isEmpty() vs size() == 0

isEmpty()The method andsize() == 0expression are functionally equivalent, but it is usually recommended to useisEmpty(), because:

  1. Better readability, clearer semantics
  2. In some collection implementations,isEmpty()may have a more efficient implementation

isEmpty() vs null Check

It should be noted that,isEmpty()the method can only be used to check whether an initializedVectorobject is empty. If theVectorobject itself isnull, callingisEmpty()will causeNullPointerException。

Example

Vector<String> vector = null;
System.out.println(vector.isEmpty()); // Throws NullPointerException

The correct approach is to first check whether it isnull, and then check whether it is empty:

Example

if (vector == null || vector.isEmpty()) {
    System.out.println("The vector is null or empty");
}

Best Practices

  1. When you need to check whether a collection is empty, prefer usingisEmpty()instead ofsize() == 0
  2. Before usingisEmpty(), make sure theVectorobject has been initialized
  3. For objects that may benullofVector, first perform anullcheck
  4. When usingVectorin a concurrent environment, pay attention to synchronization issues

Summary

VectorofisEmpty()The method is a simple but practical utility method. It provides a clear way to check whether a vector is empty. Understanding and correctly using this method can help you write more robust and more readable code. Remember, when usingVectorin a concurrent environment, althoughVectoris thread-safe, you still need to pay attention to the atomicity of compound operations.

Java Vector

Other Extensions