Explanation of Solution
a.
Stream<String>:
A stream is an immutable sequence of values that are processed lazily which means that accept the operations that are needed and skip the unnecessary operations. The stream library uses the lambda expressions. When using the lambda function with “Stream<String>”, the Java compiler will configure that the given object is string.
Expression to compute the number of elements starts with letter “a”:
words.filter(w -> w.startsWith("a")).count()
The “filter” method yields a stream of all elements that satisfies the given condition and “w” is the parameter variable. Here, the “startsWith()” method is used to check the prefix of a String and returns the Boolean value based on the specified letter or word. Then, the “count()” method counts the number of elements in the steam.
Explanation of Solution
b.
Expression to compute the number of elements whose length greater than ten starts with letter “a”:
words.filter(w -> w.startsWith("a")).filter(w -> w.length() > 10).count()
The “filter” method yields a stream of all elements that satisfies the given condition and “w” is the parameter variable. Here, the “startsWith()” method is used to check the prefix of a String “a” and also checks whether the length of the string is greater than “10” and the “count()” method counts the number of elements in the steam that satisfies the given condition.
Explanation of Solution
c.
Expression to compute the number of elements that limits to 100 that starts with letter “a”:
words.filter(w -> w.startsWith("a")).limit(100).count()
The “filter” method yields a stream of all elements that satisfies the given condition and “w” is the parameter variable. Here, the “startsWith()” method is used to check the prefix of a String “a” and the “limit()” method restricts the number of elements to 100. Finally, the “count()” method counts the number of elements in the steam.
Want to see more full solutions like this?
Chapter 19 Solutions
Big Java, Binder Ready Version: Early Objects
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education