Set ①list②set③map④ generic ⑤collections
I. List: Sequential, repeatable
Implementation class:
1, ArrayList wide, suitable for query, add and delete infrequent (similar array, its length can be increased as required, and add efficiency slow)
2, LinkedList in the middle of frequent additions and deletions, low query efficiency (two-way linked list, first-out, not support random search, must start looking, low efficiency)
3, vector-like ArrayList, thread-safe, low performance
Second, Set: no order, non-repeatable
Save up to one null
Implementation class:
1, HashSet speed, not sorted
2, TreeSet slow, internal sorting, not see
3, Linkedhashset ordered, not repeat, order in order to put
third, the MAP key value to the Key-value key cannot repeat, the value can. Both can be an object instance of any type
Implementation class:
1, HashMap speed, not sorted
2, TreeMap slow, internal sorting
3, Linkedhashmap sort
Linked: Linked list, the front of which adds this is ordered
List,set is the collection subinterface, map is not,
Methods of Colection:
Size,add,remove, clear, ToArray;
List of methods:
Add (Index,obj)
Contains (Object o) determines whether it contains
The Equals method that calls the current type, if not overridden, is the Equals method of object, which is the reference. So rewrite the Equals method to achieve the desired result
Returns True when and only if the list contains element e that satisfies (O==null e==null:o.equals (e))
Get (Index)
Set (Index,obj)
Remove (Index)
Remove (Object)
If it is a number, direct is the subscript that, the object has a number, is the automatic packing class, not the basic data type
Method of Set:
Add
Remove
Siza
Contains
Map method:
Put (K,V) re-put to overwrite the information entered in the front
-get (Key)
Remove (key)
ContainsKey (k)
Containsvalue (value)
KeySet () All keys form set (not duplicated)
VALUES () All value form collection (can be duplicated)
generics : Allows you to specify a type-formal parameter when defining a class or interface, which is determined when declaring a variable, creating an object.
Specifies a type that can only be placed in this type
List<student> Group = new Arraylist<student> ();
You can only put the student type, not the strong turn, but also prevent the addition of other types
Define a generic type yourself
public class hello<t>{
T t;//here is not, member variable t
class above the definition of generics, as long as there is <> can be, inside is the variable name, you can take the name, general e means element, KV key value
Public Hello (T t) {
}
}
Five, Collections tool class
1, collections. Sort (list);
Natural order, can row basic data type, String, wrapper class, reference can not, so to set the collation
Once the collation is set, this can be called
2.
public class Student implements comparable{
public int compareTo (Object obj) {
Student stu = (Student) obj;
if (This.age > Stu.getage ()) {
return-1;
}else if (This.age < Stu.getage ()) {
return 1;
}
return 0;
}
}
You can also add other comparison rules, such as age, etc.
Large row front (from large to small) return-1, because the line on the small on the left,
3. anonymous Inner class
Collections.sort (Group, New comparator<student> () {
@Override
public int Compare (Student in1, Student in2) {
if (In1.getname (). CompareTo (In2.getname ()) > 0) {
return-1;
}else if (In1.getname (). CompareTo (In2.getname ()) < 0) {
return 1;
}else{
return 0;
}
}//Method Compare End
}//Class Comparator End
);//sort method End
3.9 Java Foundation Summary collection ①list②set③map④ generic ⑤collections