Java Learning Lesson 33rd (Common Object API)-collection framework (i)

Source: Internet
Author: User
Tags addall

There are many numbers that are stored in arrays, There are many objects that need to be stored together

But the array is fixed-length, and the set is variable-length

The origin of the collection:

Objects are used to encapsulate unique data, objects need to be stored, if the number of objects is not determined, you need to use the collection container to store

features of the collection:

1. Containers for storing objects

2. Variable length

3. Basic data types are not stored in the collection

For the collection system, the topmost layer of storage is all the common content of the system, Collection, like inheritance, look at the top layer, with the bottom

cellection in the java.uitil Package


Collection container because of the internal data, there are a number of specific containers, constantly upward extraction, forming a set of framework

The top layer of the entire frame is the Cellection interface.

Common methods of cellection (must be mastered):

1. Add a Boolean Add (object e)//Add a Boolean AddAll (Collection e)//Add a heap of 2. Remove the 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 ()//Determines whether the collection has elements 4. Gets the int size ()//Returns the number of elements in this collection. Iterator <E>  Iterator ()//iterators Remove the elements in the collection by means of 5. Other Methods Boolean Retainall (Collect E)//Take intersection object[] ToArray ()//convert collection to array

Method Demo:

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)//Demo 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 all elements of the E container that are the same as F// System.out.println ("e =" +e),//containsall//boolean flag = E.containsall (f);//system.out.println (flag),//e contains f// RetainAllSystem.out.println (E.retainall (f));//and RemoveAll function In contrast, save the same, delete different System.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");//Change length//e.clear ();//Empty System.out.println (e);}}

Use of iterators

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 ();//Random new A Collection subclass E.add ("A1"); E.add ("A2"); E.add ("A3"); E.add ( "A4"); System.out.println (e);//Call the iterator method in the collection by calling the Iterator () method in collection   , in order to get the iterator object in the collection/*iterator it = E.iterator (); while (It.hasnext ())//it.hasnext (): Returns True if there are still elements that can iterate. {System.out.println (It.next ());//Returns the next element of the iteration. It.remove ();//delete: Removes the last element returned by the iterator from the collection that the iterator points to (optional action). }*/for (Iterator it = E.iterator (); It.hasnext ();)//write for loop, loop local code block the end, it is deleted, developed with for: Save memory {System.out.println (It.next ( ));} System.out.println (e);}}


The principle of iterators:

The iterator itself is an inner class, and the iterator must know the structure of the elements in the collection if it wants to access the elements in the collection, so the iterator is only internally able to get the contents of the collection, as for how it is implemented by the container itself, because the container itself determines


Iterator interface is the final, all the container public to remove the way, as long as the interface is found, there is no need to face each object, as long as the interface can be found to achieve the removal of each container, it also conforms to the characteristics of the interface: reduce the coupling of the container and the removal method

We only need to use the public access method, as for the internal how to fetch, do not need to know, so the iterator does not use the new object

Summarize:

Iterator it = E.iterator ();//Find the public take-out method of the E-container

The object (it) must be dependent on the container for the element to be fetched, because the data structure of each container is different, so the iterator object must be implemented inside the container, and for the container holder, the implementation is unimportant, as long as the object of the iterator to the implementation is obtained through the container

The iterator method.

The iterator interface is the common interface for all collection containers to be removed from the element

See the code for the iterator interface in the API in detail

Java Learning Lesson 33rd (Common Object API)-collection framework (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.