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

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar