First, the collection
1, the collection class is also called the container class, mainly is responsible for the preservation, the attire other data.
2, the collection can hold the quantity indeterminate data, holds the data which has the mapping relation (also called the associative array).
3, JAVA5 after the provision of some multithreaded security collection class, put in Java.util.concurrrent. (utility abbreviation, meaning multipurpose, practical)
4. The collection can only hold objects (which are actually references to objects, which are used to refer to objects).
5, the Java collection is mainly by collection and map two interface derivation, the map interface implementation class is used to hold the associative array, the set set element is not repeatable, through the element itself accesses, the map collection can be accessed through the key value, the list collection can be accessed by the subscript.
Second, collection interface
1, collection interface is the parent interface of Set, Queue, list interface, so its method can also control three sets.
A, Boolean Add (Object O): Adds an element to the specified collection, specifying that the collection object returns True because this method changes.
B, Boolean addall (Collection C): Adds all the elements in the set C to the specified collection, specifying the collection object as this method changes, returns True.
c, void Clean (): Clears all elements of the specified collection, changing the length of the collection to 0.
D, Boolean contains (Object O): Returns whether the specified element is contained in the collection.
E, Boolean containsall (Collection C): Returns whether the collection contains all the elements in the collection C.
F, Boolean isEmpty (), returns True if the collection is empty, or false if the length is 0.
G, Boolean remove (Object O): Deletes the specified element in the collection O, when the collection contains one or more elements O, removes the first qualified element, and returns True.
H. Boolean RemoveAll (Collection c): Removes all elements contained in collection C from the collection, removes one or more elements, and returns True.
I, Boolean retainall (Collection c): Removes elements outside of the set C from the collection, specifying the collection object because this method changes, returns True.
J, in size (): Returns the number of elements in the collection,
K, object[] ToArray (): Converts the set to an array, and the collection element becomes the corresponding array element.
2. Iterating through the collection elements using the Foreach Loop
The code is as follows:
1 ImportJava.util.HashSet;2 Importjava.util.Objects;3 4 Public classMyClass {5 Public Static voidMain (string[] args) {6 //The HashSet () class should be a class that implements the collection interface, otherwise it cannot be instantiated7Collection names =NewHashSet ();8 //What should be added is a reference, where the string is placed directly in the constant pool? 9Names.add ("Xiaoming");TenNames.add ("Xiaogang"); OneNames.add ("Xiaoli"); A for(Object thename_obj:names) { -String thename =(String) thename_obj; - System.out.println (thename); the //the following sentence throws Concurrentmodificationexception, because Thename is an iterative variable, not the element itself - if(Thename.equals ("Xiaoli")){ - Names.remove (thename); - } + } - } +}
Operation Result:
1 xiaoming2 Xiaoli3Exception in thread "main"java.util.ConcurrentModificationException4At Java.util.hashmap$hashiterator.nextnode (hashmap.java:1437)5At Java.util.hashmap$keyiterator.next (hashmap.java:1461)6At Com.example.MyClass.main (myclass.java:15)7 At sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)8At Sun.reflect.NativeMethodAccessorImpl.invoke (nativemethodaccessorimpl.java:62)9At Sun.reflect.DelegatingMethodAccessorImpl.invoke (delegatingmethodaccessorimpl.java:43)TenAt Java.lang.reflect.Method.invoke (method.java:498) OneAt Com.intellij.rt.execution.application.AppMain.main (appmain.java:144) A -Process finished with exit code 1
"Java" Collection _ Learning Notes