#255 Modifying an Object After Stream Filtering
What will be printed?
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
static class A {
int a;
A(int a) {
this.a = a;
}
@Override
public String toString() {
return "{" + a + "}";
}
}
public static void main(String[] args) {
List<A> list1 = Arrays.asList(new A(1), new A(2));
List<A> list2 = list1.stream()
.filter(elem -> elem.a == 1)
.collect(Collectors.toList());
list2.get(0).a = 4;
System.out.println(list1);
System.out.println(list2);
}
}
Question review
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor