List, Set, Map, and explain the difference

A, List interfaces

Collection List is inherited from the interface, that is a set of List. List are ordered queue, List Each element has an index; index of the first element is 0, the index value of the next element in the +1 order. And Set different, there are repeated elements in the List permission. Collection implementations of the List interface mainly: ArrayList, LinkedList, Vector, Stack.

ArrayList

ArrayList is a dynamic array, but also our most popular collections. It allows any element into line with the rules and even null. Each ArrayList has an initial capacity:

private static final int DEFAULT_CAPACITY = 10;

With the container elements in the increasing size of the container will also increase. Check for capacity will increase in each element to the vessel at the same time, when the fast overflows for expansion operation. So if we clear how many elements are inserted, it is best to specify an initial capacity value, avoid excessive capacity expansion and operation of a waste of time and efficiency.

size, isEmpty, get, set, iterator listIterator and operations are running at a fixed time. add operations to fix time-sharing operation, that is, adding n elements requires O (n) time (due to consider expansion, so it's not just as simple as adding elements will bring a fixed time-sharing overhead).

ArrayList good at random access. Meanwhile ArrayList unsynchronized.

LinkedList

Also different LinkedList implement the List interface with ArrayList, ArrayList is a dynamic array, whereas LinkedList is a doubly linked list. In addition it has the basic method of operation is also additionally provided ArrayList get, remove, insert method in the first LinkedList or tail.

Due to the different ways to achieve, LinkedList not random access, it needs all the operations are to be performed in a double linked list. In the index list operations through the list from the beginning or end (an end close to the specified index, saving half the time). The benefit of this is that you can insert and delete operations in the List by the lower price.

Like the ArrayList, LinkedList is asynchronous. If multiple threads access a List, they must implement their own access synchronization. One solution is a synchronization when creating List of List structure:

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

Vector

And similar ArrayList, but Vector is synchronized. So Vector is a dynamic array thread-safe. Its operation is almost the same with the ArrayList.

Stack

Stack inherited from Vector, to implement a LIFO stack. Stack provide five additional approach allows Vector to be used as a stack. The push and pop the basic method, there is obtained a method Zhanding elements peek, empty method to test whether the stack is empty, search method of detecting the position of an element in the stack. Stack after stack is empty just created.

Two, Set Interface

Set is an inherited interface Collection, Set does not include a duplicate elements Collection. It maintains its own internal order, so random access does not make any sense. And List, as it also runs the existence of null but only one. Due to the special nature of the Set interface, all incoming Set collection elements must be different, aspects of the API. Set Collection API and exactly the same. Set to achieve a set of interfaces are: HashSet, TreeSet, LinkedHashSet, EnumSet.

HashSet

HashSet called the fastest set of query speed, because its interior is HashCode to achieve. Collection elements can be null, but only into a null. Sequence elements within it is determined by the hash code, so it is not guaranteed iteration order of the set; in particular, it does not guarantee constancy of the sequence.

TreeSet

TreeSet binary tree is realized, based on TreeMap, always generate a sorted state in SET, TreeMap internally implemented into the null value is not allowed. It is natural to use the order of the elements sorting element, or sorted according to a Comparator provided at creating Set, depending on the method of construction used.

LinkedHashSet

Also LinkedHashSet set is determined according to the storage position of the element value of the element hashCode, but it also used to maintain order in the list of elements. Such that the insertion elements look like in order to save, that is, when traversing the set time, it will be to add a LinkedHashSet sequential access of elements of the set of elements. LinkedHashSet when iteration access to all elements of Set, the performance is better than HashSet, but when you insert performance slightly inferior to HashSet.

Three, Map Interface

Map and List, Set different interfaces, it is a collection of a series of key-value pairs, providing a key to the Value Mapping. In the Map It ensures one correspondence between the key and value. That is a value corresponding to a key, it can not present the same key value, the value of course may be the same value. Map collection has achieved: HashMap, HashTable, TreeMap, WeakHashMap.

HashMap

A hash table data structure to implement, its position is calculated by a hash function to find the object when it is designed for fast queries, which defines an internal hash table array (Entry [] table), the conversion element will pass the hash function converting the element into a hash address stored in an indexed array, if there is a conflict, the hash linked list is used to form all the elements of the same hash address string together, by looking HashMap.Entry source may be a single chain structure which .

HashTable

Also based on a hash table data structure implemented during conflict resolution and the same HashMap also takes the form of a hash list. HashTable inherit Dictionary class implements the Map interface. Wherein any Dictionary class can be mapped to a key (such as Hashtable) values ​​corresponding abstract parent class. Each key and each value is an object. In any Dictionary object, each key is associated with at most one value. Map is the "key-value key-value pair" interface. HashTable a "zipper" method to hash table but performance is lower than the HashMap.

TreeMap

Ordered hash table to achieve SortedMap interfaces, the underlying implementation by red-black tree.

WeakHashMap

Look at Java references before talking WeakHashMap (strength in descending order)

  1. Strong Quote: general reference object declaration, there will not GC
  2. Soft Quote: helpful but not required, before the occurrence of memory overflow, secondary recovery
  3. Weak references: can only survive until the next GC before, whether or not enough memory
  4. Phantom reference: the sole purpose of this object is able to receive a notification when the GC system

Based implementation with weak keys hash table Map. In a WeakHashMap, when a key is no longer in normal use, its entry will be automatically removed. More precisely, for a given key, which does not prevent the presence of mapping the garbage collector to discard the key, which makes the key can be terminated, is terminated, and then recovered. When a key is discarded, its entry is effectively removed from the map, therefore, this kind of behavior with other Map implementation is different. null null values ​​and keys are supported. Such HashMap class has similar performance characteristics, and performance parameters having the same initial capacity and load factor. Like most collections, this class is not synchronized.

IV Summary

1, List, Set are inherited from the Collection interface, Map is not

2, List features: element has placed the order, the elements may be repeated, Set Features: No element into the sequence of elements is not repeated, the element will overwrite is repeated, (note: although no element into the sequence, but the elements in the set of position is the element HashCode determined, the position is actually fixed, added of set Object must be defined equals () method, additional list support for loops, i.e. to traverse through the index, can also be used iterator, but set only iterative, because he was disorderly, unable to get the desired value using the index.) 

3, Set and List comparison: 

Set: Retrieving element inefficient, high efficiency of insertion and deletion, insertion and deletion of elements does not cause change in position. 

List: and an array of similar, List can dynamically grow, find elements of high efficiency, low insertion and deletion of elements efficiency, because it will lead to other elements of the position change. 

4, Map data suitable for storing key-value pairs

5, thread-safe collection classes with non-thread-safe collection classes:

  • LinkedList, ArrayList, HashSet non-thread-safe, Vector is thread-safe;
  • HashMap is not thread-safe, HashTable are thread-safe;
  • StringBuilder is not thread-safe, StringBuffer is thread-safe.

Reference links: the Java container class summary

       Java, List, Set, and Map usage scenarios and explain their differences

 

 

Guess you like

Origin www.cnblogs.com/jxxblogs/p/11561629.html