public Iterator iterator();
//exa,
Iterator it=c.iterator();
//where c is any Collection object
Iterator interface defines the following three methods:
public booleanhasNext()
public Object next()
public void remove()
let's see a simple example for better understanding:
importjava.util.*; classIteratorDemo { public static void main(String args[]) { ArrayList al=new ArrayList(); for(inti=0;i<=10;i++) { al.add(i); } System.out.println(al); Iterator it=al.iterator(); while(it.hasNext()) { Integer i=(Integer)it.next(); if(i%2==0) System.out.println(i); else it.remove(); } System.out.println(al); } }
Output
[0,1,2,3,4,5,6,7,8,9,10]
0
2
4
6
8
10
[0,2,4,6,8,10]
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc