In Java, the size() and isEmpty() methods of the Collection interface are used to check the number of elements in a collection and determine whether it contains any elements.
- size() returns the number of elements present in the collection.
- isEmpty() returns true if the collection contains no elements; otherwise, it returns false.
size() Method
The size() method is used to determine the number of elements present in a collection.
Syntax
public int size()
- Parameter: This method does not take any parameters.
- Return Value: Returns the number of elements in the collection.
isEmpty() Method
The isEmpty() method checks whether a collection contains any elements.
Syntax
public boolean isEmpty()
- Parameter: This method does not take any parameters.
- Return Value: Returns true if the collection is empty; otherwise, returns false.
Example: Using LinkedList
import java.util.*;
class CollectionSizeDemo {
public static void main(String[] args)
{
// Create a collection using LinkedList
Collection<Integer> numbers = new LinkedList<>();
// Add elements to the collection
numbers.add(25);
numbers.add(48);
numbers.add(73);
numbers.add(91);
// Display the collection
System.out.println("Collection: " + numbers);
// Get the number of elements
System.out.println("Size: " + numbers.size());
// Check whether the collection is empty
System.out.println("Is collection empty: "
+ numbers.isEmpty());
// Remove all elements
numbers.clear();
// Display the collection after clearing
System.out.println("Collection after clear: "
+ numbers);
// Check whether the collection is empty
System.out.println("Is collection empty: "
+ numbers.isEmpty());
}
}
Output
Collection: [25, 48, 73, 91] Size: 4 Is collection empty: false Collection after clear: [] Is collection empty: true
Explanation: The collection initially contains four elements, so size() returns 4 and isEmpty() returns false. After calling clear(), all elements are removed. Therefore, the collection size becomes 0 and isEmpty() returns true.
Example: Using ArrayList
import java.util.*;
class ArrayListSizeDemo {
public static void main(String[] args)
{
// Create an ArrayList
Collection<String> subjects = new ArrayList<>();
// Add elements
subjects.add("Java");
subjects.add("Python");
subjects.add("C++");
subjects.add("SQL");
subjects.add("HTML");
// Display the collection
System.out.println("Subjects: " + subjects);
// Get the size of the collection
System.out.println("Size: " + subjects.size());
// Check whether the collection is empty
System.out.println("Is collection empty: "
+ subjects.isEmpty());
}
}
Output
Subjects: [Java, Python, C++, SQL, HTML] Size: 5 Is collection empty: false
Explanation: Five elements are added to the ArrayList, so size() returns 5. Since the collection contains elements, isEmpty() returns false.
Example: Checking an Empty Collection
An empty collection can be checked directly using the size() and isEmpty() methods.
import java.util.*;
class EmptyCollectionDemo {
public static void main(String[] args)
{
// Create an empty ArrayList
Collection<Integer> values = new ArrayList<>();
// Check the size
System.out.println("Size: " + values.size());
// Check whether the collection is empty
System.out.println("Is collection empty: "
+ values.isEmpty());
}
}
Output
Size: 0 Is collection empty: true
Explanation: The values collection does not contain any elements, so size() returns 0. The isEmpty() method returns true, indicating that the collection is empty.