开发者

Setting a list equal to list using equal sign or copy constructor?

This is a simple question but if I do

List<Object> list = getObjectsFromDatabase();

This would not be the correct way to handle this?

But this would?

List<Object> firstList = getObjectsFromDatabase();

List<Object> list = new ArrayList<Object>(firstList);

Or if I had a c开发者_JS百科lass

public class ReportDisplayModel<T> {

    public ReportDisplayModel(List<T> data) {
        this.data = data;
    }
    public List<T> data;
}

And I wanted to set the data in this model I would use the constructor?

ReportDisplayModel<Object> model = new ReportDisplayModel<Object>(getData());

Instead of

ReportDisplayModel<Object> model = new ReportDisplayModel<Object>();

model.data = getData();

Just need a clarification. Thanks.


It depends entirely on what getData() returns.

  • usually it is made to return Collections.unmodifiableList(result) so that clients can't modify the result.

  • if this result is not used anywhere else, and modifications to it doesn't mess with anything, it is fine to use the result as-is

It is rarely needed to use the copy constructor - use it when you are sure that modifying the data will impact some other component.


Regarding

List<Object> list = getObjectsFromDatabase();

           vs

List<Object> firstList = getObjectsFromDatabase();
List<Object> list = new ArrayList<Object>(firstList);

either approach is fine. Depends on if you want list to refer to the list returned by getObjectsFromDatabase() or if you want it to refer to a copy of it.

If simply want to, say, print the database objects, the first approach is fine.

If you want to, say, filter out half of the database objects (i.e., remove objects from the list), and you can't say for sure that getObjectsFromDatabase() returns a mutable list, then you'll have to go with the second approach.


Regarding

ReportDisplayModel<Object> model = new ReportDisplayModel<Object>(getData());

           vs

ReportDisplayModel<Object> model = new ReportDisplayModel<Object>();
model.data = getData();

I'd prefer the first method. Simply because I wouldn't want to worry about null pointer exceptions etc if I accidentally do something like

ReportDisplayModel<Object> model = new ReportDisplayModel<Object>();
model.printData();
model.data = getData();


I don't quite get your question, but I'll give it a try.

The main difference is that using the copy constructor creates a new independent copy of the list, i.e.

List<Object> firstList = getObjectsFromDatabase(); // firstList is the list returned by the database

List<Object> list = new ArrayList<Object>(firstList); //list is an independent copy of firstList

Now if you change firstList the list returned by getObjectsFromDatabase() would be changed as well (or would throw an exception if changes are not supported). On the other hand list could freely be changed without the original list being affected.


Avoid using the equal sign, because it breaks encapsulation (bad practice). Go for the copy constructor (best practice).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