Summary of Collection and Map Collection classes, collectionmap
We know that the most basic data structure is Array, but the Array is fixed in length, difficult to read and modify, so the java Collection framework provides us with a set of powerful functions, interfaces and classes used in java. util package.
The following is a summary of commonly used List, Set, and Map interfaces.
Overall Structure
Briefly draw the structure of common centralized Sets
Differences between interfaces
Collection: The elements are unordered and not unique (may be repeated );
List: The elements are not unique (may be repeated) and the order (insertion order) is ensured );
Set: elements are not repeated, but unordered.
Map: key-value pair, key-value, and key are set, which are not repeated.
ListArrayList and lifecycle list
During construction, I usually specify the type (generic or), or do not specify the type, but do not specify the type as the Object (root class) by default, and forcibly convert each read, if the type is put incorrectly, no compilation error is reported. There is no significant difference between the two, and the generic will be erased during running.
// If the type is not specified, forced conversion is required during extraction, and List dog = new ArrayList (); dog is prone to errors. add ("d"); dog. add (10); System. out. println (String) dog. get (0); System. out. println (dog. get (1) instanceof Integer); for (Object object: dog) {System. out. println (object. toString ());}
List common methods:
Boolean add (Object o): add elements sequentially at the end of the list, starting from 0.
Addall (), removeAll
Void add (int index, Object o): specify the location to add, do not exceed the index range.
Int size (),
Object remove (int index); boolean remove (Object o)
Boolean contains (Object o );
Object get (int index)
Clear ()
The linked list is implemented by using some special methods.
AddFirst, addLast, getFirst, getLast, removeFirst, removeLast
The difference is that ArrayList is a linear table (array) get () to directly read the first few subscript, complexity O (1) add (E) to add elements, add them directly to the back, complexity O (1) add (index, E) add elements and insert them after the first few elements. The following elements need to be moved backward. complexity O (n) remove () deletes the elements, the following elements need to be moved one by one. The complexity O (n) Complexity list is the get () Operation of the linked list to obtain the first elements, traverse them in sequence, and the complexity O (n) add (E) after adding the complex O (1) add (index, E) to the end, you need to find the first few elements and direct the pointer to the operation. complexity O (n) remove () deletes elements, direct pointer to operations, complexity O (1) Set
Set ensures that the data is not repeated. If the add operation is repeated, false is returned;
Sequence of TreeSet and HashSet
The TreeSet is ordered and the HashSet is unordered.
Inheritance
TreeSet implements treeMap and hashSet implements HashMap. However, because it inherits the Set, it is a single column and non-repeated element Set.
Underlying Storage
HashSet uses the structure storage of the hash table. When we judge the repetition, we rewrite the hashCode () and equals () methods. Different hashcodes have different elements.
TreeSet is the storage method of TreeMap's binary tree structure. It can sort the elements of the Set in a tree with excellent performance. It can be ensured by repeatedly inheriting the compareTo method from the Compareable interface.
Map
The list set search element should first know the array subscript, and sometimes cannot meet some functions. map provides key retrieval, which is more suitable for some program requirements of classified statistics and data cache. The "list hash" structure is actually a combination of lists and arrays.
Common Methods
Object put (key, value ),
Object get (key ),
Object remove (key ),
Int size (),
Set keySet (): The set Set set of the Return key
Collection values (), a set of returned values
Boolean containsKey (key)
Differences between HashMap and HashTable
HashMap keys can be null, and up to one key is null. Multiple keys will be overwritten. hashTable cannot be null.
HashMap data is not synchronized, and threads are not secure. You can use Collections. synchronizedMap (new HashMap <> () returns a new Map for thread synchronization. You can also refer to ConcurrentHashMap (available only after 1.5)
HashTable thread synchronization, thread security, but also low performance.
Refer:
Http://blog.csdn.net/geduo_83/article/details/20613233
Http://blog.csdn.net/x_ I _y_u_e/article/details/46372023