Preconditions - Java

Precondition to check if the list passed as parameter is empty or not. Let us see an example −


public void my_fun(List<Object> myList){
   if (myList == null){
      throw new IllegalArgumentException("List is null");
   if (myList.isEmpty()){
      throw new IllegalArgumentException("List is empty");

A void function named ‘my_fun’ is defined that takes a list of objects as its parameters. If the list is null, it prints the relevant message. If the list has no elements in it, a specific message is displayed. The function is called by passing the list as a parameter. This code snippet is a pre-condition which can be used to check if the list is empty or null.

Updated on: 14-Jul-2020


Kickstart Your Career

Get certified by completing the course

Get Started