In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what are the ways in which java data elements are stored in memory". In daily operations, I believe that many people have doubts about the way java data elements are stored in memory. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about how java data elements are stored in memory. Next, please follow the editor to study!
There are two main storage methods:
1. Sequential storage, Random Access (Direct Access)
In this way, adjacent data elements are stored in adjacent memory addresses, and the whole block of memory addresses are contiguous. The memory address can be calculated directly according to the location of the element and read directly. The average time complexity of reading a specific location element is O (1). Normally, only collections based on array implementations have this feature. ArrayList is the representative of Java.
2. Chain storage, Sequential Access
In this way, each data element is not required to be in an adjacent location in memory, and each data element contains the memory address of its next element. The memory address cannot be calculated directly based on the location of the elements, but can only be read sequentially. The average time complexity of reading a specific location element is O (n). It is mainly represented by linked lists. LinkedList is the representative of Java.
Practice
In the Java data collection framework, a RandomAccess interface is provided, which has no methods, just a tag. It is usually used by the implementation of the List interface to mark whether the implementation of the List supports Random Access.
A data set implements this interface, which means that it supports Random Access, and the average time complexity of reading elements by location is O (1). Like ArrayList. If the interface is not implemented, Random Access is not supported. Like LinkedList.
So it seems that JDK developers are also aware of this problem, so the recommended practice is that if you want to traverse a List, first determine whether or not to support RandomAccess, that is, list instanceof RandomAccess.
If (list instanceof RandomAccess) {/ / traverses using traditional for loops. } else {/ / use Iterator or foreach. } case analysis
For example: Collections.reverse (List list) for this method, the specific implementation is as follows:
Public static void reverse (List list) {int size = list.size (); if (size)
< REVERSE_THRESHOLD || list instanceof RandomAccess) { for (int i=0, mid=size>> 1, jroomsizeMui 1; I
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.