Table in Java Collections API for Java data structures

Source: Internet
Author: User

1.Collection interface

Located in the Java.util package, the following is an important part.

1   Public InterfaceCollection<anytype>extendsIterable<anytype>2  { 3       intsize ();4       BooleanisEmpty ();5       voidClear ();6       BooleanAdd (AnyType x);7       BooleanRemove (AnyType x);8Java.util.iterator<anytype>iterator ();9}

The collection interface extends the Iterable interface. which classes implement the Iterable interface can have an enhanced for loop , which is applied on top of these classes to observe all of their items.

Enhanced for loop example: The print can be used to print all items in any collection.

1  Public Static void print (collection<anytype> coll)2{3      for (AnyType item:coll) {4        System.out.println (item); 5     }6 }

2.Iterator Interface : (personally think the iterator here vaguely has the meaning of the pointer in C)

  The combination of implementing the Iterable interface must provide a method called iterator that returns an object of the iterator class.

1  Public Interface Iterator<anytype>2{3     boolean  hasnext (); 4     AnyType Next (); 5     void remove (); 6 }
View Code

Each call to next gives the next item that the collection has not yet seen. The first call gives the first item.

The compiler sees that the enhanced for loop will overwrite it with the form of an iterator. The print example is written as a compiler.

 Public Static void print (collection<anytype> coll) {    iteraror<AnyType> itr = coll.iterator () ;      while (Itr.hasnext ()) {        = itr.next ();        System.out.println (item);    }     }
View Code

The Remove method of the iterator interface and the Remove method for the collection interface:

Remove method for the collection interface:

You must find the item that was deleted, which involves a cost issue.

Remove method for the iterator interface:

    You can delete the item that was recently returned by next (that is, the item that the iterator has just traversed), and then we cannot call the Remove method again until after another call to next.

  When using iterator directly (instead of using an enhanced for loop), the iterator will no longer legitimately throw an exception if a structural change is made to the combination that is being iterated (that is, by using the Add, remove, and clear methods for that collection). But if the iterator calls its own remove method, it is still legal.

3.List interface

The Java.util package inherits the collection interface. Here are just a few important methods.

 Public Interface extends Collection<anytype>{    AnyType get (int  idx);    AnyType Set (int  idx, AnyType newval);     void Add (int  idx, AnyType x);     void Remove (int  idx);        Listiterator<AnyType> listiterator (int  poss);}
View Code

Index 0 is in the front segment of the table, size ()-1 represents the last item in the table. list is an interface, and list ADT has two popular implementations : ArrayList ( An array of growth implementations) and LinkedList ( Double-linked list implementations). for Search, both ArrayList and LinkedList are inefficient and both spend linear time (the binary balance search tree takes O (logn) time).

Listiterator Interface:

Expanded the functionality of iterator, adding previous () and hasprevious (). The Add () method places a new item in the table at its current location. The set () method changes the last value that is seen by the iterator. So it is very convenient for LinkedList.

 Public Interface extends Iterator<anytype>{    boolean  hasprevious ();    AnyType previous ();         void Add (AnyType x);     void set (AnyType newval)}
View Code

 

Table in Java Collections API for Java data structures

Related Article

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.