In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you how to use Collection in Java. The content is simple and easy to understand. It is clearly organized. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn how to use Collection in Java.
1. Main systems and branches of sets
1.1 Collections and implementation classes
1. Overview: First, Collection is a top-level interface belonging to a single-column collection, represented as a group of objects. Its type is a reference data type, and the specific object creation is carried out in a polymorphic form.
2. The main common methods (E refers to generic types and can be any reference type):
3. Traversals of Collections (Iterator iterator, dedicated traversal of collections)
Overview:
2.List collection (List is indexed, so pay more attention to problems such as index crossing)
1. Overview: Ordered collections, allowing duplicate elements, precise user control over where each element is inserted, and quick access.
Features: Ordered (storage and extraction order consistent), and elements can be repeated.
2. Common methods:
3. List iterator
Usage: can traverse in any direction, and modify the list during iteration, and get the position of the iterator in the list, is the iterator specific to the list collection.
Common methods:
When using flashback traversal, it is necessary to pay attention to the forward order first; or directly through the reverse() method in the Collection tool class for flashback, and then output.
4. ConcurrentModificationException
The main reason: through the form of the inner class, when passing the hasNext() method, concurrent exceptions will occur when traversing the collection to judge and add, because list.add () will cause the actual modification set and the expected modification set to be unequal and trigger concurrent modification exceptions.
Because: when you use the add method of list, you add only the value of the actual modification set, and the expected modification set value does not increase, and the use of hasNext() requires both expected and actual judgment.
How to avoid this anomaly?
1. Add using the list iterator approach, because add does not result in actual and expected inequality.
2. Traversing through a for loop.
2.1 List implementation class
ArrayList (the underlying data structure belongs to array implementation, add and delete slow, query fast);
2. LinkedList (the underlying data structure belongs to double linked list, query slow, add and delete fast);
Common methods:
3. Set
Features: The collection is out of order (storage and retrieval order is inconsistent), and duplicate elements are not allowed, there is no index value, so ordinary for traversal cannot be used.
3.1 HashSet (implementation class of Set)
1. hash value
Overview: A hash value is a value of type int calculated by JDK from the address or string or number of an object.
Features: The hash value of the same object is the same, and by default the hash value of different objects is different.
2. hash table of data structure
3. How is the element uniqueness of a HashSet collection? (top priority)
4. LinkedHashSet collection (subclass of HashSet)
LinkedHashSet collection elements are ordered.
3.2 TreeSet collection (implementation class of Set)
Features: sorting can be done by natural sorting or specific comparators (mainly used for Comparable interfaces);
Natural Sort Comparable:
String class implementation of the class natural sorting Comparable interface, you can directly call to determine the size of strings, and when a class of objects need to be compared, it is necessary to implement the natural sorting Comparable interface, rewrite the compareTo method. (TreetSets can override natural sorting methods by anonymizing inner class methods).
4. 4.1 Differences between Arraylist and LinkedList
1. Whether thread safety is guaranteed: ArrayList and LinkedList are asynchronous, that is, thread safety is not guaranteed;
2. The underlying data structure: Arraylist uses the Object array at the bottom;LinkedList uses the bidirectional circular linked list data structure at the bottom;
3. Whether insertion and deletion are affected by element position: ① ArrayList is stored in arrays, so the time complexity of inserting and deleting elements is affected by element position. For example, when the add(E e) method is executed, ArrayList will append the specified element to the end of the list by default, and the time complexity in this case is O(1). But if you want to insert and delete elements at the specified position i (add(int index, E element)), the time complexity is O(n-i). Because when doing the above operation, the (n-i) elements after the ith and ith elements in the set are all subjected to the operation of moving backward/forward one bit. 2 LinkedList uses linked list storage, so the time complexity of inserting and deleting elements is not affected by the position of the elements, which is approximately O (1) and the array is approximately O (n).
4. Whether to support fast random access: LinkedList does not support efficient random element access, while ArrayList implements the RandmoAccess interface, so it has random access. Fast random access is to quickly retrieve an element object by its sequence number (corresponding to the get(int index) method).
5. Memory footprint: ArrayList space waste is mainly reflected in the reservation of a certain amount of capacity space at the end of the list, while LinkedList space cost is reflected in its each element needs to consume more space than ArrayList (because to store direct successors and direct predecessors and data).
4.2 ArrayList vs Vector
All methods of Vector classes are synchronized. A Vector object can be safely accessed by two threads, but code that accesses a Vector by one thread spends a lot of time synchronizing operations.
Arraylist is not synchronous, so it is recommended to use Arraylist when thread safety is not required.
Collection framework underlying data structure summary 1.Collection
1. List
Arraylist: Object array
Vector: Object array
LinkedList: Double-loop linked list
2. Set
HashSet (unordered, unique): Based on HashMap implementation, the bottom layer uses HashMap to store elements.
LinkedHashSet: LinkedHashSet inherits from HashSet, and its internals are implemented through LinkedHashMap. It's a bit similar to LinkedHashMap we talked about before, but it's based on Hashmap internally, but there's still a little difference.
TreeSet (ordered, unique): Red-black tree (self-balancing sorted binary tree).
The above is "Java Collection how to use" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to 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.