Java Collection Framework (i)

Source: Internet
Author: User

Diagram of the collection class in Java:

Collection

First, the largest interface in the collection--collection

You can see the most common methods in the collection interface by looking at the JDK help documentation. These methods are demonstrated by the following code examples:

ImportJava.util.*; Public classCollectiondemo { Public Static voidMain (string[] args) {//method_2 ();Method_get (); }         Public Static voidMethod_get () {ArrayList Al=NewArrayList (); //1. Adding elementsAl.add ("Java01");//add (Object obj);Al.add ("Java02"); Al.add ("Java03"); Al.add ("Java04"); /*Iterator it = Al.iterator ();//Gets the iterator that is used to remove the element in the collection while (It.hasnext ()) {SOP (It.next ()); }        */        //development, so write for (Iterator it = al.iterator (); It.hasnext ();)        {SOP (It.next ()); }            }     Public Static voidmethod_2 () {ArrayList al1=NewArrayList (); Al1.add ("Java01"); Al1.add ("Java02"); Al1.add ("Java03"); Al1.add ("Java04"); ArrayList Al2=NewArrayList (); Al2.add ("Java03"); Al2.add ("Java04"); Al2.add ("Java05"); Al2.add ("Java06"); //Al1.retainall (AL2);//Al1, only the same elements in the Al2 are preserved in theAl1.removeall (AL2); SOP ("Al1:" +al1); SOP ("Al2:" +Al2); }     Public Static voidBase_method () {//Create a collection container that uses subclasses of the collection interface, ArrayListArrayList Al =NewArrayList (); //1. Adding elementsAl.add ("Java01");//Add (Object obj);Al.add ("Java02"); Al.add ("Java03"); Al.add ("Java04"); //Print Original CollectionSOP ("Original set:" +al); //3. Deleting elements//al.remove ("java02"); //al.clear ();//emptying the collection//4. Judging elementsSOP ("JAVA03 is present:" +al.contains ("Java03"))); SOP ("Is the collection empty?" "+al.isempty ()); //2, get the number, set lengthSOP ("Size:" +al.size ()); //print a changed collectionSOP (AL); }         Public Static voidsop (Object obj) {System.out.println (obj); }}

Attention:

    1. The argument type of the Add method is object, so that any type can be accepted.
    2. A reference (address) of an object is stored in the collection.

Now focus on the iterator (Iterator)

Iterator

What is an iterator?

is actually the way the collection is taken out of the element.

The initial explanation of iterator is that the extraction method is defined inside the set so that the extraction method can directly access the elements inside the set, then the extraction method is defined as the inner class . The data structure of each container is different, so take out the details of the action is not the same, but there are common content-judgment and extraction, then these commonalities can be extracted, then these internal classes are consistent with a rule, the rule is iterator.

How do I get the Fetch object of a collection?

Through an externally available method: iterator ().

List

Collection

|---------List: elements are ordered and elements can be duplicated because the collection system is indexed.

|---------ArrayList: The underlying data structure uses an array structure. Features: Fast query, but slightly slower additions and deletions. Thread is out of sync.

|---------LinkedList: The underlying uses a linked list data structure. Features: Deletion speed quickly, query slightly slow.

|---------Vector: The bottom layer is the array data structure. Thread synchronization. Replaced by the ArrayList.

|---------Set: elements are unordered and elements cannot be duplicated.

List-specific methods: all the methods that can manipulate the angle mark are the unique methods of the system.

Increase:

    1. Add (index, Element);
    2. AddAll (index, Collection);

By deleting:

    1. Remove (index);

Change:

    1. Set (index, Element);

Check:

    1. Get (index);
    2. Sublist (from, to);
    3. Listiterator ();

The list collection-specific iterator (Listiterator)--listiterator is a sub-interface of the iterator. when iterating, the elements in the collection cannot be manipulated through the methods of the collection object, because a concurrency modification exception (concurrentmodificationexception) occurs. Therefore, in the iteration, only with the method of the iterator operation elements, but the iterator method is limited, can only judge the elements, take out, delete the operation, if you want to other operations such as add, modify, etc., you need to use its sub-interface Listiterator.

This interface can only be obtained through the listiterator () of the list collection.

Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.List;ImportJava.util.ListIterator; Public classListdemo { Public Static voidsop (Object obj) {System.out.println (obj); }         Public Static voidmethod () {ArrayList Al=NewArrayList (); //adding elementsAl.add ("Java01"); Al.add ("Java02"); Al.add ("Java03"); SOP ("Original collection:" +al); //add an element at the specified locationAl.add (1, "java09"); //deletes a collection at the specified location//Al.remove (2); //modifying elements//Al.set (2, "java007"); //get elements with a corner markSOP ("Get (1):" +al.get (1));                SOP (AL); //Get all elements         for(intx = 0; x < Al.size (); X + +) {SOP ("Al (" +x+ ") =" +al.get (x)); } Iterator It=Al.iterator ();  while(It.hasnext ()) {SOP ("Next:" +It.next ()); }                //get the location of an object through IndexOfSOP ("index =" +al.indexof ("JAVA02"))); List Sub= Al.sublist (1, 3); SOP ("Sub=" +sub); }         Public Static voidMain (string[] args) {//demo list iterators ArrayList Al =NewArrayList (); //adding elementsAl.add ("Java01"); Al.add ("Java02"); Al.add ("Java03");                SOP (AL); Listiterator Li=Al.listiterator (); //SOP ("hasprevious ():" +li.hasprevious ());                 while(Li.hasnext ()) {Object obj=Li.next (); if(Obj.equals ("JAVA02"))                //Li.add ("java009");Li.set ("java006");//Modify        }         while(Li.hasprevious ()) {SOP ("Pre:" +li.previous ()); }                //SOP ("Hasnext ():" +li.hasnext ()); //SOP ("hasprevious ():" +li.hasprevious ());                /*//During the iteration, prepare to add or remove elements Iterator it = Al.iterator ();                        while (It.hasnext ()) {Object obj = It.next ();                if (Obj.equals ("JAVA02"))//al.add ("java008"); it.remove ();//The reference to JAVA02 was removed from the collection, but the JAVA02 reference was also used by obj        SOP ("obj=" +obj);//Note: Although "java02" is removed, it can be output }            */SOP (AL); }}

Java 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.