When using java 8 map, filter and collect, does it create new objects or modify existing objects?

Viewed 2169

I have the code below where I am doing filtering using a filter and replace function inside my map and then I am doing a collect to replace my original array. My question is whether my original objects in inputArr are being discarded and new objects are created or are the same objects being modified when I am doing a collect?

public static void test () {
    inputArr.stream().filter(s -> toRemove.contains(s)).
            forEach(u -> u.replaceAll("2","X"));
    inputArr = inputArr.stream().map(x -> filterReplace(x,toRemove)).collect(Collectors.toList());
    System.out.println(inputArr);
}

static String filterReplace(String input, Set<String> toRemove) {
    if (toRemove.contains(input)) {
        return "X";
    }
    else
        return input;
}
2 Answers
Related