Java Collection: Summary of Set, List, Queue, map four systems

Source: Internet
Author: User
Tags set set

The Java collection is broadly divided into set, List, Queue, map four systems

Where set represents an unordered, non-repeatable set, the list represents an ordered, repeating set, and a map represents a collection with a mapping relationship; queue is the implementation of queues.

Collections and arrays are different, array elements can be either primitive types or objects (which actually hold reference variables for objects), and only objects are saved in the collection (in effect, only the reference variables of the object are saved).

There are two derived interfaces in the Java collection : Collection and Map
Collection The inheritance tree of the collection system:

the inheritance tree of the Map collection system:

The following are respectively described
Set Set:
The set set is similar to a jar in which the program can "throw" multiple objects in order, and the set does not remember the sequence in which the elements are added, and the set set does not allow the same elements.

HashSet:
Characteristics:
Cannot guarantee the ordering of elements
The HashSet is not synchronous
The collection element value can be null
HashSet The criterion for determining the equality of two elements is that two objects are compared by the Equals () method and the Hashcode () method return values for two objects are equal.
Note: When you put an object into HashSet, you should override its Hashcode () method if you need to override the Equals () method of the object. The rule is: if two objects return true through the Equals () method, the Hashcode value of the two objects should be the same.

Linkedset:
Linkedset determines where the element is stored based on the value of the original hashcode, but he also maintains the order of the elements using the linked list , which preserves the order in which the elements are inserted. Linkedset accesses the elements in the collection in the order in which they are added.
Linkedset needs to maintain the insertion position of the element, so performance will be slightly lower than HashSet's performance.

TreeSet:
TreeSet can ensure that the collection element is in the sorted state.
TreeSet are not sorted according to the order in which the elements are inserted, but are sorted based on the size of the element's actual values.
TreeSet uses a red-black tree's data structure to store collection elements.
TreeSet supports two sorts of sorting methods: natural sorting and custom sorting. By default, TreeSet is sorted by nature.

Natural sort: TreeSet invokes the CompareTo (Object obj) method of the collection element to compare the size relationship between elements, and then arranges the collection elements in ascending order. By default, TreeSet is sorted by nature.
When an object is added to the TreeSet collection, TreeSet calls the object's CompareTo (Object obj) method to compare the size with other objects in the container, and then finds its storage location based on the red-black tree structure.
the only criterion for determining the equality of two objects is that two objects are compared by the CompareTo (object obj) method to return 0.
If two objects are compared by the Equals () method to return True, the two objects should return 0 by using the CompareTo (Object obj) method.

Custom sorting: If you need to implement a custom sort, you need to provide a comparator object associated with the TreeSet collection when you create the TreeSet collection object, which is responsible for the sorting logic of the collection elements.

Enumset:
The collection elements of the Enumset are also ordered, and Enumset determines the order of the collection elements in the order in which the enumeration values are located inside the enum class.
The interior of the enumset is stored as a bit vector.
The Enumset collection does not allow the addition of a null element.

performance analysis of each set implementation class:
The performance of HashSet is always better than TreeSet because TreeSet requires additional red-black tree algorithms to maintain the order of the collections.
Linkedset for normal insert and delete operations, Linkedset is slightly slower than hashset, which is caused by the additional overhead associated with maintaining the linked list. But because of the linked list, traversing Linkedset is faster.
Enumset performance is the best, but only the same enumeration class is saved as a collection element.

List:
The list represents an orderly, repeatable combination of elements, each of which has a corresponding sequential index.
The list collection can access the elements in the collection based on the location index, so the list can be traversed using a for loop.

ArrayList, linkedlist and vectors
ArrayList Source Analysis:
LinkedList Source Analysis:

Queue:
Queue is used to simulate this data structure of queues.
Priorityqueue:
Priorityqueue the order in which the queue elements are saved is not in the order in which they were added, but in the size of the queue elements.
Priorityqueue does not allow the insertion of a null element.

Deque:
The Deque interface is a sub-interface of the queue interface, which represents a double-ended queue.
It is recommended to use Arraydeque when using the "stack" data structure in your program.

performance analysis of various linear tables:
1. If you need to traverse the list collection element, for ArrayList, vector collections, you should use the random access method (get) to iterate over the collection elements, so that the performance is better, and the LinkedList collection should use Iterators (Iterator) to iterate over the collection elements.
2. If you need to perform inserts and deletions frequently, you should use LinkedList.
3. If multiple threads access the elements in the list collection at the same time, you should use collections to wrap the collection as a thread-safe collection.

Map:
The key of map does not allow repetition, that is, any two key of the same map object is always returned false by the Equals method.
There is a keyset () method in map that returns a set of all the keys in the map.

HashMap, Hashtable:
The difference between HashMap and Hashtable:
1.Hashtable is a thread-safe map,hashmap is non-thread safe, so hashmap performance is better.
2.Hashtable is not allowed to use NULL as key and VALUE,HASHMAP allows NULL as key or value.

Hashtable, HashMap judge two keys equal to the standard is: the Equals () method of the two key returns True, the hashcode value of two keys is the same, and the criterion of determining the equal of two value is the same as the Equals () method return value of value.

Linkedmap:
Linkedmap will remember the order in which Key-value are added.

TreeMap:
TreeMap also uses the structure of red and black trees, the criteria for judging the equivalence of two keys in TreeMap are:
The return value of two keys through the CompareTo () method is 0. (in natural order)
The return value of two keys through the CompareTo () method is 0. The Equals () method is also compared to return true. (under Custom sorting).

Enummap:
The enummap is stored internally as an array.
Enummap does not allow NULL as key, but it allows value to be null.

Performance Analysis of map:
The performance of HashMap is better than that of Hashtable.
The Key-value pairs in TreeMap are always in an orderly state, without the need for a special sort operation.
For general application scenarios, consider using HashMap more.
Linkedmap is slower than HashMap because the list needs to be maintained to maintain the order in which the Key-value is added.
Enummap performs best, but only the enumeration value of the same enumeration class can be used as key.

Related Article

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.