

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Get the Size of the Collection
<p>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.</p><p>Below is a demonstration of the same −</p><p><strong>Suppose our input is</strong> −</p><pre class="result notranslate">Input list: [100, 180, 250, 300]</pre><p><strong>The desired output would be</strong> −</p><pre class="result notranslate">The size of the list = 4</pre><h2>Algorithm</h2><pre class="result notranslate">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</pre><h2>Example 1</h2><p>Here, we bind all the operations together under the ‘main’ function.</p><pre class="demo-code notranslate language-java" data-lang="java">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(" The size of the list = " + list_size); } }</pre><h2>Output</h2><pre class="result notranslate">The list is defined as: [100, 180, 250, 300] The size of the list = 4</pre><h2>Example 2</h2><p>Here, we encapsulate the operations into functions exhibiting object oriented programming.</p><pre class="demo-code notranslate language-java" data-lang="java">import java.util.*; public class Demo { static void print_size(List<Integer> input_list){ int list_size = input_list.size(); System.out.println(" The 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); } }</pre><h2>Output</h2><pre class="result notranslate">The list is defined as: [100, 180, 250, 300] The size of the list = 4</pre>
- Related Questions & Answers
- Get the size of a collection in MongoDB using conditions?
- Get the size of the IdentityHashMap in Java
- Get the size of the LabelValue Tuple in Java
- Get the size of an ArrayList in Java
- Java Program to Shuffle the Elements of a Collection
- 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 get the reverse of the NavigableSet
- Java Program to extend the size of an Integer array
- Get size of HashSet in Java
- Get Size of TreeSet in Java
- Java Program to get the seconds since the beginning of the Java epoch
- How to get the size of the tables of a MySQL database?
Advertisements