The java collection class can be used to store multiple objects of varying numbers, and can implement common data structures such as stacks, queues, etc. Unlike arrays, the length of arrays is immutable. Array elements can store basic types and object types, while collections can only store objects (essentially object referencesvariables), Java Sets can be roughly divided into Set, List, and Map, where Set represents an unordered, non-repeatable collection, and List represents an ordered, repeatable collection. , Map represents a collection with a mapping relationship, the key is unique in the map, and the value is repeatable. Among them, Set, List and the Queue queue newly added after java5 are subclasses derived from CollectionInterface
Set collection is not allowed to contain the same elements, and there is no obvious order between objects in the collection
HashSet, LinkedHashSet, and TreeSet are the main implementation classes of Set
HashSet cannot guarantee the order of elements, HashSet is not synchronized, and HashSet set elements are allowed to be null, When an object is stored in a HashSet, the HashSet will call the hashCode method of the object to obtain the hashCode value of the object, and determine the storage location of the object in the HashSet based on the hashCode value. In HashSet, whether two objects are equal is judged by comparing the return values of the equals() and hashCode() methods.
LinkedHashSet is a subclass of HashSet, but it also uses a linked list to maintain the order of elements, so that The elements seem to be saved in the order of insertion. Because it needs to maintain the insertion order of the elements, the performance is slightly lower than that of HashSet. Since it is still a HashSet, the same element cannot be repeated
TreeSet
TreeSet is the implementation class of the SortedSet interface. Similarly, TreeSet can ensure that the collection elements are in the sorted state. TreeSet will call the comparTo (Object obj) method of the collection elements to compare the size relationship between the elements, and then set the collection The elements are arranged in ascending order, that is, natural sorting. TreeSet can also implement customized sorting through the Comparator interface. When creating a TreeSet collection object, a Comparator object is provided to be associated with the TreeSet collection, and the Comparator object is responsible for the sorting logic of the collection elements.
Comparison of various Set performance
The performance of HashSet is always better than TreeSet, because TreeSet requires additional algorithms to maintain the order of set elements. TreeSet should only be used when a sorted set needs to be maintained. Otherwise, HashSet should be used. In addition, LinkedHashSet is a subclass of HashSet, corresponding to ordinary insertion and delete operations. The performance of LinkedHashSet is slightly slower than that of HashSet. This is due to the overhead caused by maintaining the linked list, but because of the linked list , it will be faster when traversing LinkedHashSet. However, none of the three implementation classes of set are thread-safe. You can usually use the synchronizedSortedSet method of the Collections tool class to wrap the set collection. This operation is best performed at creation time. For example: SortedSet s = Collections.synchronizedSortedSet(
new TreeSet(...));<a href="http://www.php.cn/wiki/165.html" target="_blank"></a><h4>List collection represents an ordered and repeatable collection. The elements in the collection have their corresponding <a href="http://www.php.cn/code/229.html" target="_blank">indexes</a>
</h4>
<h5>
<a href="http://www.php.cn/wiki/1041.html" target="_blank">Array</a>List and Vector are Lists Two typical implementations, and a Linke<a href="http://www.php.cn/wiki/596.html" target="_blank">dL</a>ist</h5>
<p> where the ArrayList and Vector classes encapsulate a dynamic Object[] array that allows reallocation, when the number of elements is added When the array length is exceeded, the initial length is automatically increased. The significant difference between ArrayList and Vector is that ArrayList is not thread-safe, while Vector is thread-safe and does not require a program to ensure the synchronization of the collection. Because Vector is thread-safe, Vector color performance is lower than ArrayList. Even if thread safety needs to be ensured, it is generally not recommended to use Vector. Instead, use the relevant methods of the Collections tool class to wrap the ArrayList into a thread-safe class. <br>Vector also has a subclass called Stack, which is used to simulate the data structure of a stack. Last in, first out. Since it is an <a href="http://www.php.cn/code/6064.html" target="_blank"> inheritance </a> relationship, Stack is also thread-safe, so its performance is relatively poor. It is recommended. If you need to use a data structure such as a stack, you can consider using LinkedList.<br>LinkedList is also an implementation class of List. It is a List based on linked list implementation. It is optimized for sequential access to elements in the collection, especially insertion. It is very fast to delete elements. LinkedList implements both the List interface and the Deque interface. Because it implements the Deque interface, it can be used as a stack. The Queue collection is used to simulate the data structure of a queue, first in, last out. Deque is a sub-interface of Queue, representing a double-ended queue, allowing elements of the queue to be manipulated from both ends. </p>
<h5 id="The-difference-between-LinkedList-and-ArrayList">The difference between LinkedList and ArrayList</h5>
<p>Since ArrayList stores the elements in the collection internally in the form of an array, it has better performance when randomly accessing collection elements, while LinkedList uses a linked list. It is used to save the elements of the collection, so the random access performance is poor, but the performance when inserting and deleting elements is very good. </p>
<h4 id="Map-is-used-to-save-data-with-mapping-relationships-Both-key-and-value-can-be-any-reference-type-of-data-but-the-key-value-of-the-map-is-unique-and-cannot-be-repeated">Map is used to save data with mapping relationships. Both key and value can be any reference type of data, but the key value of the map is unique and cannot be repeated. </h4>
<h5 id="HashMap-and-Hashtable-are-Maps-Two-implementation-classes">HashMap and Hashtable are Maps Two implementation classes </h5>
<p>Hashtable is a thread-safe Map implementation class, but HashMap is not thread-safe, so the performance of HashMap is slightly better. <br> In addition, Hashtable does not allow the use of null as key and value. Trying to put null into Hashtable will cause a null pointer exception, but HashMap can use null as key and value. Since keys cannot be repeated, HashMap has at most one key that is null. But there can be multiple values that are null. <br>The standard for HashMap and Hashtable to judge whether two keys are equal is that the return values of the two keys through the equals() and hashCode() methods are equal. <br>LinkedHashMap is a subclass of HashMap. LinkedHashMap also uses a doubly linked list to maintain the order of keys, that is, the iteration order is consistent with the insertion order. Because the insertion order of elements needs to be maintained, the overall performance is slightly lower than HashMap. </p>
<h5 id="TreeMap">TreeMap</h5>
<p>There is also a SortedMap interface under Map. The SortedMap interface is another TreeMap implementation class. When TreeMap saves key-value pairs, it needs to sort the nodes according to the key. TreeMap sorting is divided into natural sorting. And customized sorting. In natural sorting, keys must implement the Comparable interface. In scheduled sorting, a Comparator object needs to be passed in when creating a TreeMap, and this object will sort the keys in the TreeMap. Similar to TreeSet. TreeMap is slower than HashMap and Hashtable because it needs to maintain ordering. </p>
<h4 id="The-difference-between-Collection-and-Collections">The difference between Collection and Collections</h4>
<p>Collection is an interface of Java collection<a href="http://www.php.cn/css/css-rwd-frameworks.html" target="_blank">Framework</a><br>Collections is a tool class in the collection framework, which provides A large number of methods are provided to sort collection elements, <a href="http://www.php.cn/php/php-tp-demand.html" target="_blank">query</a>, modify and other operations. It also provides some methods for setting collection objects to be immutable and achieving synchronization control of object collections. </p>
The above is the detailed content of Java collection framework study notes. For more information, please follow other related articles on the PHP Chinese website!

