the collection frame diagram in Java
: The collection in Java is divided into two collection and map two interface can be divided into collection is a single-column collection and map of the double-row collection
collection single-column collection : Inherits the iterator interface so it has the iterator () method, which returns a iterator<t>
Hasnext (), Next (), remove () 3 methods can be implemented in an implementation class.
Hasnext (): Determine if there is a next element
Cusor is the current operation subscript
Next (): reads the next element
Remove (): Delete parameter
Lastret is the subscript for the last operation
Collection has two sub-interfaces set and list
Set: Stores a set of objects that are unique and do not require an order
Set stores a reference to an element, such as two elements whose reference is equal, or whose value is equal, and if the hash value is equal, then set holds only one NULL in the set set of the same
Cases:
Set set=new HashSet
String a= "";
String B=a;
Set. Add (a);
Set.add (b);
The number of elements of the output set set
System.out.println (Set.size ()); The result is 1: The reference for both is one so you can add only one
List: Stores a group of objects that are not unique but ordered
Two implementation classes for the list:
ArrayList: The same storage method as the array, but a variable change in the size of the storage collection is done more flexibly than the array.
LinkedList: Chained storage, each item of the collection stores the next item of reference, more conducive to the addition and deletion of elements, compared to ArrayList query and modification, LinkedList new and deleted
In fact, before ArrayList there is a vector collection class, he and ArrayList basically the same, the biggest difference is that the vector is thread-safe, and ArrayList is thread insecure. ArrayList is light and safe, and fast, vector thread safe, but slow.
Map: Two-column collection : Each item in the collection is a paired key, and the value Map interface does not inherit the Iterable interface or implement the interface so it cannot be traversed directly
Key: There is a unique, not required order
Value: Not unique, no order required
There are three ways to traverse the map collection:
Public Static voidMain (string[] args) {Map<String,String> map=NewHashmap<string,string>(); Map.put ("CN", "People's Republic of China"); Map.put ("UK", "United Nations"); //Method 1 The two-column collection cannot be traversed? Then turn him into a single column by the way! Use the collection name. EntrySet method to get set<entry<string, string>> return value for(Entry item:map.entrySet ()) {System.out.println (String.Format ("The value of key is%s\tvalue value is%s", Item.getkey (), Item.getvalue ())); } //Method 2 by getting a collection of key keys, and then using the Map method Map.get (key) to get value by key for(String key:map.keySet ()) {System.out.println (String.Format ("The value of key is%s\tvalue value is%s", Key,map.get (key)); } //The iterator is automatically implemented in the method 3for loop, and the iterator operation is done manually without a for loopIterator<string> keys=Map.keyset (). iterator (); while(Keys.hasnext ()) {String Item=Keys.next (); System.out.println (String.Format ("The value of key is%s\tvalue value is%s", Item,map.get (item))); } }
If you just want to iterate through value values, use the Map.values () method directly.
Note: All collection classes that inherit or implement the Iterable class can be traversed with iterators
Cases:
ArrayList (Inheritance)-abstractlist-(in this ArrayList's parent class implements the iterator method that inherits from Iterable, and obtains the class iterator that the method returns and its 3 methods, Implement them to enable themselves to use iterators)-implements (Implementation)--List extends (inheritance)-Collection extends (inheritance)-iterable All that can use iterators The collection classes have similar inheritance and implementation relationships.
Beginner's diary, a collection frame in Java