我们在编码时经常需要将一些元素添加到一个List中,此时我们一般有两种选择:Collections.addAll()或者是ArrayList.addAll()。在需添加元素比较少的情况下,并在List的size在万级以上时,一般建议Collections.addAll(),但当List的size较小时,两种方法没有什么区别,甚至ArrayList.addAll()更好。
当我们将一个数组添加到一个List中时,Collections.addAll()和ArrayList.addAll()没有什么性能差异。
但当我们将一个List添加到一个List中时,建议使用ArrayList.addAll()。
添加数组和列表,要比添加元素快。
HashMap和HashSet有类似用法。
示例代码如下(为了尽量不让gc对实验结论影响,特意将jvm设置成了16G):
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class CollectionsTest {
public static void main(String[] args) {
List list1 = new ArrayList() {{add(0); add(-1);}};
List list2 = new ArrayList(Arrays.asList(2, 4, -9));
list1.addAll(list2);
System.out.println(list1);
list2.set(0, 100000);
System.out.println(list1);//深复制
System.out.println();
//功能性展示
Collections.addAll(list2, 34, 67, 78);
System.out.println(list2);
list2.addAll(Arrays.asList(34, 67, 78));
System.out.println(list2);
System.out.println();
//性能展示
System.out.println("添加元素---------------------------\n");
for(int size = 1000; size < 100000000; size *= 10) {
long time = System.nanoTime();
List list3 = new ArrayList();
for(int i = 0; i < size; i++) {
list3.addAll(Arrays.asList(1, 45, 34, 456, 1212, -6, 43, 55));
}
System.out.println("ArrayList.addAll()@size = " + size + "\ntime:" + (System.nanoTime() - time));
time = System.nanoTime();
List list4 = new ArrayList();
for(int i = 0; i < size; i++) {
Collections.addAll(list4, 1, 45, 34, 456, 1212, -6, 43, 55);
}
System.out.println("Collections.addAll()@size = " + size + "\ntime:" + (System.nanoTime() - time) + "\n");
}
System.gc();
System.out.println("添加数组---------------------------\n");
Integer[] arr = {1, 45, 34, 456, 1212, -6, 43, 55};
for(int size = 1000; size < 100000000; size *= 10) {
long time = System.nanoTime();
List list3 = new ArrayList();
for(int i = 0; i < size; i++) {
list3.addAll(Arrays.asList(arr));
}
System.out.println("ArrayList.addAll()@size = " + size + "\ntime:" + (System.nanoTime() - time));
time = System.nanoTime();
List list4 = new ArrayList();
for(int i = 0; i < size; i++) {
Collections.addAll(list4, arr);
}
System.out.println("Collections.addAll()@size = " + size + "\ntime:" + (System.nanoTime() - time) + "\n");
}
System.gc();
System.out.println("添加列表---------------------------\n");
ArrayList list = new ArrayList(Arrays.asList(1, 45, 34, 456, 1212, -6, 43, 55));
for(int size = 1000; size < 100000000; size *= 10) {
long time = System.nanoTime();
List list3 = new ArrayList();
for(int i = 0; i < size; i++) {
list3.addAll(list);
}
System.out.println("ArrayList.addAll()@size = " + size + "\ntime:" + (System.nanoTime() - time));
time = System.nanoTime();
List list4 = new ArrayList();
for(int i = 0; i < size; i++) {
Collections.addAll(list4, list.toArray(new Integer[0]));
}
System.out.println("Collections.addAll()@size = " + size + "\ntime:" + (System.nanoTime() - time) + "\n");
}
}
}
运行截图展示:
HashMap和HashSet的类似用法
示例代码:
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
public class CollectionsTest {
public static void main(String[] args) {
HashSet set1 = new HashSet();
HashSet set2 = new HashSet() {{add(5);}};
Collections.addAll(set1, 2, 4, 6, 7);
System.out.println(set1);
set1.addAll(set2);
System.out.println(set1);
HashMap map1 = new HashMap(){{put(1, 5);}};
HashMap map2 = new HashMap(){{put(3, 4);}};
map1.putAll(map2);
System.out.println(map1);
}
}
运行截图: