1. The difference between List and Set
Common denominator: They're all collection sub-interfaces.
Difference:
List: This interface is able to accurately record the insertion position of each element (in other words, all elements of this interface content are stored sequentially), the user can be indexed to get the value of an element, which is a bit similar to the array in Java, All data saved in the list is allowed to be duplicated .
Set: This interface cannot record the exact location of each element, and all elements in the entire collection are unordered , and the set store data collection is not allowed to have duplicate elements .
Since both list and set are interfaces, they cannot be instantiated directly, and they need to be instantiated with subclasses that implement their interfaces, thereby using the various methods provided in the interface.
The subclasses that implement the list interface include:arraylist,linkedlist and Vector.
Where ArrayList this class is an array-like collection instantiation, and the LinkedList class is an instantiation of the form of a list, the specific difference is the data structure above the difference.
The vector class is very similar to ArrayList, and the difference is that vector is a synchronous method and may throw concurrentmodificationexception when multithreaded.
The subclass that implements the set interface includes:hasset.
2, HashSet is how to ensure that the non-repetition
According to Hashset.add (e) JDK source code, found that HashSet unexpectedly is the use of hashmap to achieve, using the uniqueness of the key in the HashMap, to ensure that the hashset does not appear duplicate values.
When the Add (e) method is called, first the Hashcode method of the object is called to determine whether the hashcode already exists, and if it does not exist, insert the element directly;
If it already exists, the Equals method that invokes the object objects determines whether to return true, if true, to indicate that the element already exists and, if False, to insert the element.
Public classHashset<e>extendsAbstractset<e>ImplementsSet<e>, cloneable, java.io.serializable{Static Final LongSerialversionuid = -5024744406713321676l; Private transientHashmap<e,object>map; //Dummy value to associate with a Object in the backing Map Private Static FinalObject PRESENT =NewObject (); /*** Constructs a new, empty set; the backing <tt>HashMap</tt> instance has * default initial Capaci Ty (+) and load factor (0.75). */ PublicHashSet () {map=NewHashmap<>(); } PublicHashSet (collection<?extendsE>c) {map=NewHashmap<> (Math.max (int) (C.size ()/.75f) + 1, 16)); AddAll (c); } PublicHashSet (intInitialcapacity,floatloadfactor) {Map=NewHashmap<>(initialcapacity, loadfactor); } PublicHashSet (intinitialcapacity) {Map=NewHashmap<>(initialcapacity); }HashSet (intInitialcapacity,floatLoadfactor,Booleandummy) {Map=NewLinkedhashmap<>(initialcapacity, loadfactor); } PublicIterator<e>iterator () {returnMap.keyset (). iterator (); } Public intsize () {returnmap.size (); } Public BooleanIsEmpty () {returnMap.isempty (); } Public Booleancontains (Object o) {returnMap.containskey (o); } Public BooleanAdd (e e) {returnMap.put (e, PRESENT) = =NULL; } Public BooleanRemove (Object o) {returnMap.Remove (o) = =PRESENT; } Public voidClear () {map.clear (); } PublicObject Clone () {Try{HashSet<E> Newset = (hashset<e>)Super. Clone (); Newset.map= (Hashmap<e, object>) Map.clone (); returnNewset; } Catch(clonenotsupportedexception e) {Throw NewInternalerror (); } } Private voidWriteObject (java.io.ObjectOutputStream s)throwsjava.io.IOException {//Write out any hidden serialization magicS.defaultwriteobject (); //Write out HASHMAP capacity and load factorS.writeint (Map.capacity ()); S.writefloat (Map.loadfactor ()); //Write out SizeS.writeint (Map.size ()); //Write elements in the proper order. for(e e:map.keyset ()) S.writeobject (e); } Private voidReadObject (java.io.ObjectInputStream s)throwsjava.io.IOException, ClassNotFoundException {//Read in any hidden serialization magicS.defaultreadobject (); //Read in HASHMAP capacity and load factor and create backing HashMap intCapacity =S.readint (); floatLoadfactor =s.readfloat (); Map= (((HashSet) This)instanceofLinkedhashset?NewLinkedhashmap<e,object>(capacity, loadfactor):NewHashmap<e,object>(capacity, loadfactor)); //Read in size intSize =S.readint (); //Read in all elements in the proper order. for(inti=0; i<size; i++) {e e=(E) s.readobject (); Map.put (E, PRESENT); } }}
/*** Associates The specified value with the specified key on this map. * If The map previously contained a mapping for the key, the old * value is replaced. * * @paramkey key with which the specified value was to be associated *@paramvalue value to being associated with the specified key *@returnthe previous value associated with <tt>key</tt>, or * <tt>null</tt> if there is No mapping for <tt>key</tt>. * (A <tt>null</tt> return can also indicate that the map * previously associated <TT> ;null</tt> with <tt>key</tt>.) */ Publicv put (K key, V value) {if(Table = =empty_table) {inflatetable (threshold); } if(Key = =NULL) returnPutfornullkey (value); inthash =hash (key); inti =indexfor (hash, table.length); for(entry<k,v> e = table[i]; E! =NULL; E =e.next) {Object k; if(E.hash = = Hash && (k = e.key) = = Key | |Key.equals (k))) {V OldValue=E.value; E.value=value; E.recordaccess ( This); returnOldValue; }} Modcount++; AddEntry (hash, key, value, I); return NULL; }
3, HashMap is thread-safe, why is not thread-safe (best drawing to explain the multi-threaded environment is not secure)?
Reference: http://www.importnew.com/22011.html
4, HashMap of the expansion process
5, the difference between HashMap 1.7 and 1.8, explain 1.8 what optimization, how to optimize?
6. Final finally Finalize
7. Strong references, soft references, weak references, virtual references
8. Java Reflection
9, Arrays.sort realization principle and Collection realization principle
10, the application of Linkedhashmap
11, Cloneable Interface Implementation principle
12. Exception classification and processing mechanism
13. The difference between wait and sleep
14. How arrays are allocated in memory
The answer is to be added ...
Java surface question Finishing-basics