How to Get a Size of Collection in Java?
Given a Collection in Java, the task is to find the length or size of the collection.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Input: Array_List: [1, 2, 3,4] Output: 4 Input: Linked_List: [geeks, for, geeks] Output: 3
The Size of the different collections can be found with the size() method. This method returns the number of elements in this collection. This method does not take any parameters.
Below is the Implementation:
Size of list = 4 Array_List = [1, 2, 3, 3]
Size of the linkedlist = 3 Linkedlist = [GEEKS, FOR, GEEKS]