This article first introduces the interfaces and classes contained in the Java collection framework in an overall way, and then summarizes some basic knowledge and key points in the collection framework, and conducts simple analysis with examples. When we put an object into a collection, the system will treat all collection elements as instances of the Object class. Since JDK1.5, this state has been improved: generics can be used to limit the types of elements in the collection and let the collection remember the types of all collection elements.
1. Summary
All collection classes are located under the java.util package. Only objects can be saved in a collection (save the reference variables of objects). (Arrays can save both basic types of data and objects).
When we put an object into a collection, the system will treat all collection elements as instances of the Object class. Since JDK1.5, this state has been improved: generics can be used to limit the types of elements in the collection and let the collection remember the types of all collection elements (see the content of specific generics).
Java's collection class is mainly derived from two interfaces: Collection and Map, Collection and Map are the root interfaces of the Java collection framework, and these two interfaces include some interfaces or implementation classes. The Set and List interfaces are two sub-interfaces derived from the Collection interface. Queue is a queue implementation provided by Java, similar to List.
Map implementation class is used to save data with mapping relationships (key-value). Set, List and Map can be regarded as three categories of collections. List collections are ordered collections, the elements in the collection can be repeated, and the elements in the collection can be accessed according to the index of the elements. Set collections are unordered sets, elements in the set cannot be repeated, and elements in the set can only be accessed based on the element itself (which is also the reason why elements in the set cannot be repeated).
The element of the form of a Key-value pair is stored in the Map collection. When accessing, its value can only be accessed according to the key of each element.
For the three sets of Set, List and Map, the most commonly used implementation classes are HashSet, ArrayList and HashMap. (Concurrent control collection class, study it later if you have time).
2. Collection interface
The Collection interface is the parent interface of List, Set and Queue interfaces, and can operate these three interfaces at the same time. The Collection interface defines the specific methods for operating collection elements. You can refer to the API documentation. Here we use an example to illustrate the methods of adding elements, deleting elements, returning the number of elements in the collection, and clearing collection elements.
3. Two methods to traverse the collection Iterator interface and foreach loop
1. Iterator interface
Iterator is also a member of the Java collection framework and is mainly used to traverse (i.e. iteratively access) elements in a collection, also known as an iterator.
Three methods provided:
boolean hasNext(): Returns the next element in the collection.
Object next(): Returns the next element in the collection.
void remove(); deletes the element returned by the last next method in the collection.
(1) When assigning the iterative variable book through the statement "book = "test string"; ", when we output the books collection again, the elements in the collection have no changes. That is, when it is used to iterate on a collection element, iterator does not pass the collection element itself to the iterative variable, but passes the value of the collection element to the iterative variable.
(2) When using Iterator to access the Collection collection element, only by deleting the collection element (it.remove();) the last time the next method returned by the collection element can be added to the collection (book = "test string";). Otherwise, a java.util.ConcurrentModificationExcption exception is raised.
2. Use foreach to loop through the collection elements.
Format: for(element type t element variable x: traversal object A) {// program block}
illustrate:
(1) Foreach simplifies the traversal of arrays and collections. If you do not want to traverse the entire collection, or you need to operate subscript values inside the loop, you need to use the traditional for loop.
(2) Simplify programming and improve code readability and security (no need to worry about arrays crossing boundaries).
(3) Foreach is generally used in combination with generics
The above summary of Java collection framework is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.