Java collection class, List and Set comparison, respective subclass comparison (ArrayList, Vector, LinkedList; HashSet, TreeSet), Map collection comparison

ArrayList , LinkedList , Vector all belong to List

 

List : The elements are in order, and the elements can be repeated because each element has its own index (index)

  |-- ArrayList: The bottom layer is an array structure, which is characterized by: the query is very fast, the addition and deletion are slightly slower, and the threads are not synchronized: A thread puts the element at the index 0 position, the CPU scheduling thread A stops, B runs, and also puts the element At index 0 , Size is programmed to 2 when A and B are running at the same time .

 

  |-- LinkedList : The bottom layer uses a linked list data structure, which is characterized by fast additions and deletions and slow queries. Thread unsafe, thread safety problems are caused by multiple threads writing or reading and writing the same resource at the same time.

 

  |--Vector: The bottom layer is the array data structure, the thread is synchronized, the synchronized keyword is added in front of the Vector method, which is replaced by ArrayList , and now only his enumeration is used.

 

Set : The elements are unordered and cannot be repeated (the order of storage and retrieval may not be the same), and the threads are not synchronized. The bottom layer of set is implemented using Map , so thread-safe Set can be implemented by way of ConcurrentHashMap .

|--HashSet : The bottom layer is the hash table data structure. Determine the uniqueness of an element according to the hashCode and equals methods.

hashCode and equals : the same function, are used to compare whether two objects are equal.

The comparison of equals is more comprehensive, and the use of hashCode () for comparison, as long as a hash value is generated for comparison for a long time, the efficiency is high.

Two objects that are equal() are equal and their hashCode() must be equal, that is, equal() is absolutely reliable.

  The equal() of two objects with equal hashCode () are not necessarily equal, and hashCode() is not absolutely reliable.

When two objects need to be compared, first use hashCode() to compare, if they are not the same, it means that the two objects are definitely not equal (there is no need to compare equal(0) ), if hashCode() is the same, then Compare equal() , if equal() is the same, then the two objects are the same.

 

  |--TreeSet : The elements in the Set collection can be sorted (natural order), the underlying data structure is a binary tree,

You can also write your own class to implement the Comparable  or  Comparator  interface, define your own comparator, and pass it as a parameter to the TreeSet constructor.

Comparable : It can be considered as an internal comparator (public int compareTo(Domain domain){  if(this.str.compareTo(domain.str) > 0) }) , the class that implements the Comparable interface has a characteristic that these classes are If you can compare with yourself, use the ComparableTo (natural comparison method) method to compare, the return value is int , the comparator and the compared: greater than return a positive number, less than return a negative number, equal return 0. ( this.str and parameter class . str comparison)

Comparator : External comparator, the method has two parameters o1 and o2 , the return value is int.  1 , o1 is greater than o2 , returns a positive integer

2 , o1 is equal to o2 , return 0.3 , o1 is less than o3 , return a negative integer

Comparison of the two: Implementing Comparable is more coupled than the Comparator interface, and Comparator is compared outside the class. You can define your own comparator and write a comparison algorithm without any modification to the implementation class. In fact, the way to implement the Comparator  interface will be written later as a typical strategy pattern.

The strategy pattern is similar to the factory pattern: reducing the coupling degree of code through polymorphism is to realize the selection of different subclasses through polymorphism.

Simple factory pattern : You only need to pass the corresponding conditions to get the desired object, and then use this object to implement the operation of the algorithm. Implements picking a class to instantiate an object.

Strategy pattern: When using, you must first create a class object you want to use, and then pass the object as a parameter, and call different algorithms through this object. The work of selecting the corresponding object is handed over to the user of the mode, and the selection work itself is not done.

In fact, the difference between the two is very subtle . The Factory directly creates a specific object and uses the object to perform the corresponding action, while the Context gives this operation to the Context class, without creating a specific object, further encapsulation of the implemented code, and client code. It is not necessary to know the specific implementation process.

 

Map : This set stores key-value pairs, one-to-one, and ensures the uniqueness of the keys ( 01 , Zhang San) This form is printed as   01= Zhang San

   |--HashTable : The bottom layer is a hash table data structure. Null keys and null values ​​cannot be stored . The collection thread is synchronized and the efficiency is relatively low. Appeared in JDK1.0 . Thread safety, use synchronized to lock the entire Hash table to achieve thread safety, that is, lock the entire table each time to make the thread exclusive.

 

   |--HashMap : The bottom layer is a hash table data structure, which can store null keys and null values. The threads are not synchronized, and the efficiency is high. Instead of HashTable , it appeared in JDK 1.2

 

   |--TreeMap: The bottom layer is a binary tree data structure, the threads are not synchronized, and can be used to sort the keys in the map collection

 

  ConcurrentHashMap : thread-safe, allowing multiple modification operations to be performed concurrently, the key is the use of lock separation technology, which uses multiple locks to control modifications to different parts of the hash table. ConcurrentHashMap internally uses segments (Segment) to represent these different parts, each segment is actually a small Hashtable , they have their own locks. As long as multiple modification operations occur on different segments, they can proceed concurrently.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326119675&siteId=291194637