As the saying goes: A thing, one thing can not be separated from three words: "What is, why, how to do"
What the collection is:
A collection is simply a high-level representation of a collection of arrays used to store data or objects in a container;
Why the collection exists:
Collection only embodies the flexibility of data manipulation, greatly facilitates the efficiency of developers;
How the collection is used:
Use is divided into the following ways: Collection collection 1.list 2.Set
List
List |
Underlying data structure |
Inquire |
Deletions |
Thread |
Efficiency |
ArrayList |
Array |
Fast |
Slow |
Not safe |
High |
Vector |
Array |
Fast |
Slow |
Safety |
Low |
LinkedList |
Linked list |
Slow |
Fast |
Not safe |
High |
Set
Set |
Underlying data structure |
Ordered |
Only |
Sort |
HashSet |
Hash table |
Whether |
Yes (dependent on hashcode () and Equals ()) |
-- |
Linkedhashset |
Linked lists and hash tables |
Is |
Is The chain list ensures that the elements are orderly Guaranteed element unique by hash table |
-- |
TreeSet |
Red and black Trees |
-- |
Is |
A. Natural sorting B. Comparator sequencing |
(4) Overview of Collection features
List cn=new ArrayList ();
A: Add Features
Add (Object e);//Direct Natural sequential insertion
Add (index, Element);//Insert by index
B: Delete Feature
Remove (index);//Remove by index
C: Judging function
Cn.contains (object e)//Determine if element e exists in the CN set
D: Get Features
Cn.get (index)//Index Get value
E: Length function
Cn.size ()//return set length
F: Intersection (understanding)
Cn.retainall (Collection C)//cn intersection of C
CN to c intersection The return value is a Boolean value
If there is a intersection, the return is true. The value in the CN collection returns the intersection value
return False if no intersection
G: Turn the set into an array
Cn.toarray ()
(5) Traversal of the collection collection
A: Turn the set into an array (usually not used)
Iterate over a set to iterate over an array
B: Iterator (Collection-specific method)
Iterators
To create an iterator for a CN collection Object
Iterator It=cn.iteraror ();
while (It.hasnext ())
{System.out.println (It.next ())}
1: Is the way the collection gets the elements.
2: There is a dependency on the collection.
3: The principle and source of the iterator.
A: Why is the definition for an interface instead of an implementation class?
Simply put: no matter what kind of collection, we have to get the data in the collection, and in the acquisition before the function of the judgment, that is, before the acquisition of elements before the first judgment, then the collection has two functions (judgment, query). In this way, if the implementation class can only implement a certain set, but the interface can do, let the sub-class to implement the interface, so that the requirements are met;
(5) Concurrency modification exceptions
A: a phenomenon that appears
Iterators iterate through collections, collections modify collection elements
B: Reason
Iterators are dependent on collections, and the collection's change iterators are not known.
C: Solutions
A: Iterator traversal, iterator modification (listiterator)
Element is added at the location of the iteration just
B: Collection traversal, collection modification (size () and get ())
Element is added at the end of the collection
(6) Common data structures
A: Stack advanced back out
B: Queue FIFO
C: Array query fast, adding and deleting slow
D: List query slow, and delete fast
Talking about Collection Collection