I. Collection Framework
Collectoin Map
List Set HashMap
ArrayList LinkedList HashSet
Methods of 1.1 list
list< elements > List = new arraylist< element type > ()
List.add (element);//Add Element
List.add (element);//Inserts an element into the specified position
List.remove (element);//delete the element with the specified small mark
List.remove (element);//delete the specified element
List.remove (element);//Gets the element with the specified subscript
List.remove (element);//Whether there is a specified element in the collection
1 Public classTest1 {2 Public Static voidMain (string[] args) {3Dog Oodog =NewDog ("Europe", "Labrador Dog"));4Dog Yydog =NewDog ("Ya Ya", "Shelly Rui");5Dog Mmdog =NewDog ("Meimei", "Shelly Rui"));6Dog Ffdog =NewDog ("Feifei", "Labrador Dog"));7List d =NewArrayList ();8 D.add (oodog);9 D.add (yydog);Ten D.add (mmdog); OneD.add (2, Ffdog);//add Yydog at specified location ASYSTEM.OUT.PRINTLN ("Total has" +d.size () + "dog"); -System.out.println ("The difference is:"); - for(intI=0;i<d.size (); i++){ theDog dog =(Dog) d.get (i); -System.out.println (Dog.getname () + "\ T" +Dog.getstrain ()); - } - } +}Example 1
The result of the operation is:
The difference between 1.2 collection,list,set
Collection: not unique, unordered
List: not unique, orderly
Set: Unique, unordered
The difference between 1.3 ArrayList and LinkedList
A. All implementations of the list
B.arraylist is a kind of edge-length array implementation, high query efficiency
LinkedList is a list-style internship, increasing and deleting the high efficiency
Common methods of 1.4 list
list<object> list = new arraylist<object> ();
List.add (obj);//Add Element
List.get (subscript)//Get the specified subscript element
List.size ();//Length
List.remove (obj);//delete the specified element
List.contains (obj);//Whether the specified element is included
1.5 Map Common methods (key-value pair way to store data)
map<object,object> maps = new hanshmap<object,object> ();
Maps.put (Key,value);//Add Element
Maps.get (key);//Gets the value of the specified key
Maps.size ();//Length
Maps.remove (key);//delete the specified element
Maps.containskey (key);//Whether the specified key is included
1 Public classTest2 {2 Public Static voidMain (string[] args) {3Dog Oodog =NewDog ("Europe", "Labrador Dog"));4Dog Yydog =NewDog ("Ya Ya", "Shelly Rui");5Dog Mmdog =NewDog ("Meimei", "Shelly Rui"));6Dog Ffdog =NewDog ("Feifei", "Labrador Dog"));7LinkedList d =NewLinkedList ();8 D.add (oodog);9 D.add (yydog);Ten D.addfirst (mmdog); One D.addlast (ffdog); ADog Dogfirst =(Dog) D.getfirst (); -System.out.println ("The first dog's nickname is:" +dogfirst.getname ()); -Dog Doglast =(Dog) d.getlast (); theSystem.out.println ("The first dog's nickname is:" +doglast.getname ()); - d.removelast (); - D.removefirst (); -System.out.println ("\ \ \ \ \ \ \ \ \ \ \ \" +d.size () + "dog" after deleting some dogs); +System.out.println ("The difference is:"); - for(intI=0;i<d.size (); i++){ +Dog D1 =(Dog) d.get (i); ASystem.out.println (D1.getname () + "\ T" +D1.getstrain ()); at } - } -}Example 2
The result of the operation is:
Note:key is unique, repeat, then overwrite the previous value can be repeated
the similarities and differences between Vector and ArrayList
A.vector thread safety, ArrayList heavy speed, light security, thread non-secure
B. When the length grows, the vector increases by one time by default and ArrayList grows by 50%
Similarities and differences of Hashtable and HashMap
A. The principle of implementation, the same function, can be interoperable
B. Main differences
1,hashtable inherits Dictionary class, HashMap implements interface
2,hashtable thread safety, HashMap thread non-secure
3,hashtable null values are not allowed, HashMap allows null values
1.6 Iteration of the collection
Syntax:interator<object> it = collection. iterator ();
while (It.hasnext) {
Object obj = It.next ();
}
It.hasnext ();//Determine if there is a next element
it.next;//move point increment, return to the pointing element
Note: The collection can not be modified in the process of the article, the modification operation, then will throw
Concarrentmodification exception exception, need to be modified, use the It.remove () method of the iterator;
1 Public classTest3 {2 Public Static voidMain (string[] args) {3Dog Oodog =NewDog ("Europe", "Labrador Dog"));4Dog Yydog =NewDog ("Asia and Asia", "Shelly Rui"));5Dog Mmdog =NewDog ("Meimei", "Shelly Rui"));6Dog Ffdog =NewDog ("Feifei", "Labrador Dog"));7Map m =NewHashMap ();8 M.put (Oodog.getname (), oodog);9 M.put (Yydog.getname (), yydog);Ten M.put (Mmdog.getname (), mmdog); One M.put (Ffdog.getname (), ffdog); ASystem.out.println ("Using iterator traversal, all dog nicknames and breeds are:"); - //Take out the collection of all keys -Set keys =M.keyset (); theIterator it =keys.iterator (); - while(It.hasnext ()) { -String k =(String) It.next (); -Dog d =(Dog) m.get (k); +System.out.println (k + "T" +D.getstrain ()); - } + } A}Example 3
The result of the operation is:
Java (18) Collection framework