Path iterator() method in Java with Examples Last Updated : 30 Jul, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report The iterator() method of java.nio.file.Path used to returns an iterator of the name elements which construct this path. The first element of this iterator contains the name element that is closest to the root in the directory hierarchy, the second element is the next closest, and so on. The last element of this iterator is the name of the file or directory denoted by this path. The root component is not returned by the iterator. Syntax: default Iterator<Path> iterator() Parameters: This method accepts nothing.It is parameter less method. Return value: This method returns an iterator over the name elements of this path. Below programs illustrate iterator() method: Program 1: Java // Java program to demonstrate // java.nio.file.Path.iterator() method import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; public class GFG { public static void main(String[] args) { // create object of Path Path path = Paths.get("D:\\eclipse\\p2" + "\\org\\eclipse\\equinox\\p2\\core" + "\\cache\\binary"); // Creating an iterator Iterator<Path> elements = path.iterator(); // Displaying the values System.out.println("The iterator values are: "); while (elements.hasNext()) { System.out.println(elements.next()); } } } Output: Program 2: Java // Java program to demonstrate // java.nio.file.Path.iterator() method import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; public class GFG { public static void main(String[] args) { // create object of Path Path path = Paths.get("D:\\Workspace" + "\\nEclipseWork" + "\\GFG\\bin\\defaultpackage"); System.out.println("Original Path:" + path); // Creating an iterator Iterator<Path> elements = path.iterator(); // Displaying the values System.out.println("The iterator values are: "); while (elements.hasNext()) { System.out.println(elements.next()); } } } Output: References: https://docs.oracle.com/javase/10/docs/api/java/nio/file/Path.html#iterator() Comment More infoAdvertise with us Next Article Stack iterator() method in Java with Example A AmanSingh2210 Follow Improve Article Tags : Java Java-Functions Java-Path java.nio.file package Practice Tags : Java Similar Reads Set iterator() method in Java with Examples The java.util.Set.iterator() method is used to return an iterator of the same elements as the set. The elements are returned in random order from what present in the set. Syntax: Iterator iterate_value = Set.iterator(); Parameters: The function does not take any parameter. Return Value: The method i 1 min read Vector iterator() method in Java with Examples iterator() method of Vector class that is present inside java.util package is used to return an iterator of the same elements as that of the Vector. The elements are returned in random order from what was present in the vector. Syntax: Iterator iterate_value = Vector.iterator(); Parameters: The func 2 min read Stack iterator() method in Java with Example The Java.util.Stack.iterator() method is used to return an iterator of the same elements as that of the Stack. The elements are returned in random order from what was present in the stack. Syntax: Iterator iterate_value = Stack.iterator(); Parameters: The function does not take any parameter. Return 2 min read SortedSet iterator() method in Java with Examples The java.util.SortedSet.iterator() method is used to return an iterator of the same elements as the set. The elements are returned in random order from what present in the set. Syntax: Iterator iterate_value = sortedSetInstance.iterator(); Parameters: The function does not take any parameter. Return 1 min read ArrayList iterator() method in Java with Examples The iterator() method of ArrayList class in Java Collection Framework is used to get an iterator over the elements in this list in proper sequence. The returned iterator is fail-fast. Syntax: Iterator iterator() Parameter: This method do not accept any parameter. Return Value: This method returns an 2 min read Path getName(int) method in Java with Examples The Java Path interface was added to Java NIO in Java 7. The Path interface is located in the java.nio.file package, so the fully qualified name of the Java Path interface is java.nio.file.Path. A Java Path instance represents a path in the file system. A path can use to locate either a file or a di 2 min read Like