[JAVA] Three points to use ArrayList efficiently

After all, the implementation class of List that is often used is ArrayList. Here are three points to keep in mind as you should use it efficiently if you use it with all your might.

Basic idea

ArrayList is a class for realizing variable length arrays. It is implemented internally using arrays, but regular arrays are fixed length. So, how to realize variable length is to create a new array of larger size and copy the data to it when the array prepared at the beginning is insufficient. Also, when inserting data in the middle, all subsequent elements are copied and shifted. As the size of the array increases, the process of regenerating and copying such an array becomes very slow, so it is important to prevent such processing from occurring in order to use ArrayList efficiently.

3 points

Initial size setting

You can specify the initial size in the ArrayList constructor. Since the array is generated first with the specified initial size, the array will not be regenerated until it exceeds that size.

For example, if the number of elements is 100 at most, ArrayList is generated with an initial capacity of 100 by doing the following.

List<String> list = new ArrayList<String>(100);

ensureCapacity ensureCapacity is a method to ensure the size of ArrayList. It is used when the initial size specified in the constructor is likely to be greatly exceeded. For example, when adding 1000 elements to an ArrayList initialized with an initial size of 100, if you add 1000 times normally, regeneration may occur several times in the middle, but with ensureCapacity, the size is expanded to 1000 in advance. If you do, you only have to do it once.

trimToSize trimToSize is a method that trims the size of ArrayList by the current number of elements. For example, if the ArrayList initialized with the initial size 1000 contains only 100 elements, the area for 900 elements will be wasted, so the area will be released by truncating it. Of course, the size will be smaller, so if you try to add an element after that, you will have to reallocate the array. Therefore, it is recommended to use it when the element is not added in the subsequent processing.

Recommended Posts

Three points to use ArrayList efficiently
[Java] How to use List [ArrayList]
[Easy-to-understand explanation! ] How to use ArrayList [Java]
How to use Segmented Control and points to note
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
PATH to use docker-slim
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
[Java] How to use Map
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
[Rails] How to use enum
How to use java Optional
How to use JUnit (beginner)
How to use Ruby return
[Rails] How to use enum
How to use @Builder (Lombok)
[Swift] How to use UserDefaults
How to use java class
How to use Swift UIScrollView
How to use Big Decimal
[Java] How to use Optional ②
[Java] How to use removeAll ()
How to use String [] args
[Java] How to use string.format
How to use rails join
How to use Java Map
[java] Reasons to use static
Rbenv command to use Ruby
Ruby: How to use cookies
How to use dependent :: destroy
How to use Eclipse Debug_Shell
How to use Apache POI
[Rails] How to use validation
How to use Java variables
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
How to use GC Viewer
[Java] Convert ArrayList to array
[Java] How to use Optional ①