The list is an interface and it is mainly accessed by other classes like ArrayList, LinkedList, and many more. The list size can be modified according to the requirements of the user at runtime. The data stored in the list can be too large or too small and it is a very time-consuming process to calculate the residing elements of the list. Luckily! This hurdle is resolved by the “size()” method.
This guide explains the procedure to use the list size() method in Java.
How to Use the List Size() Method in Java?
The “size()” method is specifically used with the list or arrays to calculate the containing number of elements. This method is applied directly with the targeted list or array name and as a return, this method returns the number of elements residing in that targeted list. The “size()” method can be utilized with data structures like “ArrayList” and “LinkedList” to perform the same functionality.
Syntax
In Java, the syntax for the “size()” method is shown below:
public int size()
//It is utilized in the Java program with lists as
listName.size()
Let’s dive into the practical implementation of the list “size()” method in Java:
Example 1: Retrieving the Size of List Via Size() Method
In this example, the “size()” method is utilized to retrieve the elements residing inside the targeted list. This method is used after the declaration of the list before initialization and after the initialization. To get a better idea about the retrieval of list size/length using the “size()” method. The code for the mentioned scenario is stated below:
import java.util.*;
public class javabeat {
public static void main(String args[])
{
List<String> provList = new ArrayList<String>();
System.out.println("List Containing Elements Before Initialization: \n\t" + provList);
System.out.println("\nList Size/length AfterBefore Initialization: \n\t" + provList.size());
provList.add("Spartacus - 1");
provList.add("Ganicus - 2");
provList.add("Axios - 3");
provList.add("Lucas - 4");
System.out.println("\n\nList Containing Elements After Initialization: \n\t" + provList);
System.out.println("\nList Size/length After Initialization: \n\t" + provList.size());
}
}
The explanation of the above code is as follows:
- First, the default Java utilities are imported, and the class named “javabeat” is created that contains the “main()” method.
- Inside this method, a “String” type list named “provList” is created using the “ArrayList” class. The “System.out.print()” method is then used to display the content of a created “provList” and its size/length by attaching the “size()” method.
- Next, multiple elements are inserted inside the created “provList” using the “add()” method.
- After the list initialization, display the elements of “provList” and its size/length which is retrieved using the “size()” method.
The generated output after the compilation shows that the list size has been retrieved using the “size()” method in Java:
Example 2: Retrieving the Size of LinkedList Via Size() Method
The “size()” method can be used along the data structure like “LinkedList” to retrieve the number of elements residing in that selected LinkedList. This concept is practically demonstrated and implemented in the below code block:
import java.util.*;
public class javabeat {//Initializing the main() driver method
public static void main(String args[])
{ //declaration of Java Linked List
LinkedList<String> provList = new LinkedList<String>();
System.out.println("List Containing Elements Before Initialization: \n\t" + provList);
System.out.println("\nList Size/length Before Initialization: \n\t" + provList.size());
provList.push("Spartacus - 1");
provList.push("Ganicus - 2");
provList.push("Axios - 3");
provList.push("Lucas - 4");
System.out.println("\n\nList Containing Elements After Initialization: \n\t" + provList);
System.out.println("\nList Size/length After Initialization: \n\t" + provList.size());
}
}
The above code works like this:
- First, the LinkedList named “provList” having the data type of “String” is created. This LinkedList containing elements and the size or length using the “size()” method is retrieved and displayed over the console.
- Next, the “push()” method is used to insert elements inside the created “provList” LinkedList.
- After the insertion of values, the content of “provList” is displayed and its size or length is retrieved using the same “size()” method.
- These retrieved data are displayed on the console using the “System.out.println()” method.
The generated output shows that the size of LinkedList before and after the initialization has been retrieved:
Example 3: Retrieving the Size of ArrayList Via Size() Method
The “ArrayList” is also a data structure similar to “LinkedList,” which is already described in the above section. The size or length of the created LinkedList can be retrieved by the usage of the java “size()” method. The Java Program to implement the discussed scenario is shown below:
import java.util.*;//importing basic utilities
public class javabeat {//Initializing the main() driver method
public static void main(String args[])
{
ArrayList<String> provList = new ArrayList<String>();
System.out.println("ArrayList Containing Elements Before Initialization: \n\t" + provList);
System.out.println("\nArrayList Size/length Before Initialization: \n\t" + provList.size());
provList.add("Spartacus - 1");
provList.add("Ganicus - 2");
provList.add("Axios - 3");
provList.add("Lucas - 4");
System.out.println("\n\nArrayList Containing Elements After Initialization: \n\t" + provList);
System.out.println("\nArrayList Size/length After Initialization: \n\t" + provList.size());
}
}
The description of the above code snippet is as follows:
- First, the ArrayList named “provList” with the data type of “String” is declared and its content with length or size is displayed on the console.
- Next, random data is inserted inside the created ArrayList using the “add()” method.
- Once the insertion is completed, the “provList” content is displayed on the console. The size of “provList” is again retrieved using the “size()” method and displayed on the console as well. The retrieved data is then displayed on the console via the “System.out.println()” method.
The generated output shows that the size of ArrayList has been retrieved:
Alternative Approach: Retrieve the Size of a List Via Java 8 Streams API
The “count()” method provided by the Java 8 stream can be used instead of the “size()” method to compute the list size or to retrieve the number of elements residing inside the list. This method is invoked by the “stream()” method which denotes the Stream API. Take a look at the below-mentioned lines of code for a practical demonstration of this method:
import java.util.*;//Importing the Required Java Utilities
public class javabeat {
public static void main(String args[])
{
List<String> provList = new ArrayList<String>();
provList.add("Spartacus - 1");
provList.add("Ganicus - 2");
provList.add("Axios - 3");
provList.add("Lucas - 4");
provList.add("Milan - 5");
provList.add("roblox - 6");
System.out.println("\n\nList Elements: \n" + provList);
System.out.println("\nRetrieved List Size/length: \n" + provList.stream().count());
}
}
The above code works like this:
- First, create a list “provList” using the “ArrayList” class and insert random values into this list by the utilization of the “add()” method.
- Next, display the content of this list by passing the “provList” as an argument for the “System.out.println()” method.
- After that, attach the “stream()” and “count()” methods in sequence to the “provList” list and display the result over the console. The result should contain the size/length of the targeted “provList” list.
The generated output shows that the list size has been retrieved:
That’s all about the usage of a list size() method in Java.
Conclusion
The size() method is used to find the size/length of a list. This method is applied directly to the targeted list and as a result, returns the number of elements residing in that list. This method can also be utilized with data structures like ArrayList, LinkedList, HashSet, or Arrays. As an alternative approach, the “count()” method provided by Java 8 Stream API can be used as well to display the size/length of a list. This guide has demonstrated the usage and working of the “size()” method in Java.