0

If I have something like:

ArrayList<Data> mList = new ArrayList();

...

public void setList(ArrayList<Data> list) {
    mList = list;
}


public void updateList(ArrayList<Data> list) {
    mList.clear();
    mList.addAll(list);
}

Which is more efficient, setList() or updateList() and why? What is the best practice?

EDIT:

Not to be confused with this question.

Community
  • 1
  • 1
young_souvlaki
  • 1,886
  • 4
  • 24
  • 28

3 Answers3

11

There is an enormous conceptional difference between the two.

Your setList() takes a reference to a list; and changes your mList ... to point to that reference.

Whereas your updateList() copies all the references from the incoming list into your existing mList object.

Meaning: setList() does require less CPU operations compared to updateList() ... but the key thing is: now mList can be changed from outside of your class. Like in:

List someList = ...
yourClass.setList(someList);
someList.clear();

will clear "your" mList as well; but

List someList = ...
yourClass.updateList(someList);
someList.clear();

will not affect your "mList".

That's the main difference you should be aware of.

Performance is actually not the real issue to be concerned about here!

In general: do not worry too much about performance. Because: you will not get it right anyway. For example, there is the JIT compiler turning naive assumptions upside down.

Long story short: focus on semantics of operations; don't restrict yourself to "performance" aspects.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Since it is an android question, there is probably an adapter involved somewhere. So updateList will be better of usage. – Murat Karagöz May 31 '16 at 15:20
  • You can also add your own immutable/unmodifiable wrapper to prevent after crud operations. – HRgiger May 31 '16 at 15:21
  • @HRgiger Where one has to be really careful. Just creating a wrapper using the static methods in Collections ... does not protect your "wrapped" collection when somebody modifies that "base collection" you created a wrapper for. – GhostCat Jun 01 '16 at 06:49
2

setList() is more efficient in CPU time in there there is less work being done. However updateList() is arguably more efficient in developer time as it is less likely to results in bugs down the road.

Before efficiency you want to consider what is being done. There are two different lists that may be referenced (and possibly changed) in other locations.

Example:

Using setList() allows outside code to modify your code in unexpected ways.

List<Data> someData = Arrays.asList(new Data("a"), new Data("b");
setList(someData); // mList contains "a" & "b"
someData.clear();  // mList is now cleared

However in contrast updateList() protects your from unexpected outside changes

List<Data> someData = Arrays.asList(new Data("a"), new Data("b");
updateList(someData); // mList contains "a" & "b"
someData.clear();  // mList still contains "a" & "b"

When passing lists to or from an object it is typically good practice to copy the values. It is less efficient in CPU time, but is less likely to cause bugs in the future, and therefore more efficient in developer and maintenance time.

cyroxis
  • 3,661
  • 22
  • 37
-1

setList is more efficient becouse u pass only a reference. Clear and add iterates over the list.

But u have to take care and know the difference between passing references or resetting lists and use what is good for ur solution.

balint.steinbach
  • 278
  • 3
  • 16