List<String> list1 = Lists.newArrayList(); List<String> list2 =new ArrayList<>();

foreword

List list1 = Lists.newArrayList();
List list2 =new ArrayList<>();
Which of these two is more efficient, in the case of high concurrent 1 million requests

Code

In the case of high concurrency, List list2 =new ArrayList<>(); This will be more efficient.
Because:

  1. Lists.newArrayList() will create a new ArrayList instance every time, while new ArrayList<>() will only be instantiated when it is used for the first time, and the instance will be reused later, so the performance is better.
  2. Lists.newArrayList() uses Guava's ImmutableList, and ArrayList is mutable. Mutable collections perform better under high concurrency because no copy operation is required.
  3. ArrayList is a native collection of JDK, and its performance is generally better than that of third-party libraries.
    So in general, in high concurrency scenarios, using JDK's native ArrayList instead of Guava's ImmutableList can achieve better performance.
    The method of creating a new instance uses new ArrayList<>() instead of Lists.newArrayList(), which can increase the chance of instance reuse and obtain better performance.
    The test data also proves this conclusion:


@Test
public void testConcurrentArrayList(

Guess you like

Origin blog.csdn.net/qq_32370913/article/details/130505231