【发布时间】:2011-03-21 14:26:54
【问题描述】:
Java API docs say以下关于Collections.addAll
此便捷方法的行为与 c.addAll(Arrays.asList(elements)) 的行为相同,但此方法在大多数实现下可能运行得更快。
所以如果我理解正确的话,a) 比 b) 慢:
一)
Collection<Integer> col = new ArrayList<Integer>();
col.addAll(Arrays.asList(1, 2, 3, 4, 5));
b)
Collection<Integer> col = new ArrayList<Integer>();
// Collections.addAll(col, Arrays.asList(1, 2, 3, 4, 5)); <-- won't compile
Collections.addAll(col, 1, 2, 3, 4, 5);
谁能给我解释一下,为什么会这样?
编辑: 更正的代码示例。谢谢polygenelubricants
【问题讨论】:
标签: java collections performance