<> aggregate
Java Set is java A toolkit is provided , All inherited from java.util.*. It mainly includes two interfaces Collection Interface and Map Interface and related tool classes (
Iterator Iterator Interface ,Enumeration Enumeration class ,Arrays and Colletions).
(1)Collection It's an interface , contain List List and Set aggregate . among List It's an orderly queue , Element values can be repeated , Index from 0 start , The implementation classes are LinkedList,
ArrayList,Vector; and Set Is a collection that does not allow duplicate elements ,Set The implementation classes of HashSet and TreeSet.HashSet Rely on HashMap
, It's actually through the HashMap Realized ;TreeSet Rely on TreeMap, It's actually through the TreeMap Realized .
(2)Map Is a mapping interface , adopt key-value Key value pair implementation . The implementation classes are HashMap,TreeMap,WeakHashMap,Hashtable.
(3)Iterator Is a tool for traversing collections ,Enumeration It's also a traversal collection , But it's more functional than that Iterator less , Only in Hashtable, Vector,
Stack Use in .
(4)Arrays and `Collections Is an operation array , Two tool classes of the collection .
Here is a more specific comparison :
(1) ArrayList, LinkedList, Vector, Stack yes List Of 4 Implementation classes , Compare their similarities and differences .
1)ArrayList Is an array queue , Equivalent to a dynamic array . It is implemented by an array , High efficiency of random access , Random insertion , The efficiency of random deletion is low . The default size is 10 element
2)LinkedList It's a two-way linked list . It can also be used as a stack , Queue or double ended queue for operation .LinkedList Low efficiency of random access , But randomly inserted , High efficiency of random deletion .
3)Vector It's a vector queue , and ArrayList equally , It is also a dynamic array , Implemented by array . however ArrayList It's not thread safe , and Vector It's thread safe .
4)Stack It's a stack , It inherits from Vector. Its characteristics are : First in, second out (FILO, First In Last Out).
(2)HashMap,HashTable,TreeMap The difference between
1)HashMap It is stored in the form of key value pairs , But there is no guarantee of order , Single thread ; Default size 16, The default expansion factor is 0.75,
structure 1.7 before ( array + Linked list ),1.8 after ( array + Linked list + Red and black trees )
HashMap stay JDK1.8 And later versions introduced the structure of red black tree , If the number of linked list elements in the bucket is greater than or equal to 8 Time , Converting linked list into tree structure ; If the number of linked list elements in the bucket is less than or equal to 6 Time , Tree structure reduced to linked list . Because the average search length of red and black trees is log(n), Count Reg 8 When , The average search length is 3, If you continue to use the linked list , The average search length is 8/2=4, This is necessary to convert to a tree . If the list length is less than or equal to 6,6/2=3, Although the speed is also very fast , But the time to convert to tree structure and spanning tree is not too short .
There are also options 6 and 8, There's a difference in the middle 7 It can effectively prevent frequent conversion of linked list and tree . Suppose , If the number of linked lists exceeds 8 Then the linked list is converted into a tree structure , The number of linked lists is less than 8 Then the tree structure is converted into a linked list , If one HashMap Constantly inserting , Delete element , The number of linked lists is in 8 Wandering left and right , It will happen frequently , Linked list to tree , It's going to be inefficient .
2)HashTable And HashMap The storage method is the same , But it's thread safe ;
3)TreeMap Is an ordered key value pair , Sorting based on red black tree .
Technology
Daily Recommendation