Java SE 8 Programmer II: Java-Oracle DB-J2EE-

Java-Oracle DB-J2EE-

Question

Given the code fragments: class TechName { String techName; TechName (String techName){ this.techName=techName; } } and List<TechName> tech = Arrays.asList( new TechName("Java-"), new TechName("Oracle DB-"), new TechName("J2EE-") ); Stream<TechName> stre = tech.stream(); //line n1 Which should be inserted at line n1 to print Java-Oracle DB-J2EE-?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

B.

The given code creates a class TechName with a single field techName and a constructor to set its value. It then creates a List of TechName objects and assigns it to a reference variable tech. It also creates a stream stre from the tech list.

To print the concatenated string of techName values of all TechName objects in the tech list, we need to use a terminal operation on the stream. There are multiple terminal operations available in the Stream API that can be used for this purpose. Let's evaluate each of the given options to find the correct one.

Option A: stre.forEach(System.out::print); This option uses the forEach method to print each element of the stream. However, this will only print the TechName objects themselves, not their techName values. Therefore, option A is incorrect.

Option B: stre.map(a-> a.techName).forEach(System.out::print); This option uses the map method to transform each element of the stream into its techName value, and then uses forEach to print each transformed value. This will print the concatenated string of all techName values. Therefore, option B is the correct answer.

Option C: stre.map(a-> a).forEachOrdered(System.out::print); This option uses the map method to create a new stream of TechName objects, and then uses forEachOrdered to print each element of the stream in order. However, this will only print the TechName objects themselves, not their techName values. Therefore, option C is incorrect.

Option D: stre.forEachOrdered(System.out::print); This option uses the forEachOrdered method to print each element of the stream in order. However, this will only print the TechName objects themselves, not their techName values. Therefore, option D is incorrect.

Therefore, the correct answer is option B: stre.map(a-> a.techName).forEach(System.out::print);.