The Java Collection schema supports 3 types of collections: Rule set (set), Linear table (list), and diagram (map), respectively, defined in Set,list,map.
A set instance stores a set of distinct elements (collections), a list instance stores a set of sequential elements (tables), a map that stores a set of objects---a mapping of key values
A reference to a Java object is stored in the collection, and is a reference collection that points to the corresponding object space rather than to the object stored by the collection itself
The base data type value cannot be stored in the collection, and the base data type can be loaded into the collection, but inside it is automatically boxed into a wrapper class object and then stored in the collection
The overall architecture is as follows, very important, including the inheritance relationship, implementation of the classification, at a Glance: The collection is divided into the map interface and the collection interface, which are the root interface of the Java Collection framework
Collection interface:
Set interface: Non-repeatable no index
HashSet Specific class: Use hashing algorithm to repeat, high efficiency, but the element is unordered
Linkedhashset Specific class: HashSet sub-class, the same principle, in addition to Deduplication can also preserve the storage order
TreeSet specific class: Using the two-tree algorithm ordering, you can specify a sequence, the object will be deposited in the order specified
List interface: repeatable, with Storage order (indexed), stored and ordered
ArrayList Concrete Class: array implementation, adding and deleting slow, Find Fast thread insecure
LinkedList specific class: Linked list implementation, delete and subtract fast, find slow thread insecure
Vector class vectors specific class: array implementation, principle and arraylist the same, but thread safety
Stack Concrete Class
Map interface:
HashMap class: Use hash algorithm to duplicate keys, high efficiency, but unordered, allow null key and null value, thread unsafe
Linkedhashmap class: Use hash algorithm to repeat, and keep storage order, thread unsafe
TreeMap class: You can customize the order by using the two-fork tree algorithm. You can sort the keys in the map collection. , Thread is not secure
Hashtable: Similar to HashMap, thread safety, low efficiency, NULL keys and null values are not allowed
Properties: Inheritance HashTable more stringent than HashTable each key and its corresponding value in the attribute list is a string.
--------------------------------------------------------------------------------------------------------------- ------------------------------------
1) First of all, collection interface, which is the root interface of processing object collection, provides some common methods, size,iterator,add,remove something
2) Both the set and the list interface are extended from Collection,set is a set of high school mathematics, not allowed to repeat, disorderly. List is like a table, can be repeated, elements in the table is placed in order.
3) Then for the 3 implementations of the set interface:
The HashSet object must implement the Hashcode method, Javaapi Most classes implement the Hashcode method. Determines where the object is placed according to the hashcode () return value
HashSet is based on the HASHMAP implementation, all elements in the hashset are saved by the key in the HashMap, but only provide access to the key, value a static object object present, does not provide access
How does HashSet guarantee the element uniqueness of the collection?
The two object Hashcode () methods in HashSet return values equal, and two objects return true through the Equals method to determine that two elements are the same, that adding the same elements to the collection is not available, not overwriting
Linkedhashset implements an extension to the HashSet, supports the ordering of elements within the ruleset, has no order in the HashSet, and in Linkedhashset, it can be extracted in the order in which the elements are inserted into the collection
TreeSet guarantees that the elements of the set are orderly, there are 2 ways to achieve comparability between objects: 1, objects added to TreeSet implement the comparable interface, 2, specify a comparer for the elements of the ruleset (Comparator)
TreeSet the bottom layer is stored with treemap, in fact, the elements in the TreeSet are saved in the key in the TreeMap, treemap using the red and black tree sort binary tree to save the entry in the collection, support natural sorting and custom sorting, Natural sorting by default
Natural sort: TreeSet invokes the element's CompareTo (Object obj) method (returns an integer, returns 0 for equality, puts it in the same position) to compare the size between elements, and then sorts it in ascending order, which is the natural sort.
The elements in the TreeSet collection must implement the Compareble interface, otherwise the exception will be reported, the way to judge the uniqueness of the element: it is based on the comparison method of whether the return result is 0, is 0, is the same element, does not exist.
TreeSet how the elements are sorted one:
Let the element itself have the comparison function, the element needs to implement the comparable interface. Overrides the CompareTo method.
If you do not sort by the natural order that is available in the object. If the object does not have a natural order. What to do?
You can use the TreeSet collection The second sort method two:
Let the collection itself have a comparison function, define a class to implement the comparator interface, overriding the Compare method.
Pass the class object as a parameter to the constructor of the TreeSet collection.
Tips for use:
If you want to extract the elements in the order in which they are inserted into the collection, with Linkedhashset, its elements are stored in the order in which they are added
If there is no above demand, should use hashset, its efficiency is higher than linkedhashset
Linkedhashset just keep order in the order in which they are added, and to add order attributes to the collection elements, you need to use TreeSet (the collection element has a sort relationship).
4) Again, several implementations of the list
The most important of course is ArrayList (unsynchronized) and LinkedList, a list of dynamically expanding capacity using an array implementation, and a chain-implemented list.
ArrayList internally encapsulates a dynamically redistributed object[] array that has a capacity property to represent the and length of this array, such as not specifying an initial length of 10, and when the increment element exceeds that length, capacity automatically increases to 1.5 times times the original
LinkedList: is actually a doubly linked list, not only implements the list interface, but also implements the Deque interface has two-way queue and the characteristics of the stack thread unsafe
There is also the vector (synchronous) class, which, in addition to the synchronization method that includes access and modification vectors, is the same as ArrayList.
Even if you need to ensure thread safety, it is not recommended to use vectors, you can use the Synchronizedlist method in the Collections tool class to wrap the ArrayList into a thread-safe substitute vector
The last is the Stack class, it inherits from the Vector class, but generally only as a function of the stack to use, do not use the vector inside the function
5) Map
Map is a mapping, which is fundamentally different from the previous set and list.
Hash chart HashMap, chain hash chart linkedhashmap, tree diagram Treehashmap is a mapping of 3 implementations, from the name, with the above set of 3 implementations of the analysis, this is similar.
HASHMAP: High Efficiency
Likedhashmap: stored in order of addition, can be removed in order of addition
Treehashmap: Sort Sex
--------------------------------------------------------------------------------------------------------------- ------------------------------------
Collections class and Arrays class:
Collections Class (Note not collection): Provides a number of static methods to manage collections, linear tables (most of which are to manipulate linear tables, such as for linear table copying, sorting, etc., see API)
Arrays class: Provides a variety of static methods for sorting, locating, comparing, and populating elements of an array.
A collection can be converted to an array of object[] objects, but not an array of primitive types, because the collection contains objects
--------------------------------------------------------------------------------------------------------------- -------------------------------------
Iteration (Traversal)
(1). List
* A. Normal for loop, using get ()
* B. Call the iterator () method to get iterator, using the Hasnext () and Next () methods
* C. Enhance for loop, as long as the class that can use iterator can be used
* D.vector Collection can use enumeration's hasmoreelements () and Nextelement () methods
(2). Set
* A. Call the iterator () method to get iterator, using the Hasnext () and Next () methods
* B. Enhance for loop, as long as the class that can use iterator can be used
(3). Map
* KeySet (): Get all the keys composed of set, traverse set to get each key, and then get the value separately
* EntrySet (): Get all the entry composed set, traverse set to get each entry, then Getkey () and GetValue () respectively
(4). Issues that are removed during iteration
* A.for Loop: Delete the loop variable after deletion because the subsequent element will move forward--
* B. iterators: Iterating through collections using iterators you must use Remove () in iterator to delete an element or an exception will be thrown
* C. Enhanced for loop: Cannot delete
--------------------------------------------------------------------------------------------------------------- -----------------------------------
A collection framework for the Java Foundation