Guava – Longs.contains()方法与实例
Guava的Longs类的Longs.contains()方法是用来检查一个值,即目标值是否存在于数组中。这个目标值和数组被作为该方法的参数。这个方法返回一个布尔值,说明目标值是否是
语法:
public static boolean contains(long[] array,
long target)
参数: 这个方法接受两个参数。
- array: 这是要搜索目标值的值数组
- target: 这是要检查是否存在的长值。
返回值: 如果对于i的某个值,存在于i True 索引的值等于目标值,则该方法返回 th 异常情况。 False .
该方法不抛出任何异常。
示例1:
// Java code to show implementation of
// Guava's Longs.contains() method
import com.google.common.primitives.Longs;
import java.util.Arrays;
class GFG {
// Driver's code
public static void main(String[] args)
{
// Creating a long array
long[] arr = { 5L, 4L, 3L, 2L, 1L };
long target = 3L;
// Using Longs.contains() method to search
// for an element in the array. The method
// returns true if element is found, else
// returns false
if (Longs.contains(arr, target))
System.out.println("Target is present"
+ " in the array");
else
System.out.println("Target is not "
+ "present in the array");
}
}
输出:
Target is present in the array
示例2:
// Java code to show implementation of
// Guava's Longs.contains() method
import com.google.common.primitives.Longs;
import java.util.Arrays;
class GFG {
// Driver's code
public static void main(String[] args)
{
// Creating a long array
long[] arr = { 2L, 4L, 6L, 8L, 10L };
long target = 7L;
// Using Longs.contains() method to search
// for an element in the array. The method
// returns true if element is found, else
// returns false
if (Longs.contains(arr, target))
System.out.println("Target is present"
+ " in the array");
else
System.out.println("Target is not "
+ "present in the array");
}
}
输出:
Target is not present in the array
参考:
https://google.github.io/guava/releases/21.0/api/docs/com/google/common/primitives/Longs.html#contains-long:A-long-