Javaispopularforcross-platformdesktopapplicationsduetoits"WriteOnce,RunAnywhere"philosophy.1)ItusesbytecodethatrunsonanyJVM-equippedplatform.2)LibrarieslikeSwingandJavaFXhelpcreatenative-lookingUIs.3)Itsextensivestandardlibrarysupportscompr

Reasons for writing platform-specific code in Java include access to specific operating system features, interacting with specific hardware, and optimizing performance. 1) Use JNA or JNI to access the Windows registry; 2) Interact with Linux-specific hardware drivers through JNI; 3) Use Metal to optimize gaming performance on macOS through JNI. Nevertheless, writing platform-specific code can affect the portability of the code, increase complexity, and potentially pose performance overhead and security risks.

Java will further enhance platform independence through cloud-native applications, multi-platform deployment and cross-language interoperability. 1) Cloud native applications will use GraalVM and Quarkus to increase startup speed. 2) Java will be extended to embedded devices, mobile devices and quantum computers. 3) Through GraalVM, Java will seamlessly integrate with languages such as Python and JavaScript to enhance cross-language interoperability.

Java's strong typed system ensures platform independence through type safety, unified type conversion and polymorphism. 1) Type safety performs type checking at compile time to avoid runtime errors; 2) Unified type conversion rules are consistent across all platforms; 3) Polymorphism and interface mechanisms make the code behave consistently on different platforms.

JNI will destroy Java's platform independence. 1) JNI requires local libraries for a specific platform, 2) local code needs to be compiled and linked on the target platform, 3) Different versions of the operating system or JVM may require different local library versions, 4) local code may introduce security vulnerabilities or cause program crashes.

Emerging technologies pose both threats and enhancements to Java's platform independence. 1) Cloud computing and containerization technologies such as Docker enhance Java's platform independence, but need to be optimized to adapt to different cloud environments. 2) WebAssembly compiles Java code through GraalVM, extending its platform independence, but it needs to compete with other languages for performance.

Different JVM implementations can provide platform independence, but their performance is slightly different. 1. OracleHotSpot and OpenJDKJVM perform similarly in platform independence, but OpenJDK may require additional configuration. 2. IBMJ9JVM performs optimization on specific operating systems. 3. GraalVM supports multiple languages and requires additional configuration. 4. AzulZingJVM requires specific platform adjustments.

Platform independence reduces development costs and shortens development time by running the same set of code on multiple operating systems. Specifically, it is manifested as: 1. Reduce development time, only one set of code is required; 2. Reduce maintenance costs and unify the testing process; 3. Quick iteration and team collaboration to simplify the deployment process.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
