Java番石榴 |带有示例的 Ints.compare() 方法
Ints.compare()是 Guava 库中 Ints 类的一个方法,用于比较两个给定的int值。它需要两个整数作为要比较的参数。它根据指定整数的比较返回一个比较器值。
句法 :
public static int compare(int a, int b)
参数:此方法有两个参数:
- a:这是要比较的第一个整数值。
- b:这是要比较的第二个整数值。
返回值:此方法返回一个 int 值。它返回:
- 0 如果“a”等于“b”,
- 正值“a”大于“b”,
- 负值“a”小于“b”
下面的程序说明了上述方法的使用:
示例 1:
Java
// Java code to show implementation of
// Guava's Ints.compare() method
import com.google.common.primitives.Ints;
import java.util.List;
class GFG {
// Driver's code
public static void main(String[] args)
{
// Using Ints.compare() method to
// compare the two specified int
// values in the standard way
// This should return positive number
// as a is greater than b
System.out.println(Ints.compare(5, 3));
}
}
Java
// Java code to show implementation of
// Guava's Ints.compare() method
import com.google.common.primitives.Ints;
import java.util.List;
class GFG {
// Driver's code
public static void main(String[] args)
{
// Using Ints.compare() method to
// compare the two specified int
// values in the standard way
// This should return 0 as a == b
System.out.println(Ints.compare(2, 2));
}
}
输出:
1
示例 2:
Java
// Java code to show implementation of
// Guava's Ints.compare() method
import com.google.common.primitives.Ints;
import java.util.List;
class GFG {
// Driver's code
public static void main(String[] args)
{
// Using Ints.compare() method to
// compare the two specified int
// values in the standard way
// This should return 0 as a == b
System.out.println(Ints.compare(2, 2));
}
}
输出:
0