In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces what the Java collection framework is, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Introduction 1. Introduction to the collection framework
The Java collection framework provides a set of high-performance, easy-to-use interfaces and classes, which are located in the java.util package. The container mainly includes Collection and Map. Collection stores a collection of objects, while Map stores a mapping table of key-value pairs (two objects)
2. Introduction to related containers 2.1 Set related
TreeSet
Based on the red-black tree implementation, it supports orderly operations, such as finding elements according to a range. However, the search efficiency is not as efficient as HashSet,HashSet, and the time complexity of search is O (1), while TreeSet is O (logN).
HashSet
Based on hash table implementation, supports fast lookup, but does not support orderly operations. And the insertion order information of the elements is lost, that is, the result of traversing the HashSet using Iterator is uncertain.
LinkedHashSet
Has the lookup efficiency of HashSet, and internally uses a two-way linked list to maintain the insertion order of elements.
2.2 List correlation
ArrayList
Based on dynamic array implementation, supports random access.
Vector
Similar to ArrayList, but it is thread-safe.
LinkedList
Based on the bi-directional linked list implementation, it can only be accessed sequentially, but elements can be quickly inserted and deleted in the middle of the linked list. Not only that, LinkedList can also be used as a stack, queue, and two-way queue.
2.3 Queue related
LinkedList
Two-way queues can be implemented.
PriorityQueue
Based on the heap structure implementation, you can use it to implement priority queues.
2.4 Map correlation
TreeMap
Based on the red-black tree.
HashMap
Based on hash table implementation.
HashTable
Similar to HashMap, but thread-safe, which means that multiple threads can write to HashTable at the same time without causing data inconsistencies. It is a legacy class and should not be used. ConcurrentHashMap can now be used to support thread safety, and ConcurrentHashMap is more efficient because ConcurrentHashMap introduces segmented locks.
LinkedHashMap
Use a two-way linked list to maintain the order of elements, in insert order or least recently used (LRU) order
3. Focus on the collection
The Collection interface stores a set of non-unique, unordered objects
The List interface stores a non-unique, ordered set of objects.
The Set interface stores a set of unique, unordered objects
The Map interface stores a set of key objects and provides key-to-value mapping
ArrayList implements an array of variable length, allocating contiguous space in memory. Traversal elements and random access elements are more efficient
LinkedList is stored in linked list. It is more efficient to insert and delete elements
Set implemented by HashSet using Hash algorithm
The bottom layer of HashSet is implemented with HashMap, so the query efficiency is high. Because the hashCode algorithm is used to determine the memory address of elements directly, the efficiency of adding and deleting is high.
Second, ArrayList analysis 1, ArrayList use method description boolean add (Object o) to add elements at the end of the list, the starting index position starts from 0 void add (int index, Object o) adds elements in the specified index position, the index position must be between 0 and the number of elements in the list int size () returns the number of elements in the list Object get (int index) returns the elements at the specified index position. The element taken out is of Object type. Use the antecedent to perform beneficial type conversion boolean contains (Object o) to determine whether there is a specified element boolean remove (Object o) in the list. Delete the element Object remove (int index) from the list. Delete the specified location element from the list. The starting index starts from 0. 2. Introduction to ArrayList
ArrayList is an index sequence that can be dynamically grown and shrunk. It is a List class based on array implementation.
This class encapsulates a dynamically reassigned Object [] array, and each class object has a capacity [capacity] attribute that represents the length of the Object [] array they encapsulate, which is automatically increased when an element is added to the ArrayList. If you want to add a large number of elements to ArrayList, you can use the ensureCapacity method to increase the capacity at one time, which can reduce the number of redistributions and improve performance.
The usage of ArrayList is similar to that of Vector direction, but Vector is an older collection with many disadvantages and is not recommended
In addition, the difference between ArrayList and Vector is that ArrayList is not thread-safe. When multiple threads access the same ArrayList collection, the program needs to manually ensure the synchronization of the collection, while Vector is thread-safe.
3. Source code analysis 3.1 inheritance structure and hierarchical relationship public class ArrayList extends AbstractList implements List, RandomAccess, Cloneable, java.io.Serializable
Here's a brief explanation of several interfaces.
RandomAccess interface
This is a marked interface, by looking at api documents, its role is to be used for fast random access, efficiency issues, in the implementation of this interface, then use a normal for loop to traverse, higher performance, such as ArrayList. If you don't implement the interface, use Iterator to iterate over it for better performance, such as linkedList. So this markup is just to let us know how we can get the data in a better way.
Cloneable interface
Once the interface is implemented, you can use the Object.Clone () method.
Serializable interface
Implement the serialization interface, indicating that the class can be serialized. What is serialization? To put it simply, you can change from a class to a byte stream, and then from a byte stream to the original class.
The inheritance structure here can be viewed through Navigate > Type Hierarchy in IDEA
3.2 attribute / / version number: private static final long serialVersionUID = 8683452581122892189L; default capacity private static final int DEFAULT_CAPACITY = 10; empty object array private static final Object [] EMPTY_ELEMENTDATA = {}; / / default empty object array private static final Object [] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; / / the actual element size of the array element transient Object [] elementData; / / non-private to simplify nested class access// stored, default is 0private int size / / maximum array capacity private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE-8 * 3.3 Construction method / * construct an empty list with specified initial capacity * if the specified initial capacity is negative, then IllegalArgumentException * / public ArrayList (int initialCapacity) {if (initialCapacity > 0) {this.elementData = new Object [initialCapacity];} else if (initialCapacity = = 0) {this.elementData = EMPTY_ELEMENTDATA } else {throw new IllegalArgumentException ("Illegal Capacity:" + initialCapacity);}} / * * the default empty array is 10 * ArrayList. The data stored in the array is actually an array, which is elementData * / public ArrayList () {this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA. } / * construct a list of elements containing the specified set in the order returned by the collection iterator * / public ArrayList (Collection list) / / A random sort of List collection elements static void shuffle (List list) / / sort the specified list in ascending order according to the natural order of the elements static void sort (List list) / / sort the specified list according to the order generated by the specified comparator static void sort (List list, Comparator list, int I Int j) / / when distance is positive Move the last distance elements of the List collection as a whole to the front When distance is negative, move the first distance elements of the list collection "as a whole" to the back. This method does not change the length of the collection static void rotate (List list, int distance) 3, search and replace operations / / use the binary search method to search the specified list to obtain the index of the specified object in the List collection / / Note: previously, you must ensure that the elements in the List collection are already in an ordered state static int binarySearch (Listc,object o) / / return the starting position of the specified target list in the specified source list for the first time If such a list does not appear, return-1static int indexofsubList (Listsource,Listtarget) / / returns the starting position of the last occurrence of the specified target list in the specified source list; if no such list appears, return-1static int lastIndexofsubList (Listsource,Listtarget) / / replace all the old values o1dvalstatic boolean replaceA1l (list list,T oldval,T newval) 4 of the List object with a new value, synchronization control
Collectons provides several synchronizedXxx () methods, which can wrap a specified collection as a thread-synchronized collection, thus solving the thread-safety problem when multiple threads access the collection concurrently. As described earlier, HashSet,TreeSet,arrayList,LinkedList,HashMap,TreeMap is thread-unsafe. Collections provides several static methods to wrap them as thread-synchronized collections.
/ / return synchronization supported by the specified Collection (thread safe) collectionstatic Collection synchronizedCollection (Collection c) / / return the list of synchronization supported by the specified list (thread safe) static List synchronizedList (List list) / / return the synchronization supported by the specified mapping (thread safe) mapping static Map synchronizedMap (Map m) / / return the synchronization supported by the specified set (thread safe) setstatic Set synchronizedSet (Set s) 5, Collection settings immutable set / return an empty, Immutable collection object The collection here can be either List, Set, or Map. EmptyXxx () / / returns an immutable collection object that contains only the specified object (only one or one element), where the collection can be: List,Set,Map. SingletonXxx (): / / returns an immutable view of the specified collection object, where the collection can be: List,Set,MapunmodifiableXxx ()
Thank you for reading this article carefully. I hope the article "what is the Java Collection Framework" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.