Saturday, August 30, 2025
HomeLanguagesJavaPath hashCode() method in Java with Examples

Path hashCode() method in Java with Examples

The Java Path interface was added to Java NIO in Java 7. hashCode() method of java.nio.file.Path used to return a hash code for this path after computing hashcode. The hashcode is always the same if the object doesn’t change. Hashcode is a unique code generated by the JVM at time of object creation. It can be used to perform some operation on hashing related algorithm like a hashtable, hashmap etc. An object can also be searched with its unique code (hashcode).

Syntax:

int hashCode()

Parameters: This method accepts nothing.

Return value: This method returns the hash-code value for this path.

Below programs illustrate hashCode() method:
Program 1:




// Java program to demonstrate
// java.nio.file.Path.hashCode() method
  
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // create object of Path
        Path path
            = Paths.get("D:/workspace/AmanCV.docx");
  
        // call hashCode() to get hashCode
        int hashCode = path.hashCode();
  
        // print hashCode
        System.out.println("Hash Code: "
                           + hashCode);
    }
}


Output:

Hash Code: 1600751599

Program 2:




// Java program to demonstrate
// java.nio.file.Path.hashCode() method
  
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // create object of Path
        Path path = Paths.get("D:/Resume.pdf");
  
        // call hashCode() to get hashCode
        int hashCode = path.hashCode();
  
        // print hashCode
        System.out.println("Hash Code: "
                           + hashCode);
    }
}


Output:

Hash Code: -996777206

References: https://docs.oracle.com/javase/10/docs/api/java/nio/file/Path.html#hashCode()

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32250 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11792 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11840 POSTS0 COMMENTS
Shaida Kate Naidoo
6733 POSTS0 COMMENTS
Ted Musemwa
7014 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6704 POSTS0 COMMENTS