HRS - Ask. Learn. Share Knowledge. Logo

In Computers and Technology / High School | 2025-07-08

Refer to the code given below where String javaReferences contains the names of all the java reference books: ```java public class Test { public static void main(String[] args) { String javaReferences = " Core Java Volume I - Fundamentals \n" + "Effective Java \n" + "Java: A Beginner's Guide\n" + "Java - The Complete Reference \n" + "Head First Java \n" + "Java Concurrency in Practice"; // Line1 } } ``` Assume Java 11 is used. Requirement: 1. It is required to replace Line1 to create a List with all the java reference book names. 2. It is also required to remove the leading and trailing whitespaces while collecting the book names. Identify the correct syntax that will fulfill the requirement. Options: 1. List filteredJavaReferences = javaReferences.lines() .filter(Predicate.not(line -> !line.isBlank())) .map(String strip) .collect(Collectors.toList()); 2. List filteredJavaReferences = javaReferences.lines() .filter(line -> !line.isBlank()) .map(String leading) .map(String trailing) .collect(Collectors.toList()); 3. List filteredJavaReferences = javaReferences.lines() .filter(line -> !line.isBlank()) .map(String strip) .collect(Collectors.toList()); 4. List filteredJavaReferences = javaReferences.filter() .Predicate.not(line -> !line.isBlank()) .map(String leading) .map(String trailing) .collect(Collectors.toList());

Asked by sarvan8378

Answer (1)

To fulfill the requirement of replacing Line 1 in the given Java code so that it creates a list of all the Java reference book names with leading and trailing whitespaces removed, you should choose the correct syntax from the provided options.
The goal is to split the string javaReferences on line breaks into individual lines, filter out any blank lines, remove leading and trailing whitespaces from each line, and collect the results into a List.
Let's analyze the provided options:

Option 1:
List filteredJavaReferences = javaReferences.lines() .filter(Predicate.not(line -> !line.isBlank())) .map(String strip) .collect(Collectors.toList());
This option has a syntax error because it incorrectly uses Predicate.not and the map method.

Option 2:
List filteredJavaReferences = javaReferences.lines() .filter(line -> !line.isBlank()) .map(String leading) .map(String trailing) .collect(Collectors.toList());
This option incorrectly uses map(String leading) and map(String trailing), which are not valid Java method references.

Option 3:
List filteredJavaReferences = javaReferences.lines() .filter(line -> !line.isBlank()) .map(String::strip) .collect(Collectors.toList());
This option correctly filters out non-blank lines, applies String::strip to remove leading and trailing whitespace, and collects them into a List. Therefore, this is the correct choice.

Option 4:
List filteredJavaReferences = javaReferences.filter() .Predicate.not(line -> !line.isBlank()) .map(String leading) .map(String trailing) .collect(Collectors.toList());
This option contains several syntax errors and is not a valid code.


Therefore, the correct answer is Option 3 :
List filteredJavaReferences = javaReferences.lines() .filter(line -> !line.isBlank()) .map(String::strip) .collect(Collectors.toList());
This option uses javaReferences.lines() to split the string by lines, .filter(line -> !line.isBlank()) to remove any lines that are blank, .map(String::strip) to trim each line, and finally, .collect(Collectors.toList()) to gather all filtered and trimmed lines into a list.

Answered by AvaCharlotteMiller | 2025-07-21