📜  Java中的 StringWriter hashCode() 方法及示例

📅  最后修改于: 2022-05-13 01:55:43.512000             🧑  作者: Mango

Java中的 StringWriter hashCode() 方法及示例

Java中StringWriter类的hashCode()方法用于获取这个StringWriter实例的HashCode值。此方法不接受任何参数并返回所需的 int 值。

句法:

public int hashCode()

参数:此方法接受不接受任何参数。

返回值:此方法返回一个 int 值,它是 StringWriter 实例的 HashCode 值。

下面的方法说明了 hashCode() 方法的工作:

方案一:

// Java program to demonstrate
// StringWriter hashCode() method
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
  
            // Create a StringWriter instance
            StringWriter StringWriter
                = new StringWriter();
  
            // Get the String
            // to be written in the stream
            String string = "GeeksForGeeks";
  
            // Write the string
            // to this StringWriter using write() method
            StringWriter.write(string);
  
            // Get the HashCode value using hashCode()
            System.out.println("HashCode value: "
                               + StringWriter.hashCode());
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
输出:
HashCode value: 589431969

方案二:

// Java program to demonstrate
// StringWriter hashCode() method
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
  
            // Create a StringWriter instance
            StringWriter StringWriter
                = new StringWriter();
  
            // Get the String
            // to be written in the stream
            String string = "GFG";
  
            // Write the string
            // to this StringWriter using write() method
            StringWriter.write(string);
  
            // Get the HashCode value using hashCode()
            System.out.println("HashCode value: "
                               + StringWriter.hashCode());
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
输出:
HashCode value: 589431969