Iterable and Iterator of java. util, java. utiliterable
package java.lang;
import java.util.Iterator;public interface Iterable Iterator}
Iterable is located in the java. lang Package, which holds an Iterator reference
package j
Total: Java provides a relatively complete set of container classes, the basic types are: List, set, Queue, Map, these object types are called collection classes.one, interface inheritance relationship:Iterable interface, in Java.lang package, Collection, List, Queue, set interface inherit Iterable interfaceAs can be seen, list, Queue, set these three interfaces are in the Java.util package, inherit from th
Parent interface of Collection:iterable interfaceA class that implements the Iterable can is used with the new for-loop.The interface has only one Iterable method:Public interface IterableIt is possible to use your own collection type classes with the new For-loop. To does so, your class must implement the java.lang.Iterable interface. Here is a very basic example:public class MycollectionAnd here is the co
Definition of iterable:Java.lang Bag/** * Implementing this interface allows a object to be the target of * the "foreach" statement. * * @param /** * Returns An iterator over a set of elements of type T. * * @return an Iterator. * /iteratorDefinition of iterator:Java.util Package:Public interface IteratorIterator is an iterator class, and iterable is designed to iterate using foreach as long as the interface is implemented.The itera
1. iterator is an iterator class, and iterable is designed to iterate by using foreach as long as the interface is implemented.
2. the iterator interface is encapsulated in iterable, and iterator iterators can be used as long as the class of the Iterable interface is implemented.
3. Collection collection, List, and set are all implementation classes of
The Java Collection class library separates the interfaces and implementations of the set. The same interface can be implemented differently.
The basic interface of the Java Collection class is the collection interface. The collection interface must implement the iterator interface.
To indicate the interfaces of the Collection framework, java. Lang and
Java Iterable interface and IteratorJava Iterable interface and Iterator iterator Iterator
The interface is defined as follows:
public interface Iterator
{ boolean hasNext(); E next(); void remove();}
This interface only contains three functions. The hasNext () and next () methods appear in our common set traversal.Functions:Use next () to obta
Http://stackoverflow.com/questions/6863182/what-is-the-difference-between-iterator-and-iterable-and-how-to-use-themIterator is an interface, which have implementation for iterate over elements. Iterable is an interface which provides Iterator.An implementation of being one Iterable that provides an of Iterator itself: Public Interface
To implement a class that can be used with a foreach loop, you must implement Iterable, which overrides a Iterator method, which returns a iteratorThe code is as follows:public class Itertest implements IterableStores the contents of the array private object[] Obj=new object[1];//record the number of elements added to the private int size;Records the current element's subscript private int current=0;//add element public void Add (String str) {Determ
! =expectedmodcount)Throw Newconcurrentmodificationexception (); } }The idea for the remove operation is broadly consistent except that Lastret is replaced with a list node lastreturned, which is also set to null after each remove. The get element is not obtained directly through get (i), as in the parent version. The iterator saves two adjacent node pointers lastreturned and next. When the element is removed (lastreturned=null), it can still be moved in the linked list because the next point
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.