Commonly used data structures in Java
A set of frames is mainly collection and map:
1, collection contains the list and set two major branches.
(1) List is an ordered set, and each element has its index. The index value of the first element is 0. The list implementation class has Arraylist,linkedlist, Vector, Stack.
(1) ArrayList
ArrayList is a dynamic array that allows any element that conforms to the rule to be inserted including NULL. Each ArrayList has a size=10, each time adding elements to the container will be a capacity check, once the capacity is found to be insufficient, will automatically expand capacity, the new size is 1.5 times times the original capacity.
If there are 20 data that need to be added, the capacity of ArrayList will be changed to 10 (one) at the first time, then the expansion will increase by 1.5 times times. That is, when the 11th data is added, ArrayList continues to expand to 10*1.5=15 (such as two); When the 16th data is added, the continuation capacity becomes 15 * 1.5 = 22
ArrayList specializes in random access. At the same time ArrayList is not synchronous.
(2) LinkedList
ArrayList is a dynamic array, and LinkedList is a doubly linked list. So it provides an additional Get,remove,insert method at the first or the end of the LinkedList in addition to the basic operating method of the ArrayList.
LinkedList cannot be accessed randomly, and all of its operations are performed according to the needs of the doubly linked list. An operation indexed in a list traverses the list from the beginning or end (from one end near the specified index). The advantage of this is that you can insert and delete operations in the list at a lower cost.
As with ArrayList, LinkedList is also unsynchronized. If multiple threads access a list at the same time, you must implement access synchronization yourself. One workaround is to construct a synchronized list:list list = collections.synchronizedlist (new LinkedList (...) when the list is created;
If you need fast access to data, you should use arrays for little or no insertion and deletion of elements, but if you need to insert and delete elements frequently, you need to use a linked list data structure.
(3) Vector
Similar to ArrayList, Vector is also a dynamic array, but vector is synchronous, so vector is a dynamic array of thread safety, because thread synchronization must affect performance, so the performance of ArrayList is better than vector.
Vector expansion will be doubled its capacity, and ArrayList only increase the size of 50%, so that ArrayList is conducive to saving memory space, the development of the time if the performance, memory space requirements high that with ArrayList, If it's a safety requirement, use a vector.
(4) Stack
Stack inherits from Vector and implements a last-in-first-out stack. The stack provides 5 additional ways to make the vector available as a stack. The basic push and pop methods, and the Peek method to get the stack top element, the empty method tests if the stack is empty, and the search method detects the position of an element on the stack. Stack has just been created as an empty stack.
(2) Set is a set that is not allowed to have duplicate elements. The implementation class for set has Hastset and TreeSet.
* HashSet: A hash table stores information by using a mechanism called hashing, and the elements are not stored in a particular order;
* Linkedhashset: Maintains a linked table of the collection in the order in which the elements are inserted, allowing iterations in the collection in the order in which they are inserted;
* TreeSet: Provides an implementation that uses a tree structure to store set interfaces, objects are stored in ascending order, and the time to access and traverse is fast.
2, map is a mapping interface, that is, key-value key value pairs. Each element in the map contains "one key" and "key corresponding to value". Key cannot be the same, value can be the same
Hashtable and HashMap
Same point:
(1) All realized the Map<k,v>, cloneable, Serializable interface.
(2) is a hash table that stores "key-value pairs (key-value)", and they are all implemented using the Zipper method.
Different points:
(1) Synchronization: Hashtable is thread-safe, that is, synchronous, and HashMap is a thread program is not secure, not synchronous.
(2) Null value processing: HashMap key, value can be null,hashtable key, value is not null.
(3) The base class is different: HashMap inherits from Abstractmap, and Hashtable inherits from dictionary.
(4) The supported traversal types are different: HashMap only supports iterator (iterator) traversal. Hashtable supports both iterator (iterators) and enumeration (enumerators) traversal.
Java Data structure Simple point