
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
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.
In this article we will look at how to get the size of a collection in Java. The size of a collection is the number of elements it contains.
Following are the ways to get the size of a collection in Java:
- Using size() Method
- Using loops
- Using Streams API
Getting Size of Collection Using size() Method
The size() method is a built-in method of the Collection interface which is used for getting the size of a collection. It returns the number of elements in the collection.
Example
In the following example, we will create a collection and then get its size using the size() method:
import java.util.*; public class CollectionSize { public static void main(String[] args) { List<String> list = new ArrayList<>(); Set<String> set = new HashSet<>(); Map<String, Integer> map = new HashMap<>(); list.add("Java"); list.add("Python"); list.add("JavaScript"); list.add("C++"); list.add("Swift"); set.add("Java"); set.add("Python"); map.put("Java", 1); map.put("Python", 2); System.out.println("Original List: " + list); System.out.println("Original Set: " + set); System.out.println("Original Map: " + map); int size = list.size(); System.out.println("Size of the list collection: " + size); int setSize = set.size(); System.out.println("Size of the set collection: " + setSize); int mapSize = map.size(); System.out.println("Size of the map collection: " + mapSize); } }
Output
Following is the output of the above code:
Original List: [Java, Python, JavaScript, C++, Swift] Original Set: [Java, Python] Original Map: {Java=1, Python=2} Size of the list collection: 5 Size of the set collection: 2 Size of the map collection: 2
Getting Size of Collection Using Loops
We can also get the size of a collection using loops. We will loop the collection and every time we encounter an element, we will increment a counter variable. This way we can count the number of elements in the collection.
Example
In the following example, we will create a collections and then get its size using loops:
import java.util.*; public class CollectionSizeUsingLoops { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Java"); list.add("Python"); list.add("JavaScript"); list.add("C++"); list.add("Swift"); System.out.println("Original List: " + list); int size = 0; for (String element : list) { size++; } System.out.println("Size of the collection using loops: " + size); } }
Output
Following is the output of the above code:
Original List: [Java, Python, JavaScript, C++, Swift] Size of the collection using loops: 5
Getting Size of Collection Using Streams API
Stream API are useful for processing sequences of elements. First, we convert the collection to a stream and then use the count() method to get the size of the collection. The count() method returns the number of elements in the stream.
Example
In the following example, we will create a collection and then get its size using the Stream API:
import java.util.*; public class CollectionSizeUsingStreams { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Java"); list.add("Python"); list.add("JavaScript"); list.add("C++"); list.add("Swift"); System.out.println("Original List: " + list); long size = list.stream().count(); System.out.println("Size of the collection using streams: " + size); } }
Output
Following is the output of the above code:
Original List: [Java, Python, JavaScript, C++, Swift] Size of the collection using streams: 5