An overview of the collections collection.
Preface
I feel very confused when I suddenly encounter the interview questions about sets, so I specially summarize some differences and usages of our commonly used ArrayList, LinkedList, set and other sets.
Moreover, this mini summary must have been picked up from many blogs, because I am still a rookie, unable to write my own views, or forgive me in my learning journey.
Reference blog:
http://shmilyaw-hotmail-com.iteye.com/blog/1595399
http://www.cnblogs.com/nayitian/p/3266090.html#collectionframe
The great gods really have a lot to learn. The more they learn, the more they find their ignorance. Thank you for standing on the shoulders of giants.
Personal idea: This is just a preliminary summary. Later articles will explain each collection in detail, such as looking at the source code and how to implement it internally, so as to deepen your understanding of these collections
、
------WZY
1、 Brief description
This is a diagram of the overall relationship of collection. It's huge. I'll pick the ones we often use to explain.
Generally speaking, the collection category is divided into two categories,
1: collection classes that are accessed iteratively in a certain order
2: a collection class that accesses relationships according to key values such as key and value
2、 The first is the inheritance diagram of the collection class accessed iteratively in a certain order,
Set interface: no sequence and cannot be repeated.
HashSet class: it is convenient for data query. The added elements should pay special attention to the implementation of hashcode () method, which is not synchronous. When multiple threads access the same HashSet object, they need to be synchronized manually
Linkedhashset class: determines the storage location of elements according to the hashcode value of elements, but it also uses a linked list to maintain the order of elements. Compared with HashSet, it is convenient for data addition and deletion,
These two are like ArrayList and LinkedList. One is convenient for query and the other is convenient for addition and deletion
Sortset class is the implementation class of sortedset interface: the class name is used for sorting and other functions. I will explain them in more detail in the following articles.
Enumset class: a collection class designed specifically for enumeration classes. All elements in enumset must be enumeration values of the specified enumeration type
List interface: sequential and repeatable
LinkedList class: internally uses the form of linked list to store data, which has better performance in adding and deleting data
It implements the list interface and deque interface, which shows that it has the characteristics of two interfaces, so it can be used as a double ended queue or as a stack, and it is implemented in the form of linked list, so the query performance is poor, but the addition and deletion performance is high.
ArrayList class: array is used internally to store data, which is equivalent to the sequential table storage of data structure. It has good performance in querying data,
Vertor class: compared with ArrayList, it is thread safe, while ArrayList is thread unsafe,
Stack class inherits vertor class: depending on the name, it is actually convenient to simulate the data structure of "stack"
Queue interface: it is used to simulate the data structure of queue, and then the interface declares some basic operation methods. For example: add, offer, remove, etc
PriorityQueue class: the order in which PriorityQueue saves queue elements is not the order in which they are added to the queue, but reordered according to the size of queue elements
Deque interface: deque represents a double ended queue, which can be used as a double ended queue or as a "stack", because it includes out of stack pop() and in stack push() methods.
Arraydeque class is the implementation class of deque: that is, it implements the methods defined in the deque interface, and the interpretation is similar to that of deque
Various linear table selection strategies
3、 The second is a collection class accessed in the form of key and value pairs.
Map interface: defines some basic operations, such as put (key, value), containkey (object key) and a series of operations.
Compared with hashtable class, HashMap class is generally used, because hashtable class is very old, which is not recommended according to investigation. Usually we also use HashMap
HashMap class is thread unsafe, while hashtable is thread safe
HashMap class can use null as key and value, while hashtable cannot
The properties class inherits the hashtable class
Additional methods are added, such as load (InputStream, instream) and loading key value from the property file.
You can save the key value in the format of XML file, which may be dealing with XML files
The LinkedHashMap class inherits the HashMap class
LinkedHashMap inherits from the HashMap class. Maintain internal order with linked list. It is similar to linkedhashset in many aspects. LinkedHashMap can remember the order in which key value pairs are added, while avoiding the performance impact when using treemap.
SortedMap interface and treemap implementation class
Similar to sortedset and TreeSet, Treemap can also customize the comparator (comparable) implements custom sorting. Its additional methods are similar to TreeSet. It adds methods to access the first, previous, last and last key value pairs, and provides methods to extract subsets from treemap. Treemap does not allow null as a key, otherwise how to compare?
Identityhashmap class
Unlike HashMap, identityhashmap considers two keys to be equal only when two keys are strictly equal (key1 = = key2); For ordinary HashMap, as long as key1 Equals (key2) and the hashcode is the same. Similarly, null values are allowed, and the order cannot be guaranteed.
Enummap class
Enummap is a map implementation used with enumeration classes. Its key must be the enumeration value of a single enumeration class. Enummap does not allow null as a key, but can be used as a value.
Various map implementation class selection strategies