AbsractCollection iterator() Method in Java with Examples
The iterator() method of Java AbstractCollection is used to return an iterator of the same elements as that of the Collection. The elements are returned in random order from what was present in the Collection.
Syntax:
Iterator iterate_value = AbstractCollection.iterator();
Parameters: The function does not take any parameter.
Return Value: The method iterates over the elements of the Collection and returns the values(iterators).
Below programs illustrate the use of AbstractCollection.iterator() method:
Program 1:
// Java code to illustrate iterator() import java.util.*; import java.util.AbstractCollection; public class AbstractCollectionDemo { public static void main(String args[]) { // Creating an empty Collection AbstractCollection<String> abs = new TreeSet<String>(); // Use add() method to add // elements into the Collection abs.add( "Welcome" ); abs.add( "To" ); abs.add( "Geeks" ); abs.add( "4" ); abs.add( "Geeks" ); // Displaying the Collection System.out.println( "Collection: " + abs); // Creating an iterator Iterator value = abs.iterator(); // Displaying the values // after iterating through the collection System.out.println( "The iterator values are: " ); while (value.hasNext()) { System.out.println(value.next()); } } } |
Collection: [4, Geeks, To, Welcome] The iterator values are: 4 Geeks To Welcome
Program 2:
// Java code to illustrate iterator() import java.util.*; import java.util.AbstractCollection; public class AbstractCollectionDemo { public static void main(String args[]) { // Creating an empty Collection AbstractCollection<String> abs = new ArrayList<String>(); // Use add() method to add // elements into the Collection abs.add( "Welcome" ); abs.add( "To" ); abs.add( "Geeks" ); abs.add( "4" ); abs.add( "Geeks" ); // Displaying the Collection System.out.println( "Collection: " + abs); // Creating an iterator Iterator value = abs.iterator(); // Displaying the values // after iterating through the collection System.out.println( "The iterator values are: " ); while (value.hasNext()) { System.out.println(value.next()); } } } |
Collection: [Welcome, To, Geeks, 4, Geeks] The iterator values are: Welcome To Geeks 4 Geeks
Recommended Posts:
- Set iterator() method in Java with Examples
- BlockingDeque iterator() method in Java with examples
- SortedSet iterator() method in Java with Examples
- ArrayList iterator() method in Java with Examples
- Vector iterator() method in Java with Examples
- AbstractList iterator() method in Java with Examples
- DelayQueue iterator() method in Java with Examples
- Path iterator() method in Java with Examples
- Difference between Iterator and Enumeration in Java with Examples
- Deque iterator() method in Java
- CopyOnWriteArraySet iterator() method in Java
- Stack iterator() method in Java with Example
- ConcurrentSkipListSet iterator() method in Java
- Java AbstractSequentialList | iterator() method
- LinkedTransferQueue iterator() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.