Given the definition of the Emp class: public class Emp private String eName; private Integer eAge; Emp(String eN, Integer eA) { this.eName = eN; this.eAge = eA; } public Integer getEAge () {return eAge;} public String getEName () {return eName;} } and code fragment: List<Emp>li = Arrays.asList(new Emp("Sam", 20), New Emp("John", 60), New Emp("Jim", 51)); Predicate<Emp> agVal = s -> s.getEAge() > 50;//line n1 li = li.stream().filter(agVal).collect(Collectors.toList()); Stream<String> names = li.stream()map.(Emp::getEName); //line n2 names.forEach(n -> System.out.print(n + " ")); What is the result?
Click on the arrows to vote for the correct answer
A. B. C. D.B.
The code defines an Emp
class with eName
and eAge
instance variables along with a constructor and getters. It also creates a list of Emp
objects and applies some operations on it using streams.
csharppublic class Emp { private String eName; private Integer eAge; Emp(String eN, Integer eA) { this.eName = eN; this.eAge = eA; } public Integer getEAge () { return eAge; } public String getEName () { return eName; } } List<Emp> li = Arrays.asList(new Emp("Sam", 20), new Emp("John", 60), new Emp("Jim", 51)); Predicate<Emp> agVal = s -> s.getEAge() > 50; li = li.stream().filter(agVal).collect(Collectors.toList()); Stream<String> names = li.stream().map(Emp::getEName); names.forEach(n -> System.out.print(n + " "));
The li
list contains three Emp
objects. The agVal
predicate is used to filter Emp
objects whose eAge
is greater than 50. The li
stream is filtered using the agVal
predicate and collected back into the li
list.
The names
stream is then created by mapping the Emp
objects to their eName
fields. Finally, the names
stream is printed to the console using the forEach
method.
The expected output is "John Jim"
because only the Emp
objects with eAge
greater than 50 pass the filter and their corresponding eName
values are mapped to the names
stream.