Big family, big family

Source: Internet
Author: User

Big family, big family

In writing Java programs, apart from the eight basic data types and the String object, we also use a collection class, which is filled with collection classes in our programs! Java's integration of big family members is too rich, there are commonly used ArrayList, HashMap, HashSet, also do not commonly used Stack, Queue, thread-safe Vector, HashTable, there are also thread-insecure referers list, TreeMap, and so on!

 

The figure above shows the members of the entire collection family and their relationships. The following describes the interfaces and base classes (mainly introduces the features of each set. Differences), more detailed presentations will be explained one by one in the near future.

1. Collection Interface

The Collection interface is the most basic Set interface and does not provide direct implementation. The classes provided by the Java SDK are the "sub-interfaces" inherited from the Collection, such as List and Set. Collection represents a rule. All elements contained in a Collection must follow one or more rules. For example, some allow duplicates, while others do not. Some must be inserted in order, while some are hash. Some support sorting, but some do not.

In Java, all classes that implement the Collection interface must provide two sets of standard constructor functions. One is a non-parameter which is used to create an empty Collection, A constructor with parameters of Collection is used to create a new Collection, which has the same elements as the imported Collection.

Ii. List Interface

The List interface is a Collection direct interface. List represents an ordered Collection, that is, it maintains the element order with a specific insert order. You can precisely control the insert position of each element in the list, access the element based on the integer index of the element (in the list), and search for the element in the list. A collection of List interfaces is mainly used: ArrayList, List, Vector, and Stack.

2.1. ArrayList

ArrayList is a dynamic array and the most common set. It allows any element that complies with the rules to insert or even include null. Each ArrayList has an initial capacity (10), which represents the size of the array. As the elements in the container increase, the container size also increases. Each time an element is added to a container, a capacity check is performed. When an overflow occurs, an expansion operation is performed. Therefore, if we determine the number of inserted elements, it is best to specify an initial capacity value to avoid excessive resizing operations and wasting time and efficiency.

The size, isEmpty, get, set, iterator, and listIterator operations are all run at a fixed time. The add operation runs at a scheduled time, that is, it takes O (n) Time to add n elements, therefore, it is not just as simple as adding an element to allocate a fixed time overhead ).

ArrayList is good at random access. Meanwhile, ArrayList is not synchronous.

2.2. shortlist

In the same way, ArrayList is a dynamic array and a two-way linked List is different from ArrayList. Therefore, in addition to the basic operation method of ArrayList, it also provides get, remove, and insert methods at the beginning or end of the lateral list.

Because of different implementation methods, the consumer list cannot be accessed randomly, and all its operations must be performed according to the needs of the dual linked list. The index operation in the list traverses the list from the beginning or end (from the end near the specified index ). The advantage of this is that you can insert or delete a List at a lower cost.

Like ArrayList, The History List is not synchronized. If multiple threads access a List at the same time, they must implement access synchronization by themselves. One solution is to construct a synchronized List when creating a List:

List list = Collections. synchronizedList (new Collections List (...));

2.3, Vector

Similar to ArrayList, but the Vector is synchronized. Therefore, Vector is a thread-safe dynamic array. Its operation is almost the same as that of ArrayList.

2.4 Stack

Stack inherits from Vector to implement a post-import, first-out Stack. Stack provides five additional methods to make the Vector used as a Stack. The basic push and pop methods also include the elements of the peek method to get the top of the stack. The empty method tests whether the stack is empty. The search method checks the position of an element in the stack. The Stack is empty after being created.

Iii. Set Interface

Set is a Collection that does not contain repeated elements. It maintains its own internal sorting, so random access makes no sense. Like List, it also runs null but only one. Due to the special nature of the Set interface, all elements in the Set must be different. At the same time, pay attention to any variable object. If you operate on elements in the Set, e1.equals (e2) = true. The Set interfaces include EnumSet, HashSet, and TreeSet.

3.1. EnumSet

Is the dedicated Set of enumeration. All elements are of the enumeration type.

3.2. HashSet

HashSet is the set with the fastest query speed, because it is implemented by HashCode internally. The order of its internal elements is determined by the hash code, so it does not guarantee the set iteration order; in particular, it does not ensure that the order remains unchanged.

3.3. TreeSet

Based on TreeMap, a set that is always in the sorting state is generated, which is implemented by TreeMap internally. It sorts elements in the natural order of the elements, or providesComparatorSort, depending on the construction method used.

Iv. Map Interface

Unlike the List and Set interfaces, Map is a Set of key-Value pairs that provide key-Value ing. At the same time, it does not inherit Collection. In Map, it ensures a one-to-one correspondence between keys and values. That is to say, a key corresponds to a value, so it cannot have the same key value. Of course, the value can be the same. Map implementation includes HashMap, TreeMap, HashTable, Properties, and EnumMap.

4.1. HashMap

It is implemented based on the data structure of a hash table. The location of an object is calculated using the hash function. It is designed for fast query, an Entry [] table is defined internally. An element converts the hash address of an element to the index stored in the array through the hash conversion function. If there is a conflict, then, all elements with the same hash address are serialized in the form of a hashed linked list. You may view the HashMap. the source code of Entry is a single-chain table structure.

