Does Java 8 Streaming Filter & Collect return references to same objects in the list?

There is no general way to clone objects in Java.

It would be fundamentally impossible for it to automatically return different instances.


The answer to your question is yes. As depicted below:

  private void run() {

    List<Ref> original = new ArrayList<>();
    original.add(new Ref(1, "A"));
    original.add(new Ref(2, "B"));
    original.add(new Ref(3, "C"));

    System.out.println("Original: " + original);
    // Original: [Ref{id=1, name='A'}, Ref{id=2, name='B'}, Ref{id=3, name='C'}]

    List<Ref> results = original.stream().collect(Collectors.toList());

    System.out.println("Aggregated: " + results);
    // Aggregated: [Ref{id=1, name='A'}, Ref{id=2, name='B'}, Ref{id=3, name='C'}]

    results.forEach(r -> r.name = r.name + "CHANGED");

    System.out.println("Original: " + original);
    //Original: [Ref{id=1, name='ACHANGED'}, Ref{id=2, name='BCHANGED'}, Ref{id=3, name='CCHANGED'}]

  }