What is the result of compiling and running the following application?.
package names; // ww w . j a va2s.c om import java.util.*; import java.util.function.*; interface ApplyFilter { void filter(List<String> input); } public class Main { static Function<String,String> first = s -> {System.out.println(s); return s;}; static Predicate second = t -> "bob".equalsIgnoreCase(t); public void process(ApplyFilter a, List<String> list) { a.filter(list); } public static void main(String[] contestants) { final List<String> people = new ArrayList<>(); people.add("Bob"); people.add("bob"); people.add("B"); people.add("A"); final Main f = new Main(); f.process(q -> { q.removeIf(second); q.forEach(first); }, people); } }
D.
The code does not compile, so Options A, B, and F are incorrect.
The first compilation error is in the declaration of the lambda expression for second.
It does not use a generic type, which means t is of type Object.
Since Object, unlike String, does not have a method equalsIgnoreCase()
, the lambda expression does not compile.
The second compilation issue is in the lambda expression in the main()
method.
Notice that process()
takes an ApplyFilter
instance, and ApplyFilter
is a functional interface that takes a List<String> object.
q in this lambda expression is treated as an instance of List<String>.
The forEach()
method defined in Collections requires a Consumer instance, not a Function, so the call q.forEach(first) does not compile.
For these two reasons, Option D is the correct answer, since the rest of the code compiles without issue.