container (Collection): An array is a container, and a collection is also a container
In Java programming, a thing that is loaded with a variety of other objects (reference types), called containers
Attention:
1, the length of the array is fixed
2, set: The length is not fixed, can be added and deleted at any time, as long as not beyond the memory, just add
two, the Assembly interface (six major interfaces)
1, Collection (including List interface and set interface)
List---( -can be used with equals (reference type)---subscript ()
LinkedList (linked list)---(Fast, slow)
*arraylist (Array)---(slow, fast)
set---( No order, can not repeat )
*hashset (hash code table) (Must override Hashcode () method)
TreeSet (binary tree---data structure)
2, Map (key-value pairs, Each time you put it in a pair of pairs) ( Key cannot be repeated, neither equals )
*hashmap
TreeMap
3, comparable (a method (Comparato))
4, Iterator (loop traversal, 3 methods)
Boolean Hasnext ()
Object Next ()
Remove ()
Usage:
while (Hasnext ()) {
Next ()
}
three, the method of collection interface
1, the use of collection interface
collection<string> C = new arraylist<string> ();
Question: Why not write directly arraylist<string> a = new arraylist<string> ();
2, C.add (parameter type must be object)
3, C.remove method: By judging whether two objects are the equals of each other to determine if the object is deleted, the custom class, you need to override the parent class's Equals method
Overriding the Equals method, you should also override the Hashcode method
4, Hashcode is usually used to index, an object through its Hashcode value can find its address in memory, so two objects if equals, and again as an index, the value of hashcode must be equal
To illustrate:
Packageutil;Importjava.util.ArrayList;Importjava.util.Collection; Public classtextcollection { Public Static voidMain (string[] args) {//interface collection cannot be directly new, it must be new by its implementation class, because the interface is abstract class, there is no method body, can not directly useCollection C =NewArrayList ();//a reference to the parent class is directed to the object of the child classC.add (1); C.add ("Nihao"); C.add (NewPerson ()); System.out.println (c); //The output is: [1, Nihao, person [haha]]C.remove (1); C.remove ("Nihao"); C.remove (NewPerson ());//New has a different point in memory and must be equalsSystem.out.println (C.size ()); //output Result: 1System.out.println (c);//the output is: [person [haha]] }}classperson{@Override PublicString toString () {return"Person [haha]"; }}
Iv. List Interface
ArrayList (the API says the initial capacity is 10, pay attention to this problem), LinkedList
Sequential, can be added repeatedly
set (have a return value to note!)
Retainall (Collection)----Returns a Boolean value that returns True when the contents of the list are changed, otherwise false, leaving only the elements contained in the specified collection in this list (optional operations).
Size (): Returns the number of elements in this list.
Packageutil;Importjava.util.ArrayList;Importjava.util.List; Public classTextCollection1 {@SuppressWarnings ("Unchecked") Public Static voidMain (string[] args) {List List1=NewArrayList (); List List2=NewArrayList (); List List3=NewArrayList (); for(inti = 0; I < 5; i++) {List1.add ("String" +i); if(i%2==0) {List2.add ("String" +i); List3.add ("String" + i*10+1); }} System.out.println (List1); //The output is: [String0, String1, String2, String3, String4]System.out.println (List1.get (3));//the output is: String3System.out.println (List1.set (2, "haha"));//the output is: String2System.out.println (List1);//The output is: [String0, String1, haha, String3, String4]System.out.println (List1.remove (4));//the output is: String4System.out.println (List1);//The output is: [String0, String1, haha, String3]System.out.println (List1.indexof ("String3"));//Output Result: 3System.out.println (List1.lastindexof ("String3"));//Output Result: 3//List1.retainall (list2);//returns True if the method is changed, False if not changedSystem.out.println (List1.retainall (List2));//the output is: trueSystem.out.println (List1);//the output is: [STRING0]//List1.retainall (LIST3);System.out.println (List1.retainall (LIST3));//the output is: falseSystem.out.println (List1);//the output is: [] }}
five, map interface
Map interface
Put: There is a return value
Remove: There is a return value
Example:
PackageUtil1;ImportJava.util.HashMap;ImportJava.util.Map; Public classTextCollection2 {@SuppressWarnings ("Unchecked") Public Static voidMain (string[] args) {map map=NewHashMap (); for(inti = 0; I < 5; i++) {map.put (I,NewPerson ("name" +i)); } System.out.println (map); //The output values are: {0=person [NAME0], 1=person [name1], 2=person [name2],//3=person [Name3], 4=person [Name4]}Map.put (5,NewPerson ("newcomer"))); SYSTEM.OUT.PRINTLN (map); //The output values are: {0=person [NAME0], 1=person [name1], 2=person [name2],//3=person [Name3], 4=person [name4], 5=person [new]}Map.put (1,NewPerson ("Another newcomer")); SYSTEM.OUT.PRINTLN (map); //The output value is: {0=person [NAME0], 1=person [another new person], 2=person [name2],//3=person [Name3], 4=person [name4], 5=person [new]}System.out.println (Map.get (1));//the output value is: Person [another newcomer]System.out.println (Map.Remove (1));//the output value is: Person [another newcomer]SYSTEM.OUT.PRINTLN (map);//The output values are: {0=person [NAME0], 2=person [name2], 3=person [Name3],//4=person [Name4], 5=person [new]}System.out.println (Map.Remove (0,NewPerson ("name" + 0));//output value is: TrueSYSTEM.OUT.PRINTLN (map);//The output values are: 2=person [name2], 3=person [Name3], 4=person [Name4],//5=person [New]}System.out.println (Map.containskey (4));//output value is: TrueSystem.out.println (Map.containsvalue (NewPerson ("name" + 0));//The output value is: false, deleted in previous stepSystem.out.println (Map.size ());//the output value is: 4System.out.println (Map.isempty ());//output value is: falsemap.clear (); SYSTEM.OUT.PRINTLN (map);//the output value is: {}Map map2=NewHashMap (); for(inti = 0; I < 5; i++) {Map2.put ("A" +i,NewPerson ("====a" +i)); } map.putall (MAP2); SYSTEM.OUT.PRINTLN (map);//The output values are: {A1=person [====a1], A2=person [====A2], A3=person [====A3],//A4=person [====A4], A0=person [====a0]} }}classperson{String name; PublicPerson (String name) { This. Name =name; } @Override PublicString toString () {return"Person [" + name + "]"; } @Override Public inthashcode () {Final intPrime = 31; intresult = 1; Result= Prime * result + ((name = =NULL) ? 0: Name.hashcode ()); returnresult; } @Override Public Booleanequals (Object obj) {if( This==obj)return true; if(obj = =NULL) return false; if(GetClass ()! =Obj.getclass ())return false; person Other=(person) obj; if(Name = =NULL) { if(Other.name! =NULL) return false; } Else if(!name.equals (other.name))return false; return true; } }
Common methods for containers, collections, and collections of Java basics