Iterator ListIterator and resolve differences as well as detailed API

Note: content from a network of others articles!

  Any implements Collection collection class interface, there is a Iterator method that returns an implementation of Iterator object interface for a set of traverse; Iterator interface defines three methods are  the hasNext (),  Next () Remove ( );  

  We use List , the Set the time, in order to achieve traversing their data, we often use to Iterator (iterators). Using an iterator, you do not need to intervene in the process of its traverse, just out each time a data you want to process it.

  But when in use is also different. List and Set has  iterator ()  to get its iterator. For List, you can also  () listIterator  get its iterator two iterators in some cases are not common, Iterator and ListIterator The main difference in the following areas:

  1.  Iterator ()  method set and list is defined for the interface, but  the ListIterator ()  is present only List interface (or implementation class);

  2. the ListIterator have  add ()  method, the List add objects, rather Iterator not

  3. ListIterator and Iterator has  hasNext ()  and  next ()  method may be implemented to traverse back order, but ListIterator there  hasPrevious ()  and  previous ()  method may be implemented reverse (forward order) traversal. Iterator can not.

  4. the ListIterator can locate the current position of the index, nextIndex ()  and  previousIndex ()  can be realized. Iterator not have this feature.

  5. Remove the object can be realized, but ListIterator can be modified to achieve the object, SET ()  method can be implemented. Iierator only traverse can not be modified.  

  Because ListIterator these functions can be achieved on LinkedList etc. List operations data structure. In fact, an array of objects can also be achieved with iterators

 

  Iterator interface API resolved 

1  // iteration to see if there is a next element has no return true false 
2  boolean hasNext ();
 3  // Get the current iteration of the elements 
4  E the Next ();
 5  // delete the current element can only delete the current only in hasNext () can be invoked for the true time 
. 6  default  void Remove () {
 . 7          the throw  new new an UnsupportedOperationException ( "Remove" );
 . 8      }
 . 9  // is a default method for introducing the Iterator interface in 1.8 when it is used through the collection foreach ( ) difference: for most implementations collection Iterable interface, you can call multiple times forEach (), and to make multiple passes through the elements. Instead, forEachRemaining () all the elements using an iterator Iterator, and the second call, it will not do anything, because there is no next element. 
10  default  void forEachRemaining (Consumer <? Super E> action) {
11         Objects.requireNonNull(action);
12         while (hasNext())
13             action.accept(next());
14     }

   ListIterator interface (API) to resolve 

public interface ListIterator<E> extends Iterator<E> 

  First, from the definition of the interface, we can know, the interface can be inherited interfaces

  The official documentation says, this interface is only for List iterator, we can iterate through it in any direction from the list, modify the list during iteration, get iterator's current position in the list. ListIterator no current element (current element), in which the elements and the cursor is always call the next previous () method returns the method returns between the elements (). A list of length n, the iterator has its cursor position n + 1          

// positive sequence of iterations to see if there is a next element has no return true false 
boolean hasNext ();
 // returns the next element in the current cursor position (note that when the iterator is in the right position the last element of the list, call the next () method will be abnormal, since the latter has no elements of the cursor) 
E Next ();
 // reverse order iteration 
Boolean hasPrevious ();
 // reverse order returns the current cursor position of an element 
E previous ();
 // n order to obtain index method returns the last element of the implementation of the next () 
int nextIndex ();
 // reverse order acquisition method returns the index of the last element in the implementation Previous () 
int previousIndex ();
 // remove the last from the list method returns a performing element next () or previous ().
// (Note: 1 call remove () method before, there must be next () or previous () method to perform, otherwise an error: java.lang.IllegalStateException
 //2. After executing next () or previous () remove the previous execution (), can not perform add (E) method, otherwise an error: a java.lang.IllegalStateException) 
void remove ();
 // parameter elements instead of next () or previous () returns the last element of
 // (Note: before calling set (E) method, can not call remove () and add (E) method, otherwise it will error: java.lang.IllegalStateException
 // because the call to remove () or after add (E) method, and no pointer element points, only at one position) 
void SET (E E);
 // the specified element is inserted into the previous () are returned between the current cursor position (note: at this call next () method still returns the next element, calling previous () is the return of the newly inserted element. It is noteworthy that, when the cursor position is at 0 (left of the first element), call add (E) The method will be abnormal, because now not yet left of the cursor element) 
void the Add (E E);

 

               

 

  

Guess you like

Origin www.cnblogs.com/liyijie/p/12108651.html