Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: The most efficient way to write pojo with ArrayList

What is the most correct and/or efficient way in terms of time & memory consumption to write simple pojo class containing ArrayList? (Regarding java mechanism for references, pass by ref', assignments etc.):

public class MyClass {

    //1. Do we need to initialize here?
    public List<String> mList = new ArrayList<>();

    //2. Do we need to declare D. Constructor?
    public MyClass() {}

    //3. Need to initialize the list here or just pass?
    public MyClass(List<String> list) {
        this.mList = list;
    }

    //4. Better way for copy/assignment?
    public void setMlist(List<String> list) {
        this.mList = list;
    }

    public List<String> getMList() {
        return this.mList;
    }

}
like image 499
michael Avatar asked Sep 11 '15 13:09

michael


2 Answers

  1. Do we need to initialize here?

No, initialize it only when you need it. Make sure to check for null if there is possibility of using it without being initialized.

  1. Do we need to declare D. Constructor?

If you do nothing in it, I don't really see the point of having it. Note that some people prefer to still declare it writing a comment in it and indicate that it should do nothing :

public MyClass(){
    //NOP
}

See NOP. This won't change anything related to memory usage. However logically, the default constructor should initialize the list instead of initializing it at the beginning. So we have two options, we pass one that already exists (with the parameterized constructor) or we use the default constructor and create an empty list.

  1. Need to initialize the list here or just pass?

Just pass, else what would be the point of receiving it as an argument ? If you initialize it and re-assign that would make no sense. You may want to check if the received one is null and initialize it otherwise.

  1. Better way for copy/assignment?

If really you want to make a copy, you might want to check Collections#copy. However, this is not the point of setter, what you have done here is correct.

like image 66
Jean-François Savard Avatar answered Nov 04 '22 20:11

Jean-François Savard


This is impossible to answer without knowing about your intentions. There's a surprisingly large number of design decisions you have to make, even when writing a "simple pojo class containing ArrayList". Here are 8 off the top of my head, but there are many, many more.

  • Do you want to make the field public or private? (probably private.)
  • If private, do you want to provide a get method?
  • Do you want to provide a set method, or do you want the field to be initialized once and for all in the constructor?
  • Should the argument to your constructor and/or set method only accept a List<String> or will you allow something more general, such as Collection<? extends CharSequence>?
  • Do you want people using your class to be able to modify mList? (This is different from reassigning mList.)
  • Do you want to write subclasses, or do you want the class to be final?
  • If you want to write subclasses, do you want to make any of the methods final?
  • Do you want to provide a constructor with no argument that initialises the ArrayList to a sensible default value?

The most subtle one of these questions is the 5th. Suppose somebody does this

List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c"));
myClass.setMList(list);

and then later does this

System.out.println(myClass.getMList());

They may expect to see [a, b, c], but this may not happen because it is possible to modify the internals of myClass in between. For example:

List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c"));
myClass.setMList(list);
list.remove(1);                         // Modifies the List stored by myClass
System.out.println(myClass.getMList()); // prints [a, c]

If you don't want this kind of thing to be possible you'll have to write a class that copies List objects in the constructor, setter and getter. This will have consequences for performance (but will be tiny for small lists).

There are no right or wrong answers. You need to think through who is using the class, why they need it, and weigh up all the relevant factors when answering all of the above questions.

like image 29
Paul Boddington Avatar answered Nov 04 '22 19:11

Paul Boddington