Java中的 CompoundName equals() 方法及示例
javax.naming.CompoundName 类的equals()方法用于将此 CompoundName 与作为参数传递的指定对象进行比较,并检查两个对象是否相等。如果两个对象相等,则 equals() 方法返回 true,否则返回 false。如果传递的 obj 为 null 或不是复合名称,则该方法返回 false。如果一个复合对象中的每个组件都等于另一个复合对象中的相应组件,则两个复合对象是相等的。
句法:
public boolean equals(Object obj)
参数:此方法接受obj ,它是要比较的非空对象。
返回值:如果 obj 等于该复合名称,则此方法返回 true,否则返回 false。
下面的程序说明了 CompoundName.equals() 方法:
方案一:
// Java program to demonstrate
// CompoundName.equals()
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(
"x:y:z", props);
CompoundName CompoundName2
= new CompoundName(
"x:y:m", props);
// apply equals()
boolean flag
= CompoundName1
.equals(CompoundName2);
// print value
if (flag)
System.out.println(
"CompoundName1 is "
+ "equal to CompoundName2");
else
System.out.println(
"CompoundName1 is "
+ "not equal to CompoundName2");
}
}
输出:
CompoundName1 is not equal to CompoundName2
方案二:
// Java program to demonstrate
// CompoundName.equals() 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(
"x@y@z@M@n", props);
CompoundName CompoundName2
= new CompoundName(
"x@y@z@M@n", props);
// apply equals()
boolean flag
= CompoundName1.equals(CompoundName2);
// print value
if (flag)
System.out.println(
"CompoundName1 is "
+ "equal to CompoundName2");
else
System.out.println(
"CompoundName1 is "
+ "not equal to CompoundName2");
}
}
输出:
CompoundName1 is equal to CompoundName2
参考文献:https://docs.oracle.com/javase/10/docs/api/javax/naming/CompoundName.html#equals(Java.lang.Object)