Introduction and performance of java-9.4 List
In this section, we will discuss the two commonly used containers ArrayList and rule List in List.
1. Similarities
Both lists are sequential.
2. Differences
ArrayList is good at performing query operations, but the insert operation has poor performance.
The explain list is good at inserting elements in the middle, but the query performance is poor.
3. Demonstrate some common methods of List
package com.ray.ch09;import java.util.ArrayList;public class Test {public static void main(String[] args) {ArrayList
list = new ArrayList
();for (int i = 0; i < 10; i++) {list.add(i);}System.out.println(list.get(3));System.out.println(list.indexOf(5));list.remove(list.indexOf(3));for (Integer param : list) {System.out.println(param);}ArrayList
list2 = new ArrayList
();list2.add(5);list.retainAll(list2);for (Integer param : list) {System.out.println(param);}list2.clear();list.clear();}}
-------------------------------- Split line --------------------------------------
Below is the extended content
4. test performance
Because the above textbooks mentioned that ArrayList is good at querying and inserting list, we will do two experiments below.
(1) Performance Test of inserting Elements
Code:
Package com. ray. ch09; import java. util. arrayList; import java. util. optional list; public class Test {public static void main (String [] args) {long amount = 10000; // change the number of inserted elements at any time long startTime = System. currentTimeMillis (); ArrayList
ArrayList = new ArrayList
(); For (long I = 0; I <amount; I ++) {arrayList. add (Math. ceil (I/2);} long endTime = System. currentTimeMillis (); System. out. println (ArrayList: + (endTime-startTime); System. gc (); System. out. println (------------------------------); startTime = System. currentTimeMillis (); Comment list
Counter list = new counter list
(); For (long I = 0; I <amount; I ++) {amount list. add (Math. ceil (I/2);} endTime = System. currentTimeMillis (); System. out. println (endTime-startTime ));}}
The above code inserts an element in the middle of the list.
We use several levels to test:
Amount = 10000 output:
ArrayList: 16
------------------------------
Counter list: 0
Amount = 100000 output: (most of the time is the following result)
ArrayList: 31
------------------------------
Counter list: 15
Amount = 500000 output: (reverse)
ArrayList: 125
------------------------------
Counter list: 219
Amount = 5000000 output: (reverse)
ArrayList: 1953
------------------------------
Counter list: 2078
Amount = 9000000 output: (memory list exhausted)
ArrayList: 3375
------------------------------
Java. lang. OutOfMemoryError
To sum up, when the partition list has a small data size, the insertion performance is indeed better than that of ArrayList. However, there is a turning point in the middle. When the data reaches a certain level, the insertion performance of the tranquility list is lower than that of the ArrayList. When the number of partitions is larger, the memory usage of the consumer list throws an exception. The memory consumption of the replica list is higher than that of the ArrayList because it uses chained storage, the stored data is more than the ArrayList.
(2) view Elements
Code:
Package com. ray. ch09; import java. util. arrayList; import java. util. using list; import java. util. random; public class Test {public static void main (String [] args) {long amount = 1000000; // list size int times = 1000; // Number of ArrayList views
ArrayList = new ArrayList
(); For (long I = 0; I <amount; I ++) {arrayList. add (Math. ceil (I/2);} specify list
Counter list = new counter list
(); For (long I = 0; I <amount; I ++) {amount list. add (Math. ceil (I/2);} System. out. println (============= view test start =============); Random random = new Random (); long startTime = System. currentTimeMillis (); for (int I = 0; I <times; I ++) {arrayList. get (random. nextInt (times);} long endTime = System. currentTimeMillis (); System. out. println (ArrayList: + (endTime-startTime); startTime = System. currentTimeMillis (); for (int I = 0; I <times; I ++) {memory list. get (random. nextInt (times);} endTime = System. currentTimeMillis (); System. out. println (endTime-startTime ));}}
We first store 1000000 elements in two lists and then perform random queries.
Output when times = 1000:
============ View test start ==============
ArrayList: 0
Upload list: 16
Output when times = 10000:
============ View test start ==============
ArrayList: 0
Counter list: 969
Output when times = 100000:
============ View test start ==============
ArrayList: 0
Shortlist: Unable to wait so long
It turns out that the query performance of the sorted list is very poor, and the ArrayList is very good.
Summary: This chapter describes the role list and ArrayList, as well as a simple performance test for the two.