
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How do I get length of list of lists in Java?
List provides a method size() to get the count of elements currently present in the list. To get size of each list, we can use iterate through each item as list and add their sizes to get the count of all elements present in the list of lists. In this example, we are using streams to achieve the same.
Syntax
int size()
Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
Example
The following example shows how to check length of list of lists using size() method and streams.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<Integer> list1 = new ArrayList<>(Arrays.asList(1,2,3)); List<Integer> list2 = new ArrayList<>(Arrays.asList(4,5,6,7)); List<Integer> list3 = new ArrayList<>(Arrays.asList(8,9)); List<List<Integer>> list = new ArrayList<>(Arrays.asList(list1, list2, list3)); int count = list.stream().mapToInt(i -> i.size()).sum(); System.out.println("Total elements: " + count); } }
Output
This will produce the following result −
Total elements: 9
- Related Articles
- How to get length of a list of lists in Python?
- How do make a flat list out of list of lists in Python?
- How do I get list of methods in a Python class?
- How do I set the size of a list in Java?
- How do I get the average string length in MySQL?
- How do I find the size of a Java list?
- How do I empty a list in Java?
- How do I search a list in Java?
- How do I get a list of all instances of a given class in Python?
- Get positive elements from given list of lists in Python
- How do I insert elements in a Java list?
- How do I find an element in Java List?
- How do you get the index of an element in a list in Java?
- How do you add two lists in Java?
- How to get sublist of List in Java?

Advertisements