The bottom layer of java basic collection introduces the default initial capacity, loading factor, and expansion increment of ArrayList, Vector, HashMap, HashTable, and HashSet

​ Here we will discuss these commonly used default initial capacities and reasons for expansion: When the underlying implementation involves expansion, the container may reallocate a larger continuous memory (if it is discrete allocation, there is no need to reallocate, discrete allocation allocates memory dynamically when inserting new elements), the original memory of the container must be All data is copied to new memory, which undoubtedly greatly reduces efficiency. The coefficient of the loading factor is less than or equal to 1, which means that when the number of elements exceeds the capacity length * the coefficient of the loading factor, the capacity will be expanded. In addition, the expansion also has a default multiple, and the expansion conditions of different containers are different. List Elements are ordered and repeatable The default initial capacity of ArrayList and Vector is 10 Vector: thread safe, but slow The underlying data structure is an array structure The loading factor is 1: that is, when the number of elements exceeds the capacity length, the capacity will be expanded Expansion increment: 1 times the original capacity For example, the capacity of Vector is 10, and after one expansion, the capacity is 20 ArrayList: thread-unsafe, fast…

Java Collection (Collection, List (ArrayList, LinkedList, Vector))

Memory map of arrays storing address valuesArrays and collections Difference Arrays can store both basic data types and reference data types. Basic data types store values, and reference data types store address values. Collections can only store reference data types (objects). Basic data types can also be stored in collections, but they will be automatically boxed into objects when stored. The length of the array is fixed and cannot grow automatically. The length of the collection is variable and can grow according to the addition of elements. When are arrays and sets used? If the number of elements is fixed, it is recommended to use an arrayIf the number of elements is not fixed, it is recommended to use a set Set system diagram Collection basic functionsboolean add(E e) If yes The List collection always returns true, because the List collection can store repeated elements If it is a Set collection, when it stores repeated elements, it will return false The parent class of ArrayList overrides the toString method, so when printing the reference of the object, the output result is not the result of toString in the Object class. boolean remove(Object o) Delete the specified elementvoid clear() Empty the…

Java collection framework | ArrayList, Vector, LinkedList (1)

theme: channing-cyan This is the 7th day of my participation in the August article update challenge,View event details:August article update challenge ArrayList, Vector, and LinkedList are three common collection classes – they all implement the List interface (the List interface inherits the Collection interface) – so what are the differences between the three? xff1f; 1. Comparison between ArrayList and Vector Why should we compare ArrayList and V first? ector What’s the difference? The most important thing is that they are both ordered sets. That is, the positions where the elements in these two sets are stored are all ordered. They are different from the array itself. Both are dynamic – when we use both – we can retrieve an element by position index – and both allow the stored data to be repeated. So what is the main difference between the two? In terms of synchronization Vector is thread-safe& #xff0c; The so-called thread safety means that when multiple threads access shared resources, atomicity, visibility, and order can be guaranteed at the same time. The implementation of Vector’s thread safety is to use the synchronized keyword. Regarding the principle of this keyword, I wrote in the article “Don’t panic when meeting…

Comparison and summary of ArrayList, Vector, LinkedList, HashMap, HashTable, and HashSet in Java

Comparison and summary of ArrayList, Vector, LinkedList, HashMap, HashTable, and HashSet in Java

1. The parent class of all collections is the Collection interface 2.Set List Map difference A In the Set, the order of elements cannot be added, so the elements in the Set cannot be repeated B In the List, there is an index Number,is similar to an array,the elements in it can be repeated,the order of addition can be remembered C In Map:each item has a key value Pair composition (key, value) 3.iteratorInterface(Iterator,Traverser): This interface is also Java Members of the collection framework,This interface is mainly used for traversal(iterative access)CollectionElements MainlyUse three methods:   A boolean hasNext():Returns true if the iterated collection elements have not been traversed yet, B Object next():returns the next element in the collection element,Cursor.                                                                                               The element returned by the next method. 4. Differences in the use of Vector and ArrayList – All in the Vector class The methods are all thread synchronized – it will be safe for multiple…

Java Collection (Collection, List (ArrayList, LinkedList, Vector))

The memory map of the address value stored in the arrayArray and The difference between collections Arrays can store both basic data types and reference data types. Basic data types store values, and reference data types store address values. Collections can only store reference data types (objects). Basic data types can also be stored in collections, but they will be automatically boxed into objects when stored. The length of the array is fixed and cannot grow automatically. The length of the collection is variable and can grow according to the addition of elements. When are arrays and sets used? If the number of elements is fixed, it is recommended to use an arrayIf the number of elements is not fixed, it is recommended to use a collection Collection system diagram Collection basic functionsboolean add(E e) If it is a List collection, it will always return true, because the List collection can store repeated elements If it is a Set collection, when it stores repeated elements, it will return false The parent class of ArrayList overrides the toString method, so when printing the reference of the object, the output result is not the result of toString in the Object class. boolean…

STL basic container: is string, vector, list, deque, set, map_string[] ordered?_cy941228’s blog

class=”markdown_views prism-atom-one-dark”> Basic containers in STL are: string, vector, list, deque, set, map set and map are unordered storage elements, which can only be accessed through the interface it provides set: collection, used to judge whether an element is in a group, less used map: mapping, equivalent to a dictionary, mapping one value to another value, if you want to create a dictionary, use it’s alright string, vector, list, deque, set are ordered containers 1.string string is the implementation of basic_string, and it is stored continuously in memory. In order to improve efficiency, there will be reserved memory, such as string s= “abcd”, then the space used by s may be 255, when string is inserted into s again When adding content, memory will not be allocated again. It will not apply for memory again until the content is > 255, thus improving its performance. When the content is > 255, string will first allocate a new memory, and then copy the content over , and then copy the previous content. The operation on string, if it is added to the end, generally does not need to allocate memory, so the performance is the fastest; If it is an operation…

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: 34331943@QQ.com

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索