📌  相关文章
📜  Java中的 CompoundName hashCode() 方法和示例

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

Java中的 CompoundName hashCode() 方法和示例

javax.naming.CompoundName 类hashCode()方法用于返回此复合名称的哈希码。该复合名称对象的哈希码是该复合名称的各个组成部分的“规范化”形式的哈希码的总和。

句法:

public int hashCode()

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

返回值:该方法返回一个int,表示该名称的哈希码。

下面的程序说明了 CompoundName.hashCode() 方法:
方案一:

// Java program to demonstrate
// CompoundName.hashCode()
  
import java.util.Properties;
import javax.naming.CompoundName;
import javax.naming.InvalidNameException;
  
public class GFG {
    public static void main(String[] args)
        throws InvalidNameException
    {
  
        // need properties for CompoundName
        Properties props = new Properties();
        props.put("jndi.syntax.separator", ":");
        props.put("jndi.syntax.direction",
                  "left_to_right");
  
        // create compound name object
        CompoundName CompoundName1
            = new CompoundName(
                "a:b:z:y:x", props);
  
        // apply hashCode()
        int hashCode
            = CompoundName1.hashCode();
  
        // print value
        System.out.println("hashCode:"
                           + hashCode);
    }
}
输出:
hashCode:558

方案二:

// Java program to demonstrate
// CompoundName.hashCode() method
  
import java.util.Properties;
import javax.naming.CompoundName;
import javax.naming.InvalidNameException;
  
public class GFG {
    public static void main(String[] args)
        throws InvalidNameException
    {
  
        // need properties for CompoundName
        Properties props = new Properties();
        props.put("jndi.syntax.separator", "/");
        props.put("jndi.syntax.direction",
                  "left_to_right");
  
        // create compound name object
        CompoundName CompoundName1
            = new CompoundName(
                "c/e/d/v/a/b/z/y/x/f",
                props);
  
        // apply hashCode()
        int hashCode
            = CompoundName1.hashCode();
  
        // print value
        System.out.println("hashCode:"
                           + hashCode);
    }
}
输出:
hashCode:1078

参考资料:https://docs.oracle.com/javase/10/docs/api/javax/naming/CompoundName.html#hashCode()