Java Vector isEmpty() Method
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 the
Vectorcontains no elements (i.e.,size() == 0), then it returnstrue - If the
Vectorcontains 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
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
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
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:
- Better readability, clearer semantics
- 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
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
System.out.println("The vector is null or empty");
}
Best Practices
- When you need to check whether a collection is empty, prefer using
isEmpty()instead ofsize() == 0 - Before using
isEmpty(), make sure theVectorobject has been initialized - For objects that may be
nullofVector, first perform anullcheck - When using
Vectorin 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.