In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you the "java.util.Collection source code example analysis", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "java.util.Collection source code example analysis" this article.
Let's be clear about the following points:
Collection is an interface, which inherits the Iterable interface
Collection belongs to a collection of single-valued types, key subinterfaces List interface and Set interface
Java.util.List interface (orderly, not unique)
ArraryList
ArrayList is an array queue, equivalent to a dynamic array. Compared with the array in Java, its capacity can grow dynamically. It inherits from AbstractList and implements the interfaces of List, RandomAccess, Cloneable and java.io.Serializable. ArrayList is asynchronous, efficient but thread-safe, Collections.sychromiziedList (new ArraryList ()); you can make that ArrayList a thread-safe class
ArrayList is based on dynamic arrays. When adding or deleting, you need to copy and copy the array.
The default initialization capacity of ArrayList is 10, which is increased by half of the original capacity, that is, 1.5 times the original capacity each time the capacity is expanded.
The capacity is not reduced when the element is deleted. If you want to reduce the capacity, call trimToSize ().
It is not thread safe. It can store null values.
LinkedList
LinkedList is a two-way circular list queue
LinkedList is a bi-directional linked list inherited from AbstractSequentialList. It can also be operated as a stack, queue, or double-ended queue.
LinkedList implements the List interface and can queue it.
LinkedList implements the Deque interface, that is, LinkedList can be used as a double-ended queue.
LinkedList implements the Cloneable interface, which overrides the function clone () and can be cloned.
LinkedList implements the java.io.Serializable interface, which means that LinkedList supports serialization and can be transmitted through serialization.
LinkedList is out of sync.
Vector
At the bottom is the array, which is now less used and replaced by ArrayList for two reasons:
All Vector methods are synchronized, resulting in a performance penalty.
When the initial length of Vector is 10, it grows at a rate of 100% over length, consuming more memory than ArrayList.
Reference: https://www.zhihu.com/question/31948523/answer/113357347
In general: query multi-use ArrayList, add and delete multi-use LinkedList.
The increase or deletion of ArrayList is not absolute (in the case of a large number, there will be exceptions):
If the addition element is always using add () (add to the end), and the capacity is not expanded)
Always delete the element at the end is also ArrayList to be fast [do not need to copy the moving location]
As for if the deletion is the middle position, or ArrayList should be fast!
But generally speaking: add and delete more or use LinkedList, because the above situation is extreme ~
Java.util.Set interface (unordered, unique)
| |-- SortedSet API-- TreeSet implementation class |
Set interface-|-HashSet implementation class
|-- LinkedHashSet implementation class
HashSet
The underlying hashSet implementation is based on hashMap, as shown in the figure
The add () method in hashSet puts object E into the location of the key in hashMap, and the location of value stores a fixed Object, as shown in the figure:
HashSet is unordered and unique, when the element is a custom object, the hashCode of the two is different, it is considered to be different elements and is allowed to be put into the HashSet, but this does not accord with the actual production significance, so it is necessary to make its judgment location equal, it is necessary to rewrite hashCode (). However, if you rewrite HashCode (), there will be a bug with double code, so you need to rewrite equals () to solve the problem.
When you add an element, if the key (and the corresponding elements of the Set collection) are equal, then modify the value value. In the Set collection, the value value is just an Object object (which is useless to the Set itself).
That is to say, if the same element is added to the Set collection, it is not inserted at all (only a useless value value has been modified). As can be seen from the source code (HashMap), both the = = and equals () methods are used. The specific process is as follows
Whether the hsah of the new element is equal to the hash of the old element, if not, the element is different, if equal, then the second comparison is made.
The new element is compared with the old element with "=". If it is equal, it is the same element, and if it is different, the third part is compared.
The new element and the old element use the equals () method to determine whether they are equal. If not, they are not the same element. If they are equal, the result is: the two elements are not the same object, and the program wants to treat them as the same object, so the equals method is overridden.
TreeSet
Just as HashSet is based on HashMap implementation, TreeSet is also based on TreeMap implementation.
TreeSet is the only implementation class for the SortedSet interface, and TreeSet ensures that collection elements are in a sorted state. TreeSet supports two sorting methods, natural sorting and custom sorting, in which natural sorting is the default sorting method. Objects of the same class should be added to the TreeSet.
LinkedHashSet
LinkedHashSet is an "extended version" of HashSet. HashSet does not care about the order, except that LinkedHashSet maintains the "insert order". HashSet internally uses HashMap objects to store its elements, while LinkedHashSet internally uses LinkedHashMap objects to store and process its elements
The above is all the contents of this article "sample Analysis of java.util.Collection Source Code". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to 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.
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.