JAVA learning lesson 33rd (Common Object API)-set framework (1)
Many numbers are stored in arrays,Many objects need to be stored in collections.
But the array is fixed length, and the set is variable length.
Collection origin:
An object is used to encapsulate special data. If more objects need to be stored, if the number of objects is uncertain, you need to use a collection container to store them.
Collection features:
1. Containers used to store objects
2. Variable Length
3. Basic data types cannot be stored in the collection
For a Collection system, the top-level storage is all the common content of the system. Collection is the same as inheritance.
Cellection in java. uitil package
Because of the different internal data, a collection container has multiple specific containers that are constantly extracted, forming a collection framework.
The Cellection interface is the top layer of the framework.
Common Cellection methods (required ): <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3ryb25np1_vcd4kpha + PHByZSBjbGFzcz0 = "brush: java;"> 1. add boolean add (Object e) // add a boolean addAll (Collection e) // add a bunch of 2. delete boolean remove (Object e) // delete a boolean removeAll (Collection e) // delete some void clear (); // delete all 3. determine whether boolean contains (object obj) // contains a boolean containsAll (Cellection e) boolean isEmpty () // determine whether the set has elements 4. get int size () // returns the number of elements in the collection. Iterator Iterator () // method by which the iterator retrieves elements from the set. 5. Other Methods: boolean RetainAll (Collect e) // returns the intersection Object [] toArray () // converts the set to an array.
Method Demonstration:
Import java. util. arrayList; import java. util. collection; public class Main {public static void main (String [] args) {Collection e = new ArrayList (); Collection f = new ArrayList (); // show (e ); show (e, f);} public static void show (Collection e, Collection f) // demonstrate xxxAll {e. add ("a1"); e. add ("a2"); e. add ("a3"); e. add ("a4"); f. add ("a2"); f. add ("a5"); f. add ("a6"); f. add ("a7"); // addAll // e. addAll (f); // System. out. println ( "E =" + e); // removeAll // e. removeAll (f); // Delete the elements in the e container that are the same as f. // System. out. println ("e =" + e); // containsAll // boolean flag = e. containsAll (f); // System. out. println (flag); // whether e contains f // retainAllSystem. out. println (e. retainAll (f); // unlike removeAll, The retainAll function is used to delete different systems. out. println (e);} public static void show (Collection e) // ctrl + shirt + o, import package {// Add. Adde. add ("asd1"); e. add ("asd2"); e. add ("asd3"); System. out. println (e); // Delete e. remove ("asd2"); // Changes the length. // e. clear (); // clear System. out. println (e );}}
Use of iterator
Import java. util. arrayList; import java. util. collection; import java. util. iterator; public class Main {public static void main (String [] args) {Collection e = new ArrayList (); // a subclass e of a new Collection. add ("a1"); e. add ("a2"); e. add ("a3"); e. add ("a4"); System. out. println (e); // call the iterator () method in the Collection-> call the Iterator method in the set to obtain the iterator object in the Set/* Iterator it = e. iterator (); while (it. hasNext () // it. hasNext (): returns tr if there are still elements that can be iterated. Ue. {System. out. println (it. next (); // return the next element of the iteration. It. remove (); // Delete: remove the last element returned by the iterator from the collection to which the iterator points (Optional ).} */For (Iterator it = e. iterator (); it. hasNext ();) // write the for loop. When the partial code block of the loop ends, it is deleted. for: saves memory {System. out. println (it. next ();} System. out. println (e );}}
Iterator principle:
The iterator itself is an internal class. To access the elements in the Set, the iterator must know the storage structure of the elements in the set. Therefore, the iterator can obtain the content of the set only after it is defined internally, as for how to obtain the information, the container implements it by itself, because the storage method is determined by the container itself.
The Iterator interface is final. All containers use the public retrieval method. As long as you find this interface, there is no need to face every object. As long as you find the interface, you can retrieve each container, it also meets the characteristics of the interface: reduces the coupling between containers and retrieval methods
We only need to use the public access method. As for how to obtain the internal information, we do not need to know, so the iterator does not need new objects.
Summary:
Iterator it = e. iterator (); // find the public Retrieval Method of the e container.
This object (it) must depend on the container to retrieve elements. Because the data structure of each container is different, the iterator object must be implemented inside the container. For container users, the specific implementation is not important. You only need to obtain the object of the implementation iterator through the container.
That is, the iterator method.
The Iterator interface is a public interface for all Collection containers to retrieve elements.
For details, refer to the Iterator interface code in the API.