Java 教程

Java 控制語句

面向物件程式設計

Java 內建類

Java 檔案處理

Java 錯誤與異常

Java 多執行緒

Java 同步

Java 網路程式設計

Java 集合

Java 介面

Java 資料結構

Java 集合演算法

高階 Java

Java 雜項

Java APIs 和框架

Java 類引用

Java 有用資源

Java - URL hashCode() 方法及示例



描述

Java 的 URL hashCode() 方法建立一個適合雜湊表索引的整數並返回它。雜湊碼基於所有與 URL 比較相關的 URL 元件。此操作是阻塞操作。

宣告

以下是 java.net.URL.hashCode() 方法的宣告

public int hashCode()

引數

返回值

此 URL 的雜湊碼。

異常

示例 1

以下示例演示了使用 Java URL hashCode() 方法處理具有 https 協議的有效 url。在此示例中,我們正在建立 URL 類的例項。現在使用 hashCode() 方法,我們獲取雜湊碼並列印它。

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();
      }
   }
}

讓我們編譯並執行上述程式,這將產生以下結果:

輸出

1207952254

示例 2

以下示例演示了使用 Java URL hashCode() 方法處理有效 url。在此示例中,我們正在建立 URL 類的例項。現在使用 hashCode() 方法,我們獲取雜湊碼並列印它。

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://tutorialspoint.tw/index.htm?language=en#j2se");
         int result = url.hashCode();
         System.out.println(result);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

讓我們編譯並執行上述程式,這將產生以下結果:

輸出

1169952135

示例 3

以下示例演示了使用 Java URL hashCode() 方法處理具有 file 協議的有效 url。在此示例中,我們正在建立 URL 類的例項。現在使用 hashCode() 方法,我們獲取雜湊碼並列印它。

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();
      }
   }
}

讓我們編譯並執行上述程式,這將產生以下結果:

輸出

1073477724
java_url.htm
廣告
© . All rights reserved.