Java List Size()

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

The size() in Java is a predefined method of the ArrayList class. It is used to calculate the number of objects in an ArrayList. It Does not take any input parameter, and whenever the ArrayList calls this function, it returns an integer representing the count of elements of ArrayList. The time complexity is O(n), where n is the number of elements in the ArrayList object.

Syntax of Java List size()

The snippet below shows the declaration syntax for java.util.ArrayList.size() method

Parameters of Java List size()

The List size() method in Java does not take any input parameter.

Return Values of Java List size()

Java List size() method returns the count of elements present in the ArrayList. For example, if an ArrayList has five string objects stored in it, the size method will return five.

introduction link

Exception for Java List size()

NullPointerException is thrown when the program tries to calculate the size of a null ArrayList. You can check whether the list is null or not to avoid it.

Example: Java list size()

Assume you have an ArrayList with some names, and you want to use the size() method to find out how big it is.

In the code below, we've created an ArrayList of Strings in Java, added two string objects to it with the add() method, and used the size() method to calculate the number of elements in the list. We embellished it with a few more elements and recalculated the size.

Output:

Internal working of ArrayList.size() in Java

The implementation of ArrayList in java is done in such a way that it always keeps track of the current size of the list in a separate variable named size. when we call the size() method, it simply returns the value stored in the size variable that is a constant time operation.

Now let's understand the size() method working on the previous example.

  • Initially, myList is empty, and the size variable stores 0 in it. Then we added two values to the list, and the size variable stores 2 now. Then, the size() method called on the list returns the same value (2).

    • mylist1
  • Again, we added two values, and the size is updated to 4. When we call the size() method on the list; it returns 4.

    • mylist3

Since the size() method returns the value stored in the size variable, it will be a constant-time operation.

More Example

We can use the same size() method to get the number of elements stored in a hashset in java. The example below shows the implementation for the same.

Output:

Conclusion

  • The List size() method in Java can be used to calculate the size of a list; it requires no parameters and returns an integer that represents the list's size.
  • Because ArrayList has a dynamic size, the size() method's return value can vary, as shown in the example.
  • ArrayLists can be used to store dynamic size data such as student records, covid-19 vaccination data, etc.