This is the 19th day of my participation in the August Wenwen Challenge.More challenges in August
Preface: This article is my 19th article about MySQL, the level is general, ability is limited. The article is relatively shallow, suitable for novice. This article briefly describes several ways to generate lists and the types of delivery.
Create a list
- The easiest way to do this is with a new List
List<Integer> list1 = new ArrayList<>();
list1.add(1);
Copy the code
- Arrays.aslist
List<String> list2 = Arrays.asList("111"."222");
Copy the code
Problem: The created list2 actually creates an ArrayList object internally. This object does not have add and remove methods.
- Using Lists. NewArrayList created
ArrayList<Integer> list3 = Lists.newArrayList(111.333);
Copy the code
- Build it with flow plus Collectors
List<Integer> list4 = Stream.of(122.333).collect(Collectors.toList());
Copy the code
Two. Time calculation
List<Integer> numList=new ArrayList<>();
for (int i = 0; i < 10000; i++) {
numList.add(i);
}
// 0. The default method is created using new List add
stopWatch.start("new List add");
List<Integer> list0 = new ArrayList<>();
numList.forEach(a->list0.add(a));
stopWatch.stop();
// 1. The default method uses new List addAll
stopWatch.start("new List addAll");
List<Integer> list1 = new ArrayList<>();
list1.addAll(numList);
stopWatch.stop();
// 2. The default method is created using new ArrayList
stopWatch.start("Arrays.asList");
List<Integer> list2 = Arrays.asList(numList.toArray(new Integer[numList.size()]));
stopWatch.stop();
/ / 3. Use Lists. NewArrayList
stopWatch.start("Lists.newArrayList");
ArrayList<Integer> list3 = Lists.newArrayList(numList);
stopWatch.stop();
// 4. Build with flow
stopWatch.start("stream Collectors.toList()");
List<Integer> list4 = numList.parallelStream().collect(Collectors.toList());
stopWatch.stop();
Copy the code
Here’s how much time we got
StopWatch 'list test': running time = 11035100 ns
---------------------------------------------
ns % Task name
---------------------------------------------
000935900 008% new List add
000049700 000% new List addAll
000056100 001% Arrays.asList
001935300 018% Lists.newArrayList
008058100 073% stream Collectors.toList()
Copy the code
We can see from the above times that the addAll method is the fastest. The list created by the stream is the slowest.
Three. Value transfer
StopWatch stopWatch = new StopWatch("Dog List test");
List<Dog> numList=new ArrayList<>();
for (int i = 0; i < 10; i++) {
numList.add(Dog.builder().age(i).build());
}
stopWatch.start("new List add");
// 1. The default method is created using new ArrayList
List<Dog> list1 = new ArrayList<>();
numList.forEach(a->list1.add(a));
stopWatch.stop();
stopWatch.start("new List addAll");
// 1. The default method is created using new ArrayList
List<Dog> list2 = new ArrayList<>();
list2.addAll(numList);
stopWatch.stop();
stopWatch.start("Lists.newArrayList");
/ / 3. Use Lists. NewArrayList
ArrayList<Dog> list3 = Lists.newArrayList(numList);
stopWatch.stop();
stopWatch.start("stream Collectors.toList()");
// 4. Build with flow
List<Dog> list4 = numList.parallelStream().collect(Collectors.toList());
stopWatch.stop();
System.out.println(stopWatch.prettyPrint());
numList.get(0).setAge(999);
System.out.println("list1="+list1);
System.out.println("list2="+list2);
System.out.println("list3="+list3);
System.out.println("list4="+list4);
Copy the code
The following is the result of the test. The values in the list are the basic data types. Making changes does not affect the list. But in the case of reference data types, all copy methods are positional because the internal objects are not newly created.
StopWatch 'Dog List Test': running time = 4142000 ns
---------------------------------------------
ns % Task name
---------------------------------------------
000184600 004% new List add
000005900 000% new List addAll
000815000 020% Lists.newArrayList
003136500 076% stream Collectors.toList()
list1=[Dog(age=999), Dog(age=1), Dog(age=2), Dog(age=3), Dog(age=4)]
list2=[Dog(age=999), Dog(age=1), Dog(age=2), Dog(age=3), Dog(age=4)]
list3=[Dog(age=999), Dog(age=1), Dog(age=2), Dog(age=3), Dog(age=4)]
list4=[Dog(age=999), Dog(age=1), Dog(age=2), Dog(age=3), Dog(age=4)]
Copy the code