Just use
int listCount = data.size();
That tells you how many lists there are (assuming none are null). If you want to find out how many strings there are, you'll need to iterate:
int total = 0;
for (List<String> sublist : data) {
// TODO: Null checking
total += sublist.size();
}
// total is now the total number of strings
Answer from Jon Skeet on Stack Overflow Top answer 1 of 4
80
Just use
int listCount = data.size();
That tells you how many lists there are (assuming none are null). If you want to find out how many strings there are, you'll need to iterate:
int total = 0;
for (List<String> sublist : data) {
// TODO: Null checking
total += sublist.size();
}
// total is now the total number of strings
2 of 4
7
Java 8
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
public class HelloWorld{
public static void main(String []args){
List<List<String>> stringListList = new ArrayList<>();
stringListList.add(Arrays.asList(new String[] {"(0,0)", "(0,1)"} ));
stringListList.add(Arrays.asList(new String[] {"(1,0)", "(1,1)", "(1,2)"} ));
stringListList.add(Arrays.asList(new String[] {"(2,0)", "(2,1)"} ));
int count=stringListList.stream().mapToInt(i -> i.size()).sum();
System.out.println("stringListList count: "+count);
}
}
How does the size() method work in Java Lists?
When you call the size() method on a Java List, it does a little bit of internal magic. Rather than iterating through all elements, it directly accesses a variable or field that keeps track of the number of elements in the List, returning that value to you swiftly.
upgrad.com
upgrad.com › home › tutorials › software & tech › java list size
Java List Size: Understanding and Utilizing Dynamic Data Structures
Why is the time complexity of the size() method constant?
The efficiency of the size() method is a boon. It boasts a constant time complexity because it merely retrieves the stored size value. It doesn't need to traverse through the List's elements, ensuring a quick and efficient response regardless of the List's size.
upgrad.com
upgrad.com › home › tutorials › software & tech › java list size
Java List Size: Understanding and Utilizing Dynamic Data Structures
Why is the size of a List different from its capacity?
Size and capacity may seem interchangeable, but in the context of a Java List, they convey distinct concepts. The size refers to the actual count of elements in the List, while capacity indicates how many elements it can house before needing to resize. Understanding this distinction helps manage lists more effectively.
upgrad.com
upgrad.com › home › tutorials › software & tech › java list size
Java List Size: Understanding and Utilizing Dynamic Data Structures
Videos
02:55
ArrayList size() method in Java | How to find ArrayList length ...
07:19
Java Tutorial - 05 - Using Array Length Instance Variable - YouTube
06:33
What is the difference between length, length() and size() (Core ...
06:43
How to find min & max length of Strings from List, Java8+ - YouTube
W3Schools
w3schools.com › java › ref_arraylist_size.asp
Java ArrayList size() Method
assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new package private protected public return requires short static super switch synchronized this throw throws transient try var void volatile while Java String Methods · charAt() codePointAt() codePointBefore() codePointCount() compareTo() compareToIgnoreCase() concat() contains() contentEquals() copyValueOf() endsWith() equals() equalsIgnoreCase() format() getBytes() getChars() hashCode() indexOf() isEmpty() join() lastIndexOf() length() matches() offsetByCodePoints() regionMatches() replace() replaceAll() replaceFirst() split() startsWith() subSequence() substring() toCharArray() toLowerCase() toString() toUpperCase() trim() valueOf() Java Math Methods
Upgrad
upgrad.com › home › tutorials › software & tech › java list size
Java List Size: Understanding and Utilizing Dynamic Data Structures
3 weeks ago - On the other hand, the length property is a fixed value for arrays, representing the number of elements that can be stored in the array. Let's explore a few more examples to solidify our understanding of the Java List size() method.
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › List.html
List (Java Platform SE 8 )
October 20, 2025 - If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the list is set to null. (This is useful in determining the length of the list only if the caller knows that the list does ...
Scaler
scaler.com › home › topics › java list size()
Java List Size() - Scaler Topics
April 28, 2024 - You can check whether the list is null or not to avoid it. 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 ...
TutorialsPoint
tutorialspoint.com › how-do-i-get-length-of-list-of-lists-in-java
How do I get length of list of lists in Java?
June 5, 2025 - In the below example, we will create 3 lists of lists and then use a for loop to get the length of each list. import java.util.ArrayList; import java.util.List; public class LengthOfListOfLists { public static void main(String[] args){ List<List<Integer>> listOfLists = new ArrayList<>(); List<Integer> list1 = new ArrayList<>(); list1.add(1); list1.add(2); List<Integer> list2 = new ArrayList<>(); list2.add(3); list2.add(4); list2.add(5); List<Integer> list3 = new ArrayList<>(); list3.add(6); listOfLists.add(list1); listOfLists.add(list2); listOfLists.add(list3); for(List<Integer> list : listOfLists) { System.out.println("Length of list: " + list.size()); } } }
Coderanch
coderanch.com › t › 552836 › java › find-size-occupied-arraylist-kilobytes
to find the size occupied by arraylist in kilobytes - i want to find the size of an arraylist (Java in General forum at Coderanch)
You need to send the items in the array list in a serialized of buffered way and that can not be larger than 4 Mb? If you REALLY want to know the byte size of an object, the "trick" is to convert it to an byte[] and look at the size of it. That is done like this: NOTE: This is serialized object ...
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › util › List.html
List (Java SE 21 & JDK 21)
January 20, 2026 - If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the list is set to null. (This is useful in determining the length of the list only if the caller knows that the list does ...
Naukri
naukri.com › code360 › library › java-program-to-find-the-length-or-size-of-an-arraylist
Java Program to Find the Length/Size of an ArrayList
September 19, 2025 - Almost there... just a few more seconds
TutorialsPoint
tutorialspoint.com › how-do-i-set-the-size-of-a-list-in-java
How do I set the size of a list in Java?
June 10, 2025 - The size of the list will grow dynamically as elements are added. The syntax for creating an ArrayList with a specific size is as follows: ... We should use index > 0 to add an element, otherwise you will get an IndexOutOfBoundsException as the index will be out of range, considering size is ...
Baeldung
baeldung.com › home › java › java array › the capacity of an arraylist vs the size of an array in java
The Capacity of an ArrayList vs the Size of an Array in Java | Baeldung
November 11, 2025 - Integer[] array = new Integer[100]; System.out.println("Size of an array:" + array.length); Here, we created an Integer array of size 100, which resulted in the below output ... Technically, the default capacity (DEFAULT_CAPACITY) of a newly created ArrayList is 10. However, Java 8 changed how this initial capacity is used for performance reasons. It’s not used immediately and is guaranteed lazily once a new item is added to the list.
Reddit
reddit.com › r/learnprogramming › [java] how to write .size() method for an arraylist?
r/learnprogramming on Reddit: [Java] How to write .size() method for an ArrayList?
April 30, 2013 -
I have an ArrayList and I need to override the .size() method to get the size. Any help would be appreciated
Top answer 1 of 5
5
Look ar ArrayList's implementation, create your own class, and implement the most useful methods(add,get,remove,size). It should be pretty straightforward. As you'll see, there is a private variable size, that increases and decreases when you add/remove an object.
2 of 5
3
I might be wrong, but ArrayList's size() method does that already, doesn't it?
Codecademy
codecademy.com › docs › java › arraylist › .size()
Java | ArrayList | .size() | Codecademy
January 27, 2023 - The .size() method of the ArrayList class returns the number of elements in the list. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more! ... Learn to code in Java — a robust programming language used to create ...
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-find-the-length-size-of-an-arraylist
Java Program to Find the Length/Size of an ArrayList - GeeksforGeeks
July 11, 2025 - // Java program to find the size // of an ArrayList import java.util.*; public class GFG { public static void main(String[] args) throws Exception { // Creating object of ArrayList<Integer> ArrayList<Integer> arrlist = new ArrayList<Integer>(); // Populating arrlist arrlist.add(1); arrlist.add(2); arrlist.add(3); arrlist.add(4); arrlist.add(5); // print arrlist System.out.println("ArrayList: " + arrlist); // getting total size of arrlist // using size() method int size = arrlist.size(); // print the size of arrlist System.out.println("Size of ArrayList = " + size); } }
W3Schools
w3schools.com › java › java_arraylist.asp
Java ArrayList
Java Data Structures Java Collections Java List Java ArrayList Java LinkedList Java List Sorting Java Set Java HashSet Java TreeSet Java LinkedHashSet Java Map Java HashMap Java TreeMap Java LinkedHashMap Java Iterator Java Algorithms · Java Wrapper Classes Java Generics Java Annotations Java RegEx Java Threads Java Lambda Java Advanced Sorting ... How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of Rectangle Celsius to Fahrenheit Sum of Digits Check Armstrong Num Random Number Count Words Count Vowels in a String Remove Vowe