Java collection inheritance diagram sharing
Object oriented languages embody things in the form of objects. Therefore, in order to facilitate the operation of multiple objects, objects are stored. Collection is the most commonly used way to store objects.
Although arrays can also store objects, their length is fixed; The length of the collection is variable. The basic data types can be stored in the array, and the collection can only store objects.
Characteristics of collection class: collection is only used to store objects, the length of collection is variable, and collection can store different types of objects.
In the above class diagram, the solid line border is the implementation class, such as ArrayList, LinkedList, HashMap, etc., the broken line border is the abstract class, such as abstractcollection, abstractlist, abstractmap, etc., and the dotted line border is the interface, such as collection, iterator, list, etc.
1. Iterator interface
Iterator interface, which is used to traverse the elements in the collection, mainly includes hashnext(), next(), remove(). Its sub interface linkediterator adds three methods to it, namely add(), previous(), hasprevious(). In other words, if the iterator interface is used first, the elements in the set can only be traversed later. The traversed elements will not be traversed. Generally, the unordered set implements this interface, such as HashSet and HashMap; For those ordered sets of elements, the linkeditor interface is generally implemented. The set implementing this interface can traverse in both directions. You can access the next element through next() and the previous element through previous(), such as ArrayList.
Use of abstract classes. If you want to implement a collection class yourself, it will be very troublesome and heavy workload to implement those abstract interfaces. Abstract classes can be used at this time. These abstract classes provide us with many ready-made implementations. We only need to rewrite some methods or add some methods according to our own needs to implement the collection classes we need, which greatly reduces the workflow cost.
2. Collection (the largest interface of a collection) inheritance relationship
- list can store duplicate content
-- set cannot store duplicate contents, so duplicate contents are distinguished by hashcode() and equals()
-- queue interface
-- sortedset can sort the data in the set
Collection defines the common functions of the collection framework.
The parameter type of the add method is object. To receive objects of any type.
All stored in the collection are references (addresses) of objects.
3. Common subclasses of list
Unique methods. All methods that can operate angle markers are unique to the system.
-- the ArrayList thread is unsafe and the query speed is fast
-- vector is thread safe but slow and has been replaced by ArrayList
-- LinkedList linked list results, fast addition and deletion
4. Set interface
Set: elements are out of order (the order of storage and retrieval may not be the same), and elements cannot be repeated. -- HashSet: the underlying data structure is a hash table. Is thread unsafe. Out of sync. How does HashSet ensure element uniqueness? This is done through two methods of the element, hashcode and equals. If the hashcode value of the element is the same, whether equals is true will be judged. If the hashcode value of the element is different, equals is not called.
Note that for operations such as determining whether an element exists or deleting it, the dependent methods are the hashcode and equals methods of the element.
――TreeSet:
Orderly storage: TreeSet thread is unsafe and can sort the elements in the set set
Ensure the uniqueness of elements through CompareTo or compare methods. Elements are stored in the form of binary tree.
5. Object class
In actual development, we often encounter the problem of distinguishing the same object. A complete class is best to override the three methods of hashcode (), equals (), and toString () of the object class.
6. Output of collection
-- four common output modes
- iterator: iterative output, the most used output mode
-- listiterator: the sub interface of iterator, which is specially used to output the contents in the list
――Enumeration
――foreach
During iteration, the elements in the collection cannot be manipulated through the methods of the collection object, because the concurrentmodificationexception exception will occur. Therefore, when using the iterator, you can only use the iterator's let go operation elements, but the iterator method is limited. You can only judge, take out and delete elements. If you want other operations such as adding and modifying, you need to use its sub interface, listiterator. This interface can only be obtained through the listiterator method of the list collection.
7. Map interface
The correction, set and list interfaces are single value operations, and each element in the map is stored in the set in the form of key - > value.
Map collection: this collection stores key value pairs. One on one. And ensure the uniqueness of the key.
8. Common subclasses of map interface
Map -- HashMap: the bottom layer is the hash table data structure, which allows the use of null values and null keys. The collection is asynchronous. Replace hashtable with jdk1 2. High efficiency. -- treemap: the bottom layer is a binary tree data structure. The thread is out of sync. It can be used to sort the keys in the map collection.
9. Collection tool class
Collections: tool class for the collection framework. It defines static methods.
What is the difference between collections and collections? Collection is a top-level interface in the collection framework, which defines the common methods of single column collections. It has two commonly used sub interfaces -- List: both have defined indexes for elements. Orderly. Elements can be repeated. -- Set: cannot duplicate elements. Disorder.
Collections is a tool class in the collection framework. The methods in this class are static. The methods provided include sorting the list set, binary search, etc. Commonly used collections are thread unsafe. Because we need to improve efficiency. When multithreading operates these collections, you can convert thread unsafe collections into safe collections through the synchronization method in the tool class.
10. Comparison
11. Summary:
List:add/remove/get/set。
1. ArrayList: in fact, it is an array with a large capacity. Frequent addition and deletion is a nightmare. It is suitable for random search;
2. LinkedList: push / [pop|remove|pull] is added. In fact, it is removefirst;
3. Vector: a legacy of history. The code of the synchronized version of ArrayList is too similar to that of ArrayList;
4. Stack: inherited from vector. In fact, there is no pure stack in Java. You can implement it yourself. You can encapsulate the LinkedList in a combined way;
5. Queue: it was originally a separate category, but LinkedList is used to provide this function in sun's JDK. The main method is offer / pull / PEEK, so it comes here.
Set:add/remove。 You can use an iterator or convert it to a list.
1. HashSet: implemented internally by HashMap;
2. Linkedhashset: implemented by LinkedHashMap;
3、TreeSet:TreeMap。
Map:put/get/remove。
1. HashMap / hashtable: hash table, like ArrayList, is implemented by array. Exceeding the initial capacity will cause performance loss;
2. LinkedHashMap: inherited from HashMap, but by overriding the nested class HashMap Entry implements the linked list structure, which also has the problem of capacity;
3. Properties: inherited hashtable.
By the way, arrays Aslist, the implementation of this method depends on a nested class, which is also called ArrayList!
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.