Java – How to clear list

All Elements in List can be removed by clear() method. Clear() method in List clear List object and size of the List will become 0

Example of clear in List

import java.util.ArrayList;
import java.util.List;

public class ListClearExample {

    public static void main(String[] args) {

        List<String> ls=new ArrayList<String>();

        ls.add("one");
        ls.add("Three");
        ls.add("two");
        ls.add("four");

        ls.clear(); // clear all elements from list collection

        System.out.println("Size of list after clear :"+ls.size());
    }
}

Output

Size of list after clear :0

Tags:

Bookmark  

 

Leave a Reply

Security Code:

 

  Random Post