Chapter SIXTEEN
Stream Operations on Collections


Exam Objectives

Develop code that uses Stream data methods and calculation methods.
Use java.util.Comparator and java.lang.Comparable interfaces.
Sort a collection using Stream API.

Answers

1. The correct answer is A.
String::compareTo is a valid method reference. Since compareTo sorts in natural order (A, B, C, etc.), "Collections" becomes the first element of the List.


2. The correct answer is D.
Option A is invalid. Comparator takes two arguments.
Option B is invalid. reversed() is not a static method.
Option C is invalid. thenComparing is not a static method.
Option D is valid. The static method Comparator.comparing takes a lambda expression with one argument that represents a Function that returns the property on which a Comparator is created.


3. The correct answer is C.
The max method of the Stream interface takes a Comparator as an argument. It's not like the max method of the primitive streams that know how to calculate the maximum element.


4. The correct answer is C.
The static method Comparator.comparing takes a lambda expression with one argument that represents a Function. Taking two arguments would require a BiFunction.