
- 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 find the size of a Java list?
List provides a method size() to get the count of elements currently present in the list.
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
Following example shows how to check size of a list using size() method.
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> list = new ArrayList<>(Arrays.asList(1,2,3)); System.out.println("List: " + list); System.out.println("List size: " + list.size()); list.add(4); list.add(5); list.add(6); System.out.println("List: " + list); System.out.println("List size: " + list.size()); list.remove(1); System.out.println("List: " + list); System.out.println("List size: " + list.size()); } }
Output
This will produce the following result −
List: [1, 2, 3] List size: 3 List: [1, 2, 3, 4, 5, 6] List size: 6 List: [1, 3, 4, 5, 6] List size: 5
- Related Articles
- How do I set the size of a list in Java?
- How do I find an element in Java List?
- How do I empty a list in Java?
- How do I search a list in Java?
- How do I find out the size of a canvas item in Tkinter?
- How do I insert elements in a Java list?
- How do we get the size of a list in Python?
- How do we get size of a list in Python?
- How do I get length of list of lists in Java?
- How to find the size of a list in C#?
- How do I remove multiple elements from a list in Java?
- How do I turn a list into an array in Java?
- How to check the Java list size?
- How can I find elements in a Java List?
- How do I change the font size of ticks of matplotlib.pyplot.colorbar.ColorbarBase?

Advertisements