Preconditions - Java

JavaServer Side ProgrammingProgramming

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

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");
   }
   my_fun(myList);
}

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.

raja
Updated on 14-Jul-2020 07:08:28

Advertisements