JAVA collection type (modern variable cluster)
1. List ( interface )
Characteristics:
A. Sequencing is sensitive, The elements in the list structure must be separated who first who is behind.
B. can be repeated
It is mainly used tosimulate thesequence-sensitive application scenarios such as queue.
<1> ArrayList (array-based implementation)
Array in use is more troublesome,ArrayList is a class, its array of 2 Transformation, or on the basis of the data of two development, from the perspective of use to overcome the many shortcomings of the array, But in essence, it relies on the array mechanism to do it.
ArrayList internally relies on an array of objects object[], which has numerous methods that are expanded around the object array.
Size () gets The number of actual contained elements inside the ArrayList.
Add () append to last
Add (index,obj) inserts the data to the subscript position specified by index
Remove (index) deletes The element at the specified index position
Remove (obj) deletes the element that matches the object (dependent on equals )
Contains (obj) to see if the element exists
IndexOf (obj) View the index bits that appear on the element (from scratch)
LastIndexOf (obj) View the index bits that appear on the element (starting at the end)
IsEmpty () to see If a valid element exists in ArrayList
Sublist () Create sub List
ToArray () Turn List to Array
TrimToSize reduces the amount of capacity to the actual number of elements that exist.
Advantages:
Similar to arrays, random positioning is fast .
But in the coding convenience is much better than the array, less write a lot of control code.
Disadvantages:
With array types, insertions and deletions are prone to large fluctuations, and when the element forces are out of range, there are also a large number of copies of array elements that occur, which have a significant impact on system performance.
Applicable scenarios:
After data entry, more stable, less delete and insert operations, mostly for positioning query operations
Data increase frequency interval is longer, does not continuously increase continuously
A data structure that is extremely sensitive to the sequencing of elements such as queues.
traversal of the ArrayList
1. General Traversal Method
for (int i=0;i<al.size (); i++)
System.out.println (Al.get (i)):
1. Collection type dedicated traversal method (jdk1.5 and above version support )
for (Object Obj:al)
System.out.println (obj);
2. Iterative body (iterator) traversal method
Iterator Itr=al.iterator ();
while (Itr.hasnext ())
System.out.println (Itr.next ());
ArrayList generic type (generic type) control
Because the ArrayList is flexible, the interior can place a variety of data types, resulting in external programs, from the ArrayList to take out an element, it must be carefully judged, Sometimes it is expensive to have a large number of different types of code for the corresponding operation of the various objects. Some programmers forget to write some code, often lead to classcastexception, So later, people have to limit the type of elements in ArrayList, Requires only one element within it.
<2> LinkedList ( linked list-based implementation )
Advantages:
With linked list types, insert and delete performance is superior and does not cause memory fluctuations.
Disadvantages:
Similar to linked lists, random positioning is slow and needs to be retrieved from the beginning .
High memory consumption of the linked list
Applicable scenarios:
Data entry, more unstable, with frequent deletion and insertion operations, random location query relatively few.
A data structure that is extremely sensitive to the sequencing of elements such as queues.
2. Set(interface)
Characteristics:
1. Set is a sub-interface of the Collection interface that inherits all the abstract methods in the Collection interface and does the set the applied method extension.
2. the Set interface emphasizes non-repetition, but no sequencing.
A. HashSet
According to the hash ( hash ) algorithm for memory allocation, the collection of data to save the type, for loading the elements, stored in the memory location, the user does not care, the way to save is certainly efficient, This is guaranteed with a hashing algorithm, but there is one thing that cannot be repeated! HashSet does not guarantee the order in which the elements are inserted.
HashSet How do I identify the elements that are inserted in a similar way?
? Understanding Object.hashcode ()
Object objects have a hashcode method, which is a local method, implemented by the C language, which returns the start address of the object in memory, and the return value is an integer.
Any object whose hashcode value is different if it does not override the hashcode method of object .
? HashSet relies on The return value of hashcode to determine whether an object is equal
1. When an object is attempted to be saved intoHashSetthe time,HashSetThis object will be called first.hashcodemethod returns ahashcodevalues,HashSetThe value and its internalhashcodeList(The table holds all the savedHashSetOf the object in thehashcodevalue)To do the comparison, If the discovery is not the same, the object is consideredHashSetis not repeated, can be accepted.
2. If the hashcode of the object is found to be identical to an object in the list, then hashset will invoke the object's equals method to make the same final judgment. the hashset will still accept the object if Equals thinks it is different.
Object has equals and hashcode Two methods to determine whether the object content is equal to the judgment operation, in ordinary applications, we generally use equals can be judged, but hashset is often judged by whether the object is the same, because the collection type has high performance requirements, and equals judgment involves more complex logic, high cost, and, to some extent, performance reduction, but equals judgment is the highest precision .
From a performance standpoint,Object also provides a lightweight method of judging , hashcode. because hashcode The interior is just an integer operation, and the bottom level is C , and it's very fast.
Hashcode think that the object is different, andequals does not intervene.
Hashcode think the same, then by equals to make a final judgment,equals result is the final result, can overturn The result of Hashcode's decision.
Hashcode is the "District Court" and equals is the "Court of Final Appeal".
? The contract between Hashcode and equals
Since there is a master-slave relationship between the hashcode and the equals method, especially if an object is ultimately to deal with HashSet , the hashcode and equals methods must be overridden at the same time, and two methods must guarantee a contract when overriding, otherwise the rewrite will fail!
Contract:
1. Hashcode different, equals must be different
2. Hashcode is the same, equals can be different
B. TreeSet ( tree structure Collection )
the elements in the TreeSet cannot be duplicated, but they are sorted out according to some established rule (natural order).
All objects added to the TreeSet must implement the comparable interface,
The interface has only one method, int compartto (obj);
return value:
1 The current object is greater than the incoming object
0 Current object equals incoming object
-1 the current object is less than the incoming object
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java collection type (ii)