简体   繁体   English

Java通过创建新实例清除列表

[英]Java clearing a list by creating a new instance

I was looking through Java code, and I came across this code: 我正在查看Java代码,发现以下代码:

list = new ArrayList();
uselist(list);

if (condition)
   list = new ArrayList();

What's the use of this, as opposed to simply using the clear() method of ArrayLists. 与简单地使用ArrayLists的clear()方法相反,它的用途是什么。

is using the new command to clear a list is ok and is it faster than clearing a list ? 使用new命令清除列表是否可以,并且比清除列表更快吗?

i am using java version 1.6 我正在使用Java 1.6版

No, they don't do the same thing. 不,他们做的不一样。 The method clear() clears an existing list - anything which still has a reference to the list and looks at it later will see that it's empty. 方法clear()清除现有列表-仍然引用该列表的任何内容,以后再查看该列表都会发现它是空的。

The approach with the new keyword, changes the value of the list variable but does nothing to the existing ArrayList object - so if anything else has a reference to the same object, they won't see any changes. 使用new关键字的方法会更改list变量的值,但不会对现有ArrayList 对象产生任何影响-因此,如果其他任何东西都引用了同一对象,则他们将看不到任何更改。

Do note that clearing and re-instantiating a list is not the same thing! 请注意,清除和重新实例化列表不是同一回事!

Consider this example: 考虑以下示例:

a = new ArrayList();
a.add("Hello")
b = a;
a = new ArrayList();   // a is now empty while b contains hello!

Versus

a = new ArrayList();
a.add("Hello")
b = a;
a.clear();            // Both a and b are now empty.

If the side-effects (shared references) are not an issue, then it is just two ways of clearing a list. 如果副作用(共享引用)不是问题,那么这只是清除列表的两种方法。 It should probably not be a performance issue unless this is called millions of times. 除非被称为数百万次,否则可能不应该是性能问题。

If the list is used elsewhere calling clear() might cause side effects. 如果该列表在其他地方使用,则调用clear()可能会导致副作用。 However, if that is not the case, I'd say that creating a new list instead of clearing the old one might be faster (however, probably for huge lists only, since ArrayList 's clear() just iterates over the elements and set's them as null ), but most likely it's just a matter of programming style. 但是,如果不是这种情况,我会说创建一个新列表而不是清除旧列表可能会更快(但是,可能仅适用于大型列表,因为ArrayListclear()只会遍历元素和集合的它们为null ),但很可能只是编程风格的问题。

Wether it is the same or not it depends on what uselist(...) does internally with the list. 是否相同,取决​​于uselist(...)对列表内部进行的操作。

For example, suppose you have the following code in uselist : 例如,假设您在uselist中具有以下代码:

public void uselist(List l) {
    this.mylist = l;
}

In that case, your code will create a new list and not touch this.mylist . 在这种情况下,您的代码将创建一个新列表,而不要触摸this.mylist。 If instead you call .clear() on it, you are clearing that same list. 相反,如果您对它调用.clear(),那么您正在清除该相同列表。

The difference can be fatal and hard to see. 这种差异可能是致命的,很难看到。 For example hibernate will flip out of you use list = new ArrayList() ; 例如, hibernate将使用list = new ArrayList()退出。 and then try to update the list in the db but it works just fine with clear() as hibernate then can see the connection. 然后尝试更新数据库中的列表,但是使用clear()休眠时可以正常工作,然后可以看到连接。

clear() // operates on your old object 

list = new ArrayList(); // list will be a new object the old will be GCed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM