Structure diagram Collection (interface) | &N Bsp ---------------------------------------------------------------------- | , &NB Sp , &NB Sp | Lis T (interface, can be same element, ordered) &N Bsp Set (interface, not the same element)------------------------------------- , &NB Sp ---------------------------------------| , &NB Sp | , &NB Sp | , &NB Sp | ArrayList (no sync, nullable) LinkedList (no sync, nullable) hashset &N Bsp TreeSet
Map (interface)|--------------------------| | HashMap Hashtable★vector:
SyncOf Vectors are very similar to ArrayList. ★hashmap: Non-synchronous, and allows null, that is, null value and null key. However, when HashMap is considered collection (the values () method returns collection), its iterator operation time overhead is proportional to the capacity of HashMap. Therefore, if the performance of the iterative operation is quite important, do not set the initialization capacity of the hashmap too high or load factor too low. ★hashtable: Is synchronous, any non-null (Non-null) object can be either a key or a value. Add data using put (key, value), take out the data using get (key), the time overhead for these two basic operations is constant. The Hashtable adjusts performance through the initial capacity and load factor two parameters. ★linkedlist: There is no synchronization method. 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; Summarize
- If it involves operations such as stacks, queues, and so on, you should consider using the list, for quick insertions, for deleting elements, should use LinkedList, and if you need to quickly randomly access elements, you should use ArrayList.
- If the program is in a single-threaded environment, or if access is done only in one thread, it is more efficient to consider non-synchronous classes, and if multiple threads may operate on a class at the same time, the synchronized classes should be used.
- Pay special attention to the operation of the hash table, and the object as key should correctly replicate the Equals and Hashcode methods.
- Try to return the interface rather than the actual type, such as returning a list instead of ArrayList, so that if you need to change ArrayList to LinkedList later, the client code does not have to be changed. This is for abstract programming.
Note: Thread safety or not, see if synchronization. Thread-Safe if it is synchronized, otherwise the thread is not secure.
general differences between ArrayList and LinkedList:1.ArrayList is the realization of the data structure based on dynamic array, LinkedList data structure based on linked list. 2. For random access get and set,arraylist feel better than LinkedList, because linkedlist to move the pointer. 3. Add and Remove,linedlist are the dominant for new and deleted operations because ArrayList is moving the data.
General Advantages and disadvantages of ArrayList and LinkedList:1. For ArrayList and LinkedList, the overhead of adding an element at the end of the list is fixed. For ArrayList, the main point is to add an entry in the internal array, pointing to the element being added, which may occasionally cause the array to be redistributed, whereas for LinkedList, the overhead is uniform, allocating an internal entry object. 2. Inserting or deleting an element in the middle of a ArrayList means that the remaining elements in the list will be moved, while the overhead of inserting or deleting an element in the middle of the linkedlist is fixed. 3. LinkedList does not support efficient random element access. 4. ArrayList space waste is mainly reflected in the end of the list to reserve a certain amount of space, while the linkedlist of the space cost is reflected in its every element needs to consume considerable space.
Common implementations of the list interface subclass---VectorThe vector is an elder class, which already exists in the JDK1.0. However, after the 1.2 release, more new interfaces (such as list) are applied, so that a new subclass of the list (such as ArrayList) will be available in order to retain the functionality of the vector.
Common knowledge of collection classes in Java