Get first element from list if value is null - Java Quiz Question


Write a program to get first element from given Integer list, only if given value is null. If the given value is not null, return it.

Input (Integer List, Integer) Output (Integer)
[14, 15, 21], 65 65
[-21, -45], -45 -45
[23, 45, 67, 89], null 23
[], null null
null, null null


import java.util.*;
class GetFirstElementInListIfValueIsNull
{
    public static void main(String s[])
    {
        ArrayList<Integer> list = new ArrayList<Integer>();
        list.add(23);
        list.add(45);
        list.add(67);
        Integer value = null;
        System.out.println("The input value is null, then the first element of list [23, 45, 67] is " + getFirstElementIfValueIsNull(list, value));

    }


    public static Integer getFirstElementIfValueIsNull(List<Integer> list, Integer value)
    {
        Integer result = null;
        return result;
    }

}