17.5.2 Filtering Strings Then Sorting Them in Case-Insensitive Ascending Order

Lines 24–28 filter and sort the Strings. Line 25 creates a Stream<String> from the array strings, then line 26 calls Stream method filter to locate all the Strings that are greater than "m", using a case-insensitive comparison in the Predicate lambda. Line 27 sorts the results and line 28 collects them into a List<String> that we output as a String. In this case, line 27 invokes the version of Stream method sorted that receives a Comparator as an argument. As you learned in Section 16.7.1, a Comparator defines a compare method that returns a negative value if the first value being compared is less than the second, 0 if they’re equal and a positive value if the first ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.