Consider the following program:
import java.util.*; public class Main { public static void main(String []args) { List<Map<List<Integer>, List<String>>> list = new ArrayList<>(); // ADD_MAP Map<List<Integer>, List<String>> map = new HashMap<>(); list.add(null); // ADD_NULL list.add(map);//from w ww . j a v a2 s . c om list.add(new HashMap<List<Integer>, List<String>>()); // ADD_HASHMAP for(Map element : list) { // ITERATE System.out.print(element + " "); } } }
Which one of the following options is correct?
e)
The lines marked with comments ADD_MAP and ADD_HASHMAP are valid uses of the diamond operator to infer type arguments.
In the line marked with comment ITERATE, the Map type is not parameterized, so it will result in a warning (not a compiler error).
Calling the add()
method passing null does not result in a NullPointerException.
The program, when run, will successfully print the output null, {}, {} (null output indicates a null value was added to the list, and the {} output indicates that Map is empty).