Java List Size

In Java, the size() method is a built-in method of the ArrayList class. The number of items included in this list container can be found using the size() method of the Java list interface. The list should not be null while getting the size using the size() method. Because, when an attempt is made to determine the size of a null list then a NullPointerException is raised. The time complexity measured for the list size() method in java is O(n) where n indicates the entire elements of the list.

Example 1:

Before getting the size of the list, it should be created. The size as an argument to the list constructor is provided to the list to produce a list of a certain size. The method is used to create the list which takes a simple array as input and gives back an IntStream that is sequential.

import java.util.Arrays; import java.util.List;
class SizeList1
public static void main(String[] args)
int MySize = 8;
List MyList = int[MySize])
                        .boxed().collect (Collectors.toList());


To create the size of the array, we have first assigned the essential packages in the head section of this program. Then, a main() method is given to the “SizeList1” Java class that we have constructed. We have declared the primitive int type variable “MySize” and set the numerical value “8” inside it. After that, we provided the variable “MyList” to the list class of integer type. The method is deployed within the variable “MyList”. This method accepts the int[] array where the “MySize” variable is provided for the size of the array. The integer stream of the array is then collected as a collection to transform into the list by using the toList() method. The array of streams with the integer type is shown as a list by the print statement.

The list with the specified size is generated by using the method. As we have not inserted any integer value that is why zero is displayed in the list.

Example 2:

The list is generated with the specified size in the prior size. Now, the program is to get the size of the list after inserting the element in it. The size() method doesn’t acquire any parameter value but is used with the list object for the size.

import java.util.*;
public class SizeList2 {
public static void main(String[] arg){
List l = new ArrayList();
System.out.println("List:" +l)

We designed the Java public class “SizeList2,” which includes the main() method. The main() method is used to implement the program for the size() method of Java. We have provided the list interface of type integer which accepts only integer values. The list object is created as “l” which is initialized with the ArrayList() method to generate the list. Then, we employed the add() method with the list object “l” and inserted the four numbers inside the list “l”. Next, we declared an object “s” and initialized this object with the size() method which gets the size of the list “l”. The list “l” will be generated once the size is determined.

The list contains four elements only so the size of the list generated in the output is also four.

Example 3:

The prior example is used to get the list size of the integer. Now, this explains the size of the string list which is created using the HashSet. The step to generate the size of the string list is the same as the size of the integer list, only the data type is different.

import java.util.*;
public class SizeList3 {
public static void main(String args[]){
Set str = new HashSet();
System.out.println("String set:+ str);
The size of the string set: + str.size());

We initially built a java class “SizeList3” which encapsulates the main() method for the source code of the size() method. We have constructed the set interface which declares the object “str” and only takes the string values. We have used the HashSet class which inserts the string values to the list set “str” with the aid of the add() method. We have first displayed the value of the setlist by providing the “str” object to the print statement. Then, we obtained the size of the string set list via the size() method. The size() method is invoked with the object “str” inside the print method.

There are six-string values inserted in the setlist which is fetched by the size() method and output below.

Example 4:

Now the size() method of java is applied to all the characters of the alphabet to get the size of alphabetic characters. The for-loop method iterates over all the characters and gets the size.

import java.util.LinkedList; import java.util.List;
public class SizeList4 {
public static void main(String[] args) {
List CharList= new LinkedList();
for (char c='a';c<='z'; C++) {
System.out.println("Totalalphabets characters: "+CharList.size()); System.out.println("Character List: "+CharList);

We have imported the LinkList and the list class because of the linked list interface. Next, we generated the class “SizeList4” where the main() method of the program is developed. Here, we have defined the list class which takes the character type and declared the object “CharList” for that list. We have initialized the linked list method in the object “CharList” to generate the list of the characters. To get the characters, we employed the for-loop to iterate over all the characters and added all the characters in the add() method. Then, we printed the size of the characters by using the size() method.

The size of the alphabetic characters is “26” which is displayed on the readout below. Along with the size, all the characters are also displayed.

Example 5:

The size of the list can also be reduced by using the limit method of Java. The number of elements that will be retrieved from a stream can be limited by the developer using the limit method. When only the first few elements in the stream need to be processed, the limit method can be helpful.

import*; import java.util.*;
public class SizeList5 {
private static List animals =new ArrayList();
 static {
public static void main (String args[]) {
Console c =System.console();
int ul = Integer.parseInt(c.readLine("Please enter the number: "));
if (ul animals.size()) {
  ul animals.size();
} (System.out::println);

We have built a java class “SizeList5” where we have defined the “animals” object of the class list and set the array list with the ArrayList() method. The array list is added with the string element by calling the add() method. Then, we have a specification for the main() method. Here, we have created the Console object “c” to print the statement of the user. After that, we have an integer object “ul” to limit the user where the ParseInt() parsed the message from the readLine() method. Next, we have specified an if-statement where the size of the list animal is fetched from the size() method. The size of the list is held by the “ul” object of the user limit. When the “ul” object value is greater than the size of the list it generates the size list, and the limit method is called then to get the integer value from the user and display only those list items of a given size.

The user provides the size limit “2” so only two elements from the list are displayed here.


Java’s List size() method can be used to determine a list’s size. It accepts no argument and generates an integer that indicates the list’s size. The ArrayList’s size can be altered because of its size. We have provided a few sample programs of size() method to get the size of the specified ArrayList for both the integer and string elements. Additionally, we have also reduced the size of the ArrayList via a limit method.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.