Collection Class
1. List Collection
List: Elements are ordered and elements can be duplicated. Because the collection system has an index.
ArrayList: The underlying data structure uses an array structure. Features: Fast query speed. But additions and deletions are slightly slower. Thread is out of sync.
LinkedList: A linked list data structure used at the bottom. Features: Deletion speed quickly, query slightly slow. Thread is out of sync.
Vector: The bottom layer is the array data structure. Thread synchronization. Replaced by the ArrayList. Because the efficiency is low.
LinkedList: Unique Method:
AddFirst ();
AddLast ();
GetFirst ();
GetLast ();
Gets the element, but does not delete the element. If there are no elements in the collection, Nosuchelementexception appears
Removefirst ();
Removelast ();
Gets the element, but the element is deleted. If there are no elements in the collection, Nosuchelementexception appears
An alternative approach appeared in JDK1.6.
Offerfirst ();
Offerlast ();
Peekfirst ();
Peeklast ();
Gets the element, but does not delete the element. If there are no elements in the collection, NULL is returned.
Pollfirst ();
Polllast ();
Gets the element, but the element is deleted. If there are no elements in the collection, NULL is returned.
2. List collection-specific iterator: Listiterator
Listiterator is a sub-interface of iterator
In an iteration, you cannot manipulate the elements in the collection through the methods of the collection object.
Because the concurrentmodificationexception exception occurs.
So, in an iterator, you can only use an iterator-sparing element, but the iterator method is limited,
Only the elements can be judged, taken out, removed from the operation,
If you want other operations such as add, modify, and so on, you need to use its sub-interface, Listiterator.
This interface can only be obtained through the Listiterator method of the list collection.
2.Set Collection
Set: Elements are unordered (the order in which they are deposited and taken out is not necessarily consistent), and elements cannot be duplicated. 、
HashSet: The underlying data structure is a hash table. is not thread-safe. Different steps.
How the HashSet guarantees element uniqueness:
is done through the elements of the two methods, hashcode and equals.
If the hashcode value of the element is the same, the equals is not determined to be true.
If an element has a different hashcode value, equals is not called.
TreeSet: The elements in the set collection can be sorted, and the underlying data structure is a two-fork tree.
The basis for ensuring element uniqueness:
CompareTo method return 0.
TreeSet the first way to sort: to make the elements themselves comparable.
The element needs to implement the comparable interface, overriding the CompareTo method.
It also becomes the natural order of the elements, or is called the default order.
The second sort of
treeset.
Package Unit14;import Java.util.*;class Studnet_2 implements comparable<object>{private String name;private int Age Studnet_2 (String name, int age) {this.name = Name;this.age = age;} Public String GetName () {return name;} public int getage () {return age;} Based on the streeset underlying data structure, the collection implements element conformance through the CompareTo method public int compareTo (Object obj) {if (!) ( obj instanceof studnet_2)) throw new RuntimeException ("This is not a student object"); Studnet_2 stu = (studnet_2) obj;if (This.age > Stu.age) return 1;if (this.age = stu.age) return This.name.compareTo ( Stu.name); return-1;}} public class Treesettest {public static void main (String args[]) {treeset<object> st = new Treeset<object> (); s T.add (New Studnet_2 ("ASGASDG")), St.add (New Studnet_2 ("Gao"), St.add (New Studnet_2 ("Wang"); St.add (new Studnet_2 ("Lin"), St.add (New Studnet_2 ("ASGASDG"));iterator<object> it = St.iterator (); while ( It.hasnext ()) {studnet_2 Stu = (studnet_2) it.next (); System.out.println (Stu.getname () + "..." + stu.getage ());}}
3. Map Collection
Hashtable: The underlying is a hash table data structure that cannot be stored as a null key null value. The collection is thread-synchronized. jdk1.0. Low efficiency.
HashMap: The underlying is a hash table data structure that allows NULL values and NULL keys, which are not synchronized. Replace Hashtable, jdk1.2. High efficiency.
TREEMAP: The bottom layer is a two-fork tree data structure. Thread is out of sync. Can be used to sort the keys in the map collection.
Map Collection: The collection stores key-value pairs. A pair of pairs went into the deposit. and to guarantee the uniqueness of the key.
1, Add.
Put (K key, V value)
Putall (map<? extends K,? extends v> m)
2, delete.
Clear ()
Remove (Object key)
3, Judge.
Containsvalue (Object value)
ContainsKey (Object key)
IsEmpty ()
4, get.
Get (Object key)
Size ()
VALUES ()
EntrySet ()
KeySet ()
Collection classes: List, set, Map