Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java - FilePermission hashCode() method



Description

The Java FilePermission hashCode() method returns a hash code value for the FilePermission object. This hash code is primarily used for efficient storage in hash-based collections like HashSet and HashMap. The hash code is derived from the file path and permission actions.

Declaration

Following is the declaration for java.io.FilePermission.hashCode() method −

public int hashCode()

Parameters

NA

Return Value

This method returns the hash code value of this object.

Exception

NA

Example - Usage of FilePermission hashCode() method

The following example shows the usage of Java FilePermission hashCode() method.

FilePermissionDemo.java

package com.tutorialspoint;

import java.io.FilePermission;
import java.io.IOException;

public class FilePermissionDemo {
   public static void main(String[] args) throws IOException {
      FilePermission fp = null;
      
      try {
         // create new file permissions
         fp = new FilePermission("test.txt", "read");
         
         // the hash code value
         int hash = fp.hashCode();
         
         // prints
         System.out.print("Hash Code: "+hash);
         
      } catch(Exception ex) {
         // if an error occurs
         ex.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Hash Code: 1006542423

Example - Hash Code for Read and Write Permissions

The following example shows the usage of Java FilePermission hashCode() method.

FilePermissionDemo.java

package com.tutorialspoint;

import java.io.FilePermission;

public class FilePermissionDemo {
   public static void main(String[] args) {
      // Creating a FilePermission object with "read" and "write" actions
      FilePermission filePermission = new FilePermission("example.txt", "read,write");

      // Getting and displaying the hash code
      int hash = filePermission.hashCode();
      System.out.println("Hash Code: " + hash);
   }
}

Output (varies at runtime)

Let us compile and run the above program, this will produce the following result−

Hash Code: 579275665

Explanation

  • A FilePermission object is created for "example.txt" with "read" and "write" permissions.

  • The hashCode() method generates a unique integer based on the file path and permission actions.

Example - Different Permissions Result in Different Hash Codes

The following example shows the usage of Java FilePermission hashCode() method.

FilePermissionDemo.java

package com.tutorialspoint;

import java.io.FilePermission;

public class FilePermissionDemo {
   public static void main(String[] args) {
      // Creating two FilePermission objects with different actions
      FilePermission filePermission1 = new FilePermission("example.txt", "read");
      FilePermission filePermission2 = new FilePermission("example.txt", "write");

      // Getting and displaying the hash codes
      System.out.println("Hash Code (read): " + filePermission1.hashCode());
      System.out.println("Hash Code (write): " + filePermission2.hashCode());
   }
}

Output (varies at runtime)

Let us compile and run the above program, this will produce the following result−

Hash Code (read): -1195731697
Hash Code (write): 1324228237

Explanation

  • Two FilePermission objects are created for the same file (example.txt) but with different actions (read and write).

  • Since permissions differ, the hashCode() values will also differ.

  • If two FilePermission objects have the same file path and actions, they will generate the same hash code.

java_io_filepermission.htm
Advertisements