Relationship Diagram
Single-Instance set system
---------| Collection The root interface of a singleton collection
--------------| List inherits the collection Interface , features: orderly, repeat .
------------------| Arrarylist Inherit the list interface, the bottom is the use of the Object Array Implementation, features: Fast query speed, and delete slow.
------------------| LinkedList inherit the list interface, the underlying is the use of linked list data structure to achieve, features: Slow query, and delete quickly.
------------------| Vectors inherit the list interface, and vector implementations are consistent with ArrayList , but are thread-safe and operate inefficiently. jdk1.0 , when it came up .
--------------| Set inherits the collection interface, has the characteristic: disorderly, cannot repeat .
------------------| HashSet inherits the set interface, which is supported by a hash table, with the following features: fast access speed.
------------------| TreeSet inherits the set interface, which is implemented using the red-black tree (binary tree) data structure, and features: The elements are sorted and stored.
----------- ---| The queue inherits the collection interface, which queues the first -in, FIFO containers. There are more queue subclasses, which are used in threads and concurrency.
HashSet the principle of adding elements:
towards HashSet the element is added, first HashSet calls the element 's hashcode method to get the hash code value of the element, and then passes through a series of operations You can figure out where the element is stored in the hash table.
Scenario 1: If the position of the element is not currently stored in any element, then the element can be stored directly
Scenario 2: If the position of the element is currently present with other elements, then the equals method of the element is also called to compare with the element at that location.
if the Equals method returns false, then the element allows storage if the euqlas method returns true , the element is considered a repeating element and is not allowed to be stored.
TreeSet Things to be aware of:
1. When adding elements to TreeSet, if the elements themselves have natural order characteristics, they are sorted according to the nature of the elements ' natural order.
2. When adding elements to TreeSet, if the element itself does not have a natural order, then the class to which the element belongs must implement the comparable interface. Define the comparison rules for elements
in the the CompareTo method.
3. When adding elements to TreeSet, if the element itself does not have a natural order, and the class to which the element belongs does not implement the comparable interface, Then you have to create a
TreeSet The object when it is passed into the comparer.
4. if the method of comparison (CompareTo or Compare) returns 0 , then the element is considered a repeating element, Add is not allowed.
the definition format of the comparator: Customizing a class implementation COmparator interface.
class name implements comparator{
}
two-column collection system:
--------------| Map map and collection have no relationship , with the characteristics: stored data are in the form of key-value pairs exist, the key is not repeatable, the value can be repeated .
------------------| The HASHMAP is based on a hash table.
------------------| TreeMap is based on the red-black tree (binary tree) data structure implementation, characteristics: The key of the element will be sorted storage.
------------------| Hashtable
HashMap principle of storage :
towards HashMap the element is added, it first invokes the hashcode method of the key to get the hash code value of the element, and then the operation can calculate the location of the element in the hash table.
Scenario 1: If the calculated position does not currently have any element storage, then the element can be added directly to the hash table.
Scenario 2: If the calculated position already has other elements present, then the equals method of the element is also called to compare with the element at this position, if equals method returns the false , then the element is allowed to be stored if equals method returns the true , then the element is treated as a repeating element, forbid storage.
TreeMap Things to be aware of:
1. When adding elements to TreeMap, if the keys of the elements are in a natural order, they are sorted by the natural order characteristics of the keys.
2. When adding elements to TreeMap , if the key of the element does not have a natural order attribute, then the class that the key belongs to must implement the comparable interface, the key The comparison rule is defined on the CompareTo method.
3. When adding elements to TreeMap, if the key of the element does not have a natural order attribute and the class to which the key belongs does not implement the comparable interface, then it must be When you create TreeMap The object when it is passed into the comparer.
Summary of differences
Vectorand theArrayList
1,Vectoris thread-safe, so it is thread-secure, andArrayListis thread-asynchronous and is unsafe. If you do not take into account the security factors of the thread, GeneralArrayListhigh efficiency ratio.
2, if the number of elements in the collection is greater than the length of the current collection array,VectorThe growth rate is the current array length100%, whileArrayListThe growth rate is the current array length50%. If you use large data volumes in the collection,Vectorhave a certain advantage.
3, if you are looking for data at a specified location,Vectorand theArrayListThe time used is the same, if the data is accessed frequently, this time useVectorand theArrayListall can. If moving a specified position causes the subsequent elements to move, you should consider using thelinklist,Other elements do not move because it moves the data at a specified location.
ArrayListand theVectoris to store the data in an array, which is larger than the actual stored data in order to increase and insert elements, both allow the direct ordinal index element, but the insertion of data to involve the array element movement and other memory operations, so the index data fast, the insertion of data is slow,Vectorbecause of the use ofsynchronizedmethod (thread-safe) so the performance ratioArrayListto be poor,LinkedListusing a doubly linked list for storage, indexed data by ordinal is required to traverse forward or backward, but inserting data requires only the front and back of the item to be recorded, so the number of insertions is faster.
ArrayListand theLinkedList
1, ArrayListis to implement a data structure based on a dynamic array,LinkedLista linked list-based data structure.
2.for random accessGetand theSet,ArrayListfeel better thanLinkedList, becauseLinkedListThe pointer to move.
3.for new and deleted operationsAddand theRemove,linedlistcomparative advantage, becauseArrayListyou want to move the data. this point depends on the actual situation. If you insert or delete only a single piece of data,ArrayListthe speed is better thanLinkedList. But if the bulk of the random insert delete data,LinkedListthe speed is much better thanArrayList.becauseArrayListto move the insertion point and all subsequent data, insert a single piece of data.
HashMapwith theTreeMap
1,HashMapthroughhashcodequickly find its content, andTreeMapall elements in a fixed order, and if you need to get an orderly result you should useTreeMap(HashMapThe order in which the elements are arranged is not fixed.)
2, inMapto insert, delete, and position elements in theHashMapis the best choice. But if you want to traverse the key in natural order or in a custom order,TreeMapwould be better. UseHashMapthe key class required to be added clearly defines thehashcode ()and theequals ()the implementation.
two xMap, but the order is different, causinghashcode ()not the same.
do the same test:
in theHashMapThe same value in themap,different order,equalswhen thefalse;
and inTreeMapThe same value in themap,different order, equalswhen thetrue, Description,TreeMapin theequals ()was sorted out in order.
HashTablewith theHashMap
1, synchronization: Hashtableis thread-safe, that is, synchronous, andHashMapIt is unsafe for the thread program, not synchronous.
2,HashMapallows for the existence of aNULLof theKey, multiple forNULLof thevalue.
3,Hashtableof theKeyand thevalueare not allowed toNULL.
Summary of Java collection classes