count how many sub ArrayList are contained in one code example
Example 1: how to count the number of occurrences of an element in a arraylist in java
ArrayList<String> listName = new ArrayList<>();
int occurrences = Collections.frequency(listName, "element");
Example 2: get number of items in arraylist java
import java.util.ArrayList;
public class ArrayListDemo{
public static void main(String[] args) {
System.out.println("Welcome to Java Program to find the length of array list");
ArrayList<String> listOfBanks = new ArrayList<>();
int size = listOfBanks.size();
System.out.println("size of array list after creating: " + size);
listOfBanks.add("Citibank");
listOfBanks.add("Chase");
listOfBanks.add("Bank of America");
size = listOfBanks.size();
System.out.println("length of ArrayList after adding elements: " + size);
listOfBanks.clear();
size = listOfBanks.size();
System.out.println("size of ArrayList after clearing elements: " + size);
}
}
Output
Welcome to Java Program to find length of array list
the size of array list after creating: 0
the length of ArrayList after adding elements: 3
the length of ArrayList after clearing elements: 0