当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java CompoundName compareTo()用法及代码示例


javax.naming.CompoundName类的compareTo()方法用于比较此CompoundName与作为参数传递的指定对象。当此CompoundName对象小于,等于或大于给定Object作为参数时,它将返回负整数,零或正整数。如果传递的对象为null或不是CompoundName的实例,则此方法将引发ClassCastException。

用法:

public int compareTo(Object obj)

参数:此方法接受obj,它是要与之比较的非null对象。



返回值:此方法返回负整数,零或正整数,因为此Name小于,等于或大于给定的Object。

异常:如果传递的obj不是CompoundName对象,则此方法将引发ClassCastException。

以下示例程序旨在说明CompoundName.compareTo()方法:
程序1:

// Java program to demonstrate 
// CompoundName.compareTo() 
  
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 compareTo() 
        int value 
            = CompoundName1 
                  .compareTo(CompoundName2); 
  
        // print value 
        if (value > 0) 
            System.out.println( 
                "CompoundName1 is "
                + "greater than CompoundName2"); 
        else if (value < 0) 
            System.out.println( 
                "CompoundName1 is "
                + "smaller than CompoundName2"); 
        else
            System.out.println( 
                "CompoundName1 is "
                + "equal to CompoundName2"); 
    } 
}
输出:
CompoundName1 is greater than CompoundName2

程序2:

// Java program to demonstrate 
// CompoundName.compareTo() 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 compareTo() 
        int value 
            = CompoundName1 
                  .compareTo(CompoundName2); 
  
        // print value 
        if (value > 0) 
            System.out.println( 
                "CompoundName1 is "
                + "greater than CompoundName2"); 
        else if (value < 0) 
            System.out.println( 
                "CompoundName1 is "
                + "smaller than CompoundName2"); 
        else
            System.out.println( 
                "CompoundName1 is "
                + "equal to CompoundName2"); 
    } 
}
输出:
CompoundName1 is equal to CompoundName2

参考文献:https://docs.oracle.com/javase/10/docs/api/javax/naming/CompoundName.html#compareTo(java.lang.Object)




相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 CompoundName compareTo() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。