Java Collection Framework Collection framework diagram
The collection interface provides a set of methods for manipulating mass objects, which provides basic operations such as adding, deleting.List
The most important feature of list is Order; it ensures that the elements are preserved in a certain order.
- ArrayList: A list implemented with an array. Allows for fast random access, but it is slower to insert and delete elements in the middle of the list.
- LinkedList: Sequential access is optimized. Inserting and deleting elements in the middle of a list is not expensive. The speed of random access is relatively slow. Commonly used as a stack, queue implementation.
Set
Object elements in set cannot be duplicated.
- HashSet: Set that is designed to optimize query speed, sets the hash function to implement the index. To put into the hashset inside the object also has to define HASHCODE ().
- The Linkedhashset collection also determines where the element is stored, based on the hashcode value of the element, but it maintains the order of the elements using the linked list.
- TreeSet is the only implementation class for the SortedSet interface, which uses red-black trees to sort the treeset to ensure that the collection elements are in the sorted state.
Map
Key-value pairs.
- HASHMAP: Based on the implementation of the hash table, an array of hash values is established for the key.
The difference between the Hashtable and the
1. Inherit different
public class Hashtable extends Dictionary implements Map
public class HashMap extends Abstractmap implements Map
The methods in 2.Hashtable are synchronous, and the methods in HashMap are not synchronized by default. In a multithreaded concurrency environment, you can use Hashtable directly.
3. Using different hash values, Hashtable directly uses the object's hashcode. The hash value is recalculated by the HashMap.
- Linkedhashmap: Much like HashMap, but when traversing with iterator, it is accessed in the order of insertion or in the order in which it was first used (least-recently-used (LRU).
- TREEMAP: Based on the implementation of red-black tree data structure.
Java Collection Framework