Java container and iterator interface detailed analysis using

Source: Internet
Author: User
Tags int size

Iterator interface:

So the container class that implements the collection interface has a iterator method to return an object that implements the iterator interface.

The iterator object is called an iterator to facilitate the traversal of elements within a container.

The iterator interface defines the following methods:

  
   
   
Boolean hasnext (); To determine if there are elements on the right side of the cursor
Object Next (); Returns the element to the right of the cursor and moves the cursor to the next position
void Remove (); Deletes the element to the left of the cursor, which can only be executed once after the next action is executed

Practice:

   
    
    
Import java.util.*;
public class testiterator{
public static void Main (string[] args) {
Collection C = new HashSet ();
C.add ("Hello");
C.add ("World");
C.add ("!");
Iterator i = C.iterator ();
while (I.hasnext ()) {
string n = (string) i.next ();
System.out.println (n);
}
}
}

Device: an instance of a series of classes provided by the Java API that is used to store objects in a program. The container class provided by the JDK is in the Java.util package. The collection interface defines methods for accessing a set of objects, and its Sub-interface set and list define the storage mode respectively.


The data objects in the set are not ordered and cannot be duplicated.

The data objects in the list are in order and can be duplicated.

The map interface defines a method for storing "key (value) mapping pairs"
Collection The method defined in the interface:

   
    
    
int size (); How many elements
Boolean IsEmpty ();//IS NULL
void Clear ();
Boolean contains (object element);//Whether or not to contain an object
Boolean Add (object element);//Add Element
Boolean remove (object element);//delete
Iterator iterator ();
Boolean Containsall (collection C);//is not containing the elements in another collection
Boolean AddAll (collection C);//Add All
Boolean RemoveAll (collection C);//Delete all
Boolean Retainall (collection C);//Find Intersection
Object[] ToArray ();//Convert to object class array


The collection is loaded with objects and cannot be the underlying data type. Because the underlying data type is stored on the stack, it can be retracted at any time.

The Remove () method calls the Equals () method, so the class that you implement overrides the Equals method and the Hashcode () method.

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.