4.2 TreeMap

The key is sorted by certain sorting rules, and the red-black (red-black) tree data structure is used internally to implement the SortedMap interface.

4.3. HashTable

It is also implemented based on the data structure of the hash table. In the case of conflict resolution, HashMap uses the form of a hash linked list, but the performance is lower than that of HashMap.

4.4, WeakHashMap class

WeakHashMap is an improved HashMap that implements "weak references" to keys. If a key is no longer referenced by external entities, it can be recycled by GC.

V. Queue

The queue is mainly divided into two categories. One is a blocking queue. When the queue is full, an exception will be thrown when elements are inserted again, including ArrayBlockQueue, PriorityBlockingQueue, and LinkedBlockingQueue. Another queue is a dual-end queue that supports inserting and removing elements at the header and tail ends, including ArrayDeque, LinkedBlockingDeque, and shortlist.

6. Similarities and Differences 6.1, Vector and ArrayList

1. vector is thread-synchronous, so it is thread-safe, while arraylist is asynchronous and insecure. If thread security is not taken into account, arraylist is generally used for high efficiency.

2. If the number of elements in the set is greater than the length of the current set array, the growth rate of vector is 100% of the current array length, and that of arraylist is 50% of the current array length. if you use a large amount of data in a collection, using vector has some advantages.

3. If you look for data at a specified position, the time used by the vector and arraylist is the same, both of which are 0 (1). In this case, you can use both vector and arraylist. If it takes 0 (n-I) n to move the data at a specified position as the total length, you should consider using linklist, because it takes 0 (1) to move data at a specified location, and the time spent querying data at a specified location is 0 (I ).

ArrayList and Vector use arrays to store data. The number of elements in the array is greater than that in the actual storage to add and insert elements. Both allow direct serial number index elements, however, data insertion is designed to move array elements and other memory operations. Therefore, index data is inserted slowly. Because Vector uses the synchronized Method (thread-safe), its performance is inferior to that of ArrayList, the sort list uses a two-way linked list for storage. Data indexed by serial number needs to be traversed forward or backward. However, when inserting data, you only need to record the items before and after this item, so the insertion speed is faster!

6.2. categorylist and categorylist

1. ArrayList is a dynamic array-based data structure. The ArrayList is based on the linked list data structure.

2. for Random Access to get and set, ArrayList thinks it is better than the sorted list because the sorted list needs to move the pointer.

3. For add and remove operations, LinedList is dominant because ArrayList needs to move data.

This depends on the actual situation. If only a single piece of data is inserted or deleted, the ArrayList speed is better than the sort list speed. However, if data is inserted and deleted randomly in batches, the speed of the sorted list is much higher than that of the ArrayList. Because each inserted data in the ArrayList needs to move the inserted point and all subsequent data.

6.3. HashMap and TreeMap

1. HashMap uses hashcode to quickly search its content, while all elements in TreeMap maintain a fixed order, if you need to get an ordered result, you should use TreeMap (the arrangement order of elements in HashMap is not fixed ). The order of elements in HashMap is not fixed ).

2. HashMap uses hashcode to quickly search its content, while all elements in TreeMap maintain a fixed order, if you need to get an ordered result, you should use TreeMap (the arrangement order of elements in HashMap is not fixed ). The Collection framework provides two general Map implementations: HashMap and TreeMap (the SortedMap interface implemented by TreeMap ).

3. insert, delete, and locate elements in Map. HashMap is the best choice. However, if you want to traverse keys in the natural or custom order, it is better to use TreeMap. The key classes required to be added using HashMap clearly define the implementation of hashCode () and equals. This TreeMap has no optimization options because the tree is always in the balance state.

6.4. hashtable and hashmap

1. Historical Reasons: Hashtable is based on the obsolete Dictionary class, And HashMap is an implementation of the Map interface introduced by Java 1.2.

2. Synchronization: Hashtable is thread-safe, that is, synchronous, while HashMap is not secure or synchronous.

3. value: Only HashMap allows you to use a null value as the key or value of a table entry.

7. Set selection 7.1 and List selection

1. for random queries and iterative traversal, arrays are faster than all containers. Therefore, ArrayList is generally used for random access.

2. The two-way linked list provides excellent support for adding and deleting elements. Element displacement is required for adding and deleting elements in the ArrayList.

3. We generally avoid using Vector.

4. Use ArrayList as the first choice. After all, we traverse all set elements. When the performance of the program decreases due to frequent List insertion and deletion, we should consider the Skip List.

7.2 Set selection

1. Because HashSet is implemented using HashCode, its performance is always better than TreeSet to some extent, especially for adding and searching operations.

3. Although TreeSet does not have a good HashSet performance, it can still be useful because it can maintain the sorting of elements.

7.3 select Map

1. Like HashSet, HashMap supports fast query. Although the HashTable speed is not slow, it is still a little slow in front of HashMap, so HashMap can replace HashTable in query.

2. Because TreeMap needs to maintain the order of internal elements, it is generally slower than HashMap and HashTable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.