Java – Count the number of items in a List

In Java, we can use List.size() to count the number of items in a List


package com.mkyong.example;

import java.util.Arrays;
import java.util.List;

public class JavaExample {

    public static void main(String[] args) {

        List<String> list = Arrays.asList("a", "b", "c");
        System.out.println(list.size());

    }

}

Output


3

References

  1. Java doc – List

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments