
- 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
Java Program to Get the Size of the Collection
In this article, we will understand how to get the size of the collection. The Collection is a framework that provides architecture to store and manipulate the group of objects. Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion.
Below is a demonstration of the same −
Suppose our input is −
Input list: [100, 180, 250, 300]
The desired output would be −
The size of the list = 4
Algorithm
Step 1 - START Step 2 - Declare a list namely input_list. Step 3 - Define the values. Step 4 - Using the function size(), we get the size of the input_list. Step 5 - Display the result Step 6 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
import java.util.*; public class Demo { public static void main(String[] args){ List<Integer> input_list = new ArrayList<Integer>(); input_list.add(100); input_list.add(180); input_list.add(250); input_list.add(300); System.out.println("The list is defined as: " + input_list); int list_size = input_list.size(); System.out.println("\nThe size of the list = " + list_size); } }
Output
The list is defined as: [100, 180, 250, 300] The size of the list = 4
Example 2
Here, we encapsulate the operations into functions exhibiting object oriented programming.
import java.util.*; public class Demo { static void print_size(List<Integer> input_list){ int list_size = input_list.size(); System.out.println("\nThe size of the list = " + list_size); } public static void main(String[] args){ List<Integer> input_list = new ArrayList<Integer>(); input_list.add(100); input_list.add(180); input_list.add(250); input_list.add(300); System.out.println("The list is defined as: " + input_list); print_size(input_list); } }
Output
The list is defined as: [100, 180, 250, 300] The size of the list = 4
- Related Articles
- Get the size of a collection in MongoDB using conditions?
- Swift program to Get the Size of Set
- Get the size of the IdentityHashMap in Java
- Java Program to Shuffle the Elements of a Collection
- Get the size of the LabelValue Tuple in Java
- Get the size of an ArrayList in Java
- Java Program to double the size of an array
- Get Size of Java LinkedHashMap
- Get Size of Java LinkedHashSet
- How to get the Tab Size of a JTextArea in Java?
- Java Program to extend the size of an Integer array
- Java Program to get the reverse of the NavigableSet
- Get size of HashSet in Java
- Get Size of TreeSet in Java
- Java Program to Print a Collection

Advertisements