java

How to Count Number of Elements in a List in Java

In this tutorial, we are going to see how to count the number of elements in a list in Java. In Java, we can use the List.size() method to count the number of elements in a list.
 

Program to Count Number of Elements in a List in Java:
import java.util.*;

public class Main {

    public static void main(String[] args) {
		
        List<String> lang = Arrays.asList("PHP", "Java", "Python", "C++");
        System.out.println(lang.size());
    }
}

Output:

4
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *