Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ArrayList in java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about how to use ArrayList in java. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Public void trimToSize ()

Presumably many people have not used this method or even may not know that ArrayList has this method, so what does this method do? From the introduction of this method, we can know that the method is to minimize the internal array, how to minimize it? That is, if the length of the array is 100 and the actual use (with data) is only 10, then using this method will reapply for an array of length 10, then copy the data from the original array, and then assign the new array reference of length 10 to the ArrayList object as the internal array. The source code is as follows:

If you are not a computer major or your foundation is not solid enough, you may ask: what are the benefits of this? Well, I can tell you here that the advantage of this is to save space, because if the internal array is very large but only a small part of it is actually used, the extra part of the array space does not store anything, but it still takes up a certain amount of space, and this small optimization is necessary in some memory-strapped scenarios. At the same time, according to the source code, it can be easily concluded that the time progressive complexity of this method is O (n), where n is the same as the size of the current ArrayList.

If you think the text is not intuitive enough, give you a piece of code, and then you can use jconsole to observe the changes in heap memory usage when the program is running.

If you don't know how to use jconsole and want to see the result, I have a screenshot that works. You can take a look at it. The screenshot is as follows:

You can see that when the program executes to build the ArrayList object, the heap memory occupies more than 1G, and when the trimToSize method is called, the heap memory drops to the initial state, so when ArrayList expands to a larger state, a lot of elements are deleted or a large internal array is built when initializing ArrayList (using ArrayList's constructor). Calling trimToSize will significantly improve the heap memory footprint. However, because the time complexity of this method is O (n), where n is related to the size of ArrayList, this method is not efficient when the size is relatively large, so it is not recommended to call this method frequently when the size is large and the memory is sufficient, or when the utilization rate of the internal array of ArrayList is relatively high (the length of the internal array of size/, the higher the value indicates the higher the utilization of the array).

Constructor Optimization of ArrayList

When you see the subtitle above, some people may ask, can ArrayList's constructor still be optimized? Yes, it does. As mentioned in the previous article, there are three constructors for ArrayList, one of which receives an int type value, and this constructor is the focus of our optimization.

According to the knowledge of the previous section, it can be concluded that the add method of ArrayList is usually efficient and can be completed in O (1). However, if the length of the internal array of ArrayList is not long enough and needs to be expanded, it is necessary to call the grow method to expand the capacity, and the time progressive complexity of this method is O (n), where n is related to the current size. Therefore, this method is relatively inefficient (in fact, the time complexity of O (n) is usually more efficient in the algorithm world), so how to reduce the number of calls to this method has become a key problem to optimize the performance of ArrayList.

To reduce the number of calls to the grow method, we first need to know when the grow method will be called, and as we said earlier, this method will be called when we need to expand, that is, when the array maintained internally by the current ArrayList is not long enough, so if the array maintained internally by our ArrayList has been long enough, don't we need to call this method? In fact, this idea is feasible, because we can control the initial size of the array maintained internally by ArrayList through the constructor of ArrayList, and if we know exactly how much data we are going to put into ArrayList, then we can specify the initial size of the array maintained internally by ArrayList directly through the constructor of ArrayList, even if we are not sure how much data we are going to put in the ArrayList object we create. We can also estimate a relatively large value and then use that value to build the ArrayList to minimize calls to the grow method.

The above is how to use ArrayList in java. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report