0

In my business logic, I want to replace one list completely with another list.

Is the below approach correct?

public class TestListReplacement {

    public static void main(String[] args) {

        List<String> values= new ArrayList<String>();
        values.add("a");
        values.add("b");
        values.add("c");
        values.add("d");
        values.add("e");
        values.add("f");

        List<String> valuesNew= new ArrayList<String>();
        valuesNew.add("g");
        valuesNew.add("h");
        valuesNew.add("i");
        valuesNew.add("j");
        valuesNew.add("k");

        values= valuesNew;
        for(String val:values){
            System.out.println(val);
        }
    }
}

This seems to work fine, but I want to know if in some use cases, it might not work.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
user3756951
  • 45
  • 1
  • 6
  • its correct and will work all the time – Mustanser Iqbal Mar 24 '16 at 05:35
  • 1
    Your code isn't doing anything specific with the lists. It simply shows a variable's object reference being overwritten with a different object reference. If you're trying to update the values within the existing list, that's not what your code is doing. If you're expecting other places in your code that are referring to that list to see the new values, that won't happen. Based on your description above, and the isolated code sample, it's not clear what problem you're trying to address, and whether your solution works or not. – nbrooks Mar 24 '16 at 05:38
  • 1
    Actually, this question is a duplicate of http://stackoverflow.com/questions/3823398/better-practice-to-re-instantiate-a-list-or-invoke-clear (and a few others). – nbrooks Mar 24 '16 at 06:04

1 Answers1

6

If I understand correctly what you're trying to do, what about

values.clear(); // delete original contents in the list
values.addAll(valuesNew); // insert everything in the new list

To add some more context from answers from @nbrooks and others: Overwriting the reference isn't always an option and, frankly, the perceived inefficiency of iterating over the collection twice is almost always irrelevant in practice. If you need to retain the same list instance (because it's being referred to at other places in the code) overwriting the variable doesn't update those other places.

Willcodeforfun
  • 361
  • 3
  • 10
  • @Jonny Because if the list is a member of some other class, the latter has no effect? Different problems have different solutions, and this answer is the most straightforward to replace all the values in a given list with new values. Overwriting the variable does nothing to change other references to the initial, existing list. – nbrooks Mar 24 '16 at 05:43
  • @nbrooks isn't calling `clear()` wasting time? you would rather simply get rid of the reference and the GC would do the dirty work for you, – Debosmit Ray Mar 24 '16 at 05:43
  • @Debosmit It depends on the OP's use-case, which they haven't provided. Overwriting the reference isn't always an option and, frankly, the perceived inefficiency of iterating over the collection twice is almost always irrelevant in practice. If you need to retain the same list instance (because it's being referred to at other places in the code) overwriting the variable doesn't update those other places. – nbrooks Mar 24 '16 at 05:49
  • @nbrooks I completely overlooked the fact that there could be other references in other locations that would cause exceptions. Thank you so much for the note. I truly appreciate it. – Debosmit Ray Mar 24 '16 at 05:52
  • 1
    @Jonny I won't bother typing up an answer for this one, since the question's already been answered in a few places. There's a similar response [here](http://stackoverflow.com/a/3823430/803925). – nbrooks Mar 24 '16 at 06:06
  • Thanks @nbrooks, edited to add suggested explanation. – Willcodeforfun Mar 25 '16 at 23:40
  • Also thanks @JonnyHenly. – Willcodeforfun Mar 25 '16 at 23:40