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

Summary of Java Common knowledge points (⑦)-- Collection Framework

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

one。 The difference between Collection and Collections

Collection is the top-level interface (interface) in the collection inheritance structure, which is a subclass of Iterable.

Collections is a utility class that provides powerful methods for manipulating collections, including a variety of static polymorphic methods about collection operations. This class cannot be instantiated (its constructor is private, and the methods in this class are static and can be called directly)

two。 Collection

ArrayList: threads are out of sync. The default initial capacity is 10, and the growth rate is 50% of the current length when the array size is insufficient.

Vector: thread synchronization. The default initial capacity is 10, and the growth rate is 100% of the current length when the array size is insufficient. Its synchronization is achieved through the Iterator method plus synchronized.

LinkedList: threads are out of sync. Double-ended queue form.

Stack: thread synchronization. Inherited from Vector, several methods have been added to complete the functionality of the stack.

A Set:Set is a Collection,Set that does not contain repeating elements and has at most one null element.

HashSet: threads are out of sync, HashMap is used internally for data storage, and the methods provided are basically calling HashMap methods, so the two are essentially the same. The collection element can be NULL.

NavigableSet: a search function has been added to search for a given element: less than, less than or equal to, greater than or equal to, and put back a qualified key closest to the given element.

TreeSet: threads are out of sync and NavigableMap operations are used internally. The default elements are arranged in "natural order", which can be changed through Comparator.

EnumSet: threads are out of sync. Internally, it is implemented using Enum array, which is faster than HashSet. Enumerated values can only be stored in enumerated classes passed in by the constructor.

three。 Map

HashMap: threads are out of sync. It is stored according to the hashcode of key, and internally using an array of static internal class Node. The default initial size is 16, which is doubled at a time. When a Hash conflict occurs, the zipper method (linked list) is used. Can be accepted as key values (key) and values (value) of null. In JDK 1.8: when the number of elements in a single bucket is greater than or equal to 8, the linked list implementation is changed to the red-black tree implementation; when the number of elements is less than 6, the linked list implementation is changed back. So as to prevent hashCode attacks.

LinkedHashMap: saves the insertion order of records. When traversing LinkedHashMap with Iterator, the first record must be inserted first. You can also use parameters at construction time to sort by the number of times you apply. It will be slower than HashMap when traversing, but there is an exception. When the capacity of HashMap is very large and the actual data is less, it may be slower than LinkedHashMap, because the traversal speed of LinkedHashMap is only related to the actual data and has nothing to do with capacity, while the traversal speed of HashMap is related to its capacity.

TreeMap: thread is not synchronized, based on the red-black tree (Red-Black tree) NavigableMap implementation, it can sort records according to the key, the default is to sort by the ascending order of the key value, you can also specify the sort comparator, when using Iterator to traverse the TreeMap, the records are sorted.

HashTable: thread safe, the Iterator of HashMap is the fail-fast iterator. HashTable cannot store key and value of NULL.

four。 Tool class

Collections, Arrays: a utility class / helper class of a collection class that provides a series of static methods for sorting, searching, thread-safe, and other operations on elements in the collection.

Comparable,Comparator: generally used for object comparison to achieve sorting, the two are slightly different.

The class designer did not implement the Comparable interface without considering the comparison problem. This is what we can do by using Comparator, in which case we don't need to change the object. In a collection, we may need to have multiple sorting criteria, so if we use Comparable, we can inherit Comparator to provide a variety of standard comparators for sorting.

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