The iterative iterator iterator and enumerator Enumeration_java in Java

Source: Internet
Author: User
Tags iterable

Iterator Iterator interface
1. Iterator interface
Iterable
The built-in method iterator () returns a new iterator.
Such as:

Public interface Iterable {iterator iterator ();}

Iterator has Hasnext () and Next () two methods to implement. Public interface Iterator {Boolean hasnext (); Item next (); void Remove (); Optional Implementation}
2. To achieve
Import

Import Java.util.Iterator;

Generic class, implement Iterable interface implements iterable< Item >
Implements the Iterable iterator () method, returning any defined iterator type.
Define iterator type implements iterator< Item >
Implement Hasnext (), Next (), remove ()
3. Example:

public class Stack<item> implements iterable<item> {public
  iterator<item> iterator () {
    return new Listiterator<item> (a)

  Private class Listiterator<item> implements iterator<item> {
    private node<item> current;

    Public Listiterator (node<item> i) {current
      = i
    public Boolean Hasnext () {return current!= null;           }
    public void Remove ()   {throw new Unsupportedoperationexception ();}

    Public Item Next () {
      if (!hasnext ()) throw new Nosuchelementexception ();
      Item item = Current.item;
      current = Current.next;
      return item;
    }
  }


4. Calling methods
foreach Iteration

 
 

If this is a basic type such as int\double, use the previous conversion relationship
5. Loop traversal

Iterator i = Stack.iterator (); while (I.hasnext ()) {String s = I.next ();}

Enumerator enumeration Interface

Enumeration is an interface class in Java.util that encapsulates a method for enumerating data sets in enumeration, similar to iterator, used to traverse elements in a collection  However, the enumeration enumeration only provides the ability to traverse the vector and Hashtable type collection elements, and this type of collection object obtains a enumeration object by invoking the elements () method The Enumeratino object then calls the following methods to iterate through the elements in the collection.

hasMoreElements (): Determine if there is any data in the enumeration object

Nextelement (): Gets the next data in the enumeration object

Examples are as follows:

Enumeration req = Request.getparameternames ();
 while (Req.hasmoreelements ()) {
   Object obj = (Object) req.nextelement ();
   if (obj.tostring (). Trim (). Equals ("LastPage")) {  
     System.out.println ("LastPage \ n");
   } else if (Obj.tostring () . Trim (). Equals ("NextPage")) {
    System.out.println ("NextPage");
   }
 

The difference between iterator and enumeration
in the Java collection, we usually traverse the collection through the iterator (iterator) or the enumeration (enum Class). Today, let's learn what the difference is between them.
Let's take a look at the source code of Enumeration.java and Iterator.java, and talk about their differences.
Enumeration is an interface, and its source code is as follows:

Package java.util;

Public interface Enumeration<e> {

  boolean hasmoreelements ();

  E nextelement ();
}

Iterator is also an interface, its source code is as follows:

Package java.util;

Public interface Iterator<e> {
  boolean hasnext ();

  E next ();

  void Remove ();
}

After reading the code, let's say the difference between them.
(01) function interface is different
enumeration has only 2 function interfaces. With enumeration, we can only read the data in the collection, not modify the data. The
iterator has only 3 function interfaces. Iterator can also delete data in addition to reading the collection's data. The
() iterator supports the fail-fast mechanism and enumeration is not supported. The
enumeration is an interface added by JDK 1.0. The functions used to include vectors, Hashtable, and the like, which are all added to JDK 1.0, enumeration exist to provide them with traversal interfaces. The enumeration itself does not support synchronization, and when Vector, Hashtable implements enumeration, synchronization is added. The
iterator is an interface added by JDK 1.2, and it also provides traversal interfaces for collections such as HashMap, ArrayList, and so on. Iterator is supported by the FAIL-FAST mechanism: When multiple threads operate on the contents of the same collection, Fail-fast events can occur.

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.