8 Integrated
Used to store objects with variable lengths to store different types of objects
8.1 Integrated Frame
Collection
List
ArrayList: The underlying data structure uses an array structure. Features: Fast query speed, adding and deleting slow. Thread is out of sync
LinkedList: The underlying data structure uses a linked list data structure. Features: Slow query, adding and deleting fast. Thread is out of sync
Vector: The underlying data structure is used by the array-of-structures. Thread synchronization, replaced by ArrayList.
Set
HashSet: The underlying data structure uses a hash table, and the thread is out of sync.
TreeSet: The underlying data structure uses a two-fork tree to sort the elements in the set set, but the comparable interface needs to be implemented. The basis for guaranteeing the uniqueness of the element: CompareTo method return 0.
1. Common methods:
Add (); adding elements
Remove (); Delete element
Clear (); empty collection
Contains (); Contains element
IsEmpty (); Whether the collection is empty
Size ();
Retainall (); Take the intersection of two sets
Example: A.retainall (b); A only retains the same elements as B
2. Traversing elements
Using iterators: Iterator
Iterator it = A.iterator ();
while (It.hasnext ()) {
It.next ();
}
for (Iterator it = A.iterator (); It.hasnext ();) {
It.next ();
}
8.2 List
1, Characteristics: elements are ordered, can be repeated, the collection has an index
2. Unique methods
Add (index,element); Specify the location of the element
AddAll (index,collection); Add a collection at the specified location
Remove (index); Delete the element at the specified position
Set (index,element); Sets the value of the specified element
Get (index); Gets the element at the specified position
IndexOf (); Gets the position of the element
Sublist (From,to), intercepting the collection, containing the From, does not contain the same
Listiterator (); list iterator, iterator sub-interface
In an iteration, you cannot manipulate the elements in the collection through the methods of the collection object, because the concurrentmodificationexception exception occurs. Therefore, in the iteration, only through the iterator amount method operation elements, free to judge, remove and delete operations.
If you want other operations, such as add, modify, and so on, you need to use its subinterface listiterator.
8.3 LinkedList
1. Unique methods
AddFirst (); Adding elements to the head
AddLast (); Adding elements at the tail
GetFirst (); get the head element
GetLast (); get tail element
Removefirst (); Remove and remove the head element
Removelast (); Remove and remove trailing elements
Where get and remove are used, nosuchelementexception occurs if there are no elements in the collection.
2, there is an alternative method in JDK1.6
adding elements
Offerfirst ();
Offerlast ();
Gets the element that returns NULL if there are no elements in the collection;
Peekfirst ();
Peeklast ();
Gets and deletes an element that returns null if there are no elements in the collection;
Pollfirst ();
Polllast ();
8.4 Set
1, Characteristics: elements are unordered, elements can not be repeated
2, the function of set set and collection is consistent
3. HashSet guarantees the uniqueness of the element: True by Hashcode and equals
If the hashcode value of the element is the same, the equals is not determined to be true.
If the Harbin code value of the element is not the same, the Equals method is not called.
For actions that determine whether an element exists, as well as additions and deletions, the dependent method is the hashcode of the element and the Equals method.
8.5 generic type
After the JDK1.5 version, which is used to resolve security issues, is a security mechanism that uses <> representations
Arraylist<string> a = new arraylist<string> ();
1. Generic type
When the reference data type to be manipulated in the class is not deterministic
Class utiles<tt>{
Private TT t;
public void Settt (TT t) {
THIS.T = t;
}
Public TT gettt () {
return t;
}
}
A static method cannot access a generic class and can access a generic method.
2. Generic method
Public <T> void Show (T t) {
...... ;
}
3. Generic Limit
ArrayList <? Extends e>;
? Represents a wildcard character
You can receive an e-type or a subtype of e, the upper limit.
ArrayList <? Super e>;
You can receive the type E or the parent type of e, the lower bound.
8.6 Map
The collection stores key-value pairs
HashTable: The underlying data structure uses a hash table, which does not deposit null keys and null values, and thread synchronization.
HASHMAP: The underlying data structure uses a hash table that can use NULL keys and null values, and threads are out of sync.
TREEMAP: The underlying data structure uses a two-fork tree, and the thread is out of sync. Can be used to sort the keys in the map collection.
1. Method:
Add to
Put (K key,v value);
Putall (map<? extends K,? extends v> m);
Delete
Clear ();
Judge
ContainsKey (Object key);
Containsvalues (Object value);
IsEmpty ();
Get
Get (Object key);
Size ();
Values ();
EntrySet ();
Set <Map.Entry<k,v>> entryset; the mapping relationships in the map collection are stored in the set collection, and the data type of the relationship is map.entry. Once you get map.entry, you can get the keys and values in the relationship through the Getkey and GetValue methods in them.
KeySet (); Save all the keys to the set set because the set has iterators, so you can remove all the keys in an iterative way, and then get the values for each key according to the map's Get method
8.7 tool classes for the Integrated Framework
Collections.fill (List,object);
You can replace all the elements in the list collection with the specified elements
Collections.repalceall (List,oldvalue,newvalue);
Replaces the old value in the list collection with the new value.
Collections.reverse (List);
Reverses the elements in the list collection
8.8 Advanced for Loop
Format:
For (data type variable name: traversed collection or array) {
}
Only elements can be obtained, but the collection cannot be manipulated.
8.9 variable Parameters
When passing parameters, use ...
public void Show (int ... arr) {}
Where arr is actually an array
8.10 Static Import
Import static java.util.arrays.*;
All static members in the arrays class are imported.
This article is from the "Java Basics" blog, so be sure to keep this source http://8163413.blog.51cto.com/8153413/1713087
Eighth Chapter Collection Class