Junior — Middle+
Forming list B from list A with field selection and transformation
livecode
Task condition
There are two classes A and B. It is required to obtain a list of B objects, built based on an existing listA. Only those elements whose y value is even should be included in the resulting list, where y is calculated by the formula y = 3 * a.x / 2.
public class A { public int x;}
public class B {
public int y;
public B(int y){this.y=y}
}
List<A> listA = List.of(....);
List<B> listB = listA.stream()
.map(a -> new B(3 * a.x / 2))
.filter(b -> b.y % 2 == 0)
.collect(Collectors.toList());
You need to perform filtering and transformation using Java language features (e.g., Stream API) or any other approach that allows obtaining the required listB.