Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Useful Resources

Java - URL hashCode() Method with Examples



Description

The Java URL hashCode() method creates and returns an integer suitable for hash table indexing. The hash code is based upon all the URL components relevant for URL comparison. This operation is a blocking operation.

Declaration

Following is the declaration for java.net.URL.hashCode() method

public int hashCode()

Parameters

NA

Return Value

a hash code for this URL.

Exception

NA

Example 1

The following example shows the usage of Java URL hashCode() method for a valid url with https protocol. In this example, we're creating an instance of URL class. Now using hashCode() method, we're getting the hashcode and printing the same −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;

public class UrlDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https","www.tutorialspoint.com","/index.htm");
         int result = url.hashCode();
         System.out.println(result);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

1207952254

Example 2

The following example shows the usage of Java URL hashCode() method for a valid url. In this example, we're creating an instance of URL class. Now using hashCode() method, we're getting the hashcode and printing the same −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;

public class UrlDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://www.tutorialspoint.com/index.htm?language=en#j2se");
         int result = url.hashCode();
         System.out.println(result);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

1169952135

Example 3

The following example shows the usage of Java URL hashCode() method for a valid url with file protocol. In this example, we're creating an instance of URL class. Now using hashCode() method, we're getting the hashcode and printing the same −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;

public class UrlDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("file","www.tutorialspoint.com","/index.htm?language=en#j2se");
         int result = url.hashCode();
         System.out.println(result);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

1073477724
java_url.htm
Advertisements