Java containers can be divided into two main categories:
Collection
* List
ArrayList
LinkedList
Vector (understood, obsolete)
* Set
HashSet
TreeSet
Linkedhashset
Map
* HashMap
* TREEMAP
Linkedhashmap
Concurrenthashmap
Hashtable (understood, obsolete)
The difference between a ArrayList and a vector
Common:
- All two of these classes implement the list interface, which is an ordered collection (stored in order) and an array at the bottom. We can remove an element by the position index number, allowing the element to repeat and be null.
Difference:
Synchronization:
- ArrayList Non-synchronous
- Vector is synchronous.
- Even when synchronization is required, we can use the Collections tool class to build a synchronized ArrayList without vectors
Expansion Size:
- The vector grew 0.5 times times as much as the original ArrayList growth
Ii. the difference between HashMap and Hashtable
Common:
- From the storage structure and implementation is basically the same, is to implement the map interface ~
Difference:
Synchronization:
- HashMap Non-synchronous
- Hashtable is synchronized.
- Need to synchronize, we often do not use, and use concurrenthashmapconcurrenthashmap based on JDK1.8 source analysis
Whether to allow null:
- HashMap allow null
- Hashtable not allowed for null
Contains method
- This knowledge point is in the cattle net brush, did not expect this problem will have (I do not like it) ....
- Hashtable has contains method
- HashMap hashtable contains method removed, changed to Containsvalue and ContainsKey
Inheritance differs:
- Hashmap<k,v> extends Abstractmap<k,v>
- public class Hashtable<k,v> extends dictionary<k,v>
Iii. the difference between list and map
Common:
- Are common Java containers, are interfaces (PS: Write to feel like and not write the same ...)
Different points:
The elements in the set cannot be duplicated, so what is the method used to distinguish between repetition or not? Are you using = = or equals ()?
We know that the set collection actually uses the put method of the Map collection to add elements .
Take HashSet as an example, the elements in the hashset can not be duplicated in the source code (HASHMAP) is the embodiment of:
//1. If key is equal if(P.hash = = Hash &&((k= p.key) = = Key | | (Key! =NULL&&Key.equals (k)))) E=p; //2. Modify the corresponding value if(E! =NULL) {//existing mapping for keyV OldValue =E.value; if(!onlyifabsent | | oldValue = =NULL) E.value=value; Afternodeaccess (e); returnOldValue; }
When adding an element, if key (which also corresponds to the elements of the set set) is equal, then the value is modified. In the set set, the value is simply an object ( the object is useless to the set itself ). that is, the set set is not inserted at all if the element is added (only a useless value is modified)! From the source code (HASHMAP) also see,= = and Equals () methods are used !
V. The difference between collection and collections
- Collection is the parent interface of a collection, inheriting its set and list interfaces.
- Collections is a collection of tool classes that provide a series of static methods for searching, finding, synchronizing, and so on for collections.
Vi. describe the storage performance and features of Arraylist,linkedlist
The bottom of the ArrayList is an array, and the bottom of the LinkedList is a doubly linked list.
- ArrayList it supports indexing of corresponding elements (random access) with the position of a corner, while LinkedList needs to traverse the entire list to get the corresponding element. So generally speaking, the speed of ArrayList is faster than the LinkedList;
- ArrayList because it is an array, for the deletion and modification of the consumption is relatively large (copy and move the array implementation), LinkedList is the two-way list deletion and modification only need to modify the corresponding pointer, the consumption is very small. Therefore generally linkedlist the deletion speed is to be quicker than the ArrayList.
6.1 Extensions:
ArrayList's additions and deletions are not necessarily slower than LinkedList.
- If both additions and deletions are at the end of the Operation "Remove () and add () are called each time, ArrayList does not need to move and copy the array to do this." If the amount of data is millions, the speed will be faster than the LinkedList.
If the location of the delete operation is in the middle . Since the consumption of LinkedList is mainly on the traverse, the consumption of ArrayList is mainly on the move and copy (The Arraycopy () method is called by the bottom, and the native method).
- LinkedList's traversal speed is slower than the ArrayList copy movement speed.
- If the amount of data is millions, ArrayList is fast .
Vii. differences between the enumeration and iterator interfaces
I did not tell them in detail in the previous article, but I probably know that iterator replaced Enumeration,enumeration as an old iterator.
Iterator is more secure than enumeration, because when a collection is being traversed, it blocks other threads from modifying the collection .
- When we do the exercises, the iterations often go wrong, throwing concurrentmodificationexception exceptions, saying that we are still modifying the elements as we traverse.
- This is actually the fail-fast mechanism.
The difference is three points:
- Iterator's method name is more scientific than enumeration.
- Iterator has a fail-fast mechanism, safer than enumeration.
- Iterator can delete elements, enumeration cannot delete elements
Viii. What are the characteristics of listiterator
- Listiterator inherits the iterator interface, which is used to traverse the elements of the list collection .
- Listiterator can implement bidirectional traversal, add elements, set elements
Nine, what is the concurrent collection class?
The Java1.5 and bundle (java.util.concurrent) contains a thread-safe collection class that allows the collection to be modified at iteration time .
The key value of HashMap in Java if it is a class object, what conditions does the class need to meet?
you need to override both the Hashcode () method of the class and its Equals () method .
- From the source can be learned that when inserting elements is the first to calculate the object's Hashcode. If the hashcode is equal. Then it indicates that the object is stored in the same location.
- If the Equals () method is called, two keys are the same, the element is replaced
- If the Equals () method is called and the two keys are different, then the hashcode is just the same , it is a hash conflict, and the new element is placed on the bucket.
In general, we would assume that as long as the values of the member variables of the two objects are equal, then we think the two objects are equal ! Because the object is compared to the address of two objects, and for our development this is not very significant ~ this is why we have to rewrite the equals()
method. overriding the Equals () method will override the method of Hashcode (). Because equals () determines that the two objects are the same, and the same object calls the Hashcode () method , it should return the same value!
Xi. What are the best practices related to the Java collection framework?
- Determine the type of collection as needed . If it is a single-column collection, we consider using sub-interfaces ArrayList and set under collection. If it's a mapping, we'll consider using map~
After determining the type of our collection, we'll next determine which subclass of the collection type to use ~ I think it's easy to break it down into several steps:
Whether synchronization is required
- To find a thread-safe collection class using
Whether the iterations need to be ordered (insert order)
- To find the linked two-way list structure
Whether sorting is required (natural order or manual sort)
- To find the tree-red-black-type (JDK1.8)
- Estimating the amount of data stored in a collection, whether it's a list or a map, is a performance drain for dynamic growth. Giving a reasonable capacity at the initial set will reduce the consumption of dynamic growth
- use generics to avoid classcastexception at run-time
- Use the Collections tool class whenever possible, or get a read-only, synchronous, or empty collection instead of writing your own implementation . It will provide code reuse, which has better stability and maintainability
12, ArrayList set add 10,000 data, how to improve efficiency
The default initial capacity of the ArrayList is 10, and it is necessary to continuously expand when large amounts of data are inserted, while the expansion is very performance-impacting. So now that we have 100,000 data, we can set the ArrayList capacity directly at the time of initialization !
This will increase efficiency.
13. Summary
This article as a summary of the collection, using the following framework diagram as a summary of this article. (Images can be saved to a local view or opened in a new tab)
Refer to "Java phoenixmiles" public number in the original
Summary of Java Collections