清除ArrayList的两个选项中哪一个更好更快,为什么?
list.clear()
Run Code Online (Sandbox Code Playgroud)
要么
list = new ArrayList<Integer>();
Run Code Online (Sandbox Code Playgroud)
碰巧我必须在随机的时间清除我的ArrayList中的所有条目,我无法知道将来会有多少新条目,可能有0或1000.哪种方法更快更好,为什么?
我有一个Map作为语法Map<String, String> testMap = new HashMap<String, String>();
.在此地图中可以有1000个数据.
当我的应用程序需要新的数据列表时,我必须清除Map.但是当我看到Map.clear()的代码为
/**
* Removes all of the mappings from this map.
* The map will be empty after this call returns.
*/
public void clear() {
modCount++;
Entry[] tab = table;
for (int i = 0; i < tab.length; i++)
tab[i] = null;
size = 0;
}
Run Code Online (Sandbox Code Playgroud)
我意识到clear方法循环n次(其中n是Map中的数据).所以我认为可以有一种方法来重新定义Map,因为testMap = new HashMap<String, String>();
之前使用的Map将是Garbage收集的.
但我不确定这将是一个好方法.我正在研究移动应用程序.
你能指导我吗?
使用Java(1.6)是否最好在List上调用clear()方法或者只是重新实例化引用?
我有一个ArrayList,其中填充了未知数量的对象并定期"刷新" - 处理对象并清除列表.刷新后,List再次填满.冲洗是在随机时间发生的.列表中的数字可能很小(对象的10个)或大的(数百万个对象).
那么"flush"调用clear()或new ArrayList()会更好吗?
是否值得担心这类问题,还是我应该让VM担心呢?我怎样才能看看Java的内存占用为我自己做这类事情?
任何帮助非常感谢.
我有一个arraylist<interface>
对象被添加到for循环中的列表中.每次调用方法时,我都希望这个arraylist为空.
这是代码:
我想在这里清空的数组是建议的Phrases.
public List<Interface> returnSuggestedList(String prefix) {
String tempPrefix = prefix;
// suggestedPhrases = null;
//suggestedPhrases = new ArrayList<Interface>();
//Vector<String> list = new Vector<String>();
//List<Interface> interfaceList = new ArrayList<Interface>();
Collections.sort(wordsList);
System.out.println("Sorted Vector contains : " + wordsList);
int i = 0;
//List<String> selected = new ArrayList<String>();
for(String w:wordsList){
System.out.println(w);
if(w.startsWith(prefix.toLowerCase())) { // or .contains(), depending on
//selected.add(w); // what you want exactly
Item itemInt = new Item(w);
suggestedPhrases.add(itemInt);
}
}
Run Code Online (Sandbox Code Playgroud) 我在一个分析器下运行我的应用程序,内存使用量比我预期的要高得多,其中对象在不再需要后仍然存在.它们中的大多数都在列表对象已脱离上下文的列表中.
是否需要更长的垃圾收集器来释放位于列表中的对象,即使列表本身不再被引用?如果是这样的话,如果我在列表中脱离上下文之前调用清单(),它们会更快地释放吗?
谢谢 - 戴夫
我尝试将外部非线程安全库集成到我的Web项目中; 我发现为每个客户端线程创建此对象的实例太昂贵了.
因此,我想创建一个具有以下属性的对象池.
我该怎么做?如果有一个有效的例子,我将不胜感激.
java concurrency thread-safety java.util.concurrent threadpool
java ×6
arraylist ×2
concurrency ×2
list ×2
actor ×1
android ×1
clear ×1
collections ×1
latency ×1
performance ×1
threadpool ×1