• Post Reply Bookmark Topic Watch Topic
  • New Topic

Efficient way to handle lists.  RSS feed

 
kartik krishnan
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have List<String> object that i wish to populate with some data.

I can do it two ways.

public static void main() {
List<String> list = new ArrayList<String>();
populateList(list);
}

where

private static void populateList(List<String> list) {
list.add(....);
list.add(....);
....
....
....
}



Is the above option preferable to this option;

public static void main() {
List<String> list = populateList();
}



where

private static void populateList() {
List<String> list = new ArrayList<String>();
list.add(....);
list.add(....);
list.add(...);
return list;
}



Which option is better in terms of performance, efficiency? Please help
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
both are exactly the same performance-wise,
which you use is more a personal preference, or style issue,
and depends on how you may or may not reuse the code

(and, correct execution is priority #1, execution performance is at least #3)
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'd really prefer that one, because:
  • It does what the name implies - it just populates an existing list. A better name for your second method would be "createPopulatedList".
  • You can use it to populate different List implementations (e.g. LinkedList as well) without having to create multiple "populateList" methods.
  • For performance they should be equal as Bill said.
     
    Campbell Ritchie
    Marshal
    Posts: 56546
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    . . . and has nobody noticed the 2nd suggestion won't compile because it is trying to return something from a void method?
     
    Anshul Malpani
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    We have seen that but we are mainly concentrating on Question asked. I think the second option is better. Bcz Local variable can be garbage colleted. But performance wise first one is better Bcz Variable initilized on the creation time will save the time.
     
    Joanne Neal
    Rancher
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Anshul Malpani:
    I think the second option is better. Bcz Local variable can be garbage colleted.


    Only objects are garbage collected - not variables. As a copy of the reference to the ArrayList is being returned and assigned to another variable in the main method the ArrayList will not be garbage collected when the method returns - there would be no point in creating it otherwise.

    Originally posted by Anshul Malpani:
    But performance wise first one is better Bcz Variable initilized on the creation time will save the time.


    If you were interested in saving nanoseconds this might be true. Of course you also have to take into account time spent setting up the stack to pass a parameter to the method.
    I think however you would have to be doing this millions of times before you could detect any measureable difference. As Bill said, get it working first - there will probably be many other places in your program where performanace could be improved before you have to consider minuscule savings like these.
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!