Java中高效判断数组中是否包含某个元素的几种方法
作者:hollis 时间:2022-02-21 05:46:39
如何检查一个数组(无序)是否包含一个特定的值?这是一个在Java中经常用到的并且非常有用的操作。同时,这个问题在Stack Overflow中也是一个非常热门的问题。在投票比较高的几个答案中给出了几种不同的方法,但是他们的时间复杂度也是各不相同的。本文将分析几种常见用法及其时间成本。
检查数组是否包含某个值的方法
使用List
public static boolean useList(String[] arr, String targetValue) {
return Arrays.asList(arr).contains(targetValue);
}
使用Set
public static boolean useSet(String[] arr, String targetValue) {
Set<String> set = new HashSet<String>(Arrays.asList(arr));
return set.contains(targetValue);
}
使用循环判断
public static boolean useLoop(String[] arr, String targetValue) {
for(String s: arr){
if(s.equals(targetValue))
return true;
}
return false;
}
使用Arrays.binarySearch()
Arrays.binarySearch()方法只能用于有序数组!!!如果数组无序的话得到的结果就会很奇怪。
查找有序数组中是否包含某个值的用法如下:
public static boolean useArraysBinarySearch(String[] arr, String targetValue) {
int a = Arrays.binarySearch(arr, targetValue);
if(a > 0)
return true;
else
return false;
}
时间复杂度
下面的代码可以大概的得出各种方法的时间成本。基本思想就是从数组中查找某个值,数组的大小分别是5、1k、10k。这种方法得到的结果可能并不精确,但是是最简单清晰的方式。
public static void main(String[] args) {
String[] arr = new String[] { "CD", "BC", "EF", "DE", "AB"};
//use list
long startTime = System.nanoTime();
for (int i = 0; i < 100000; i++) {
useList(arr, "A");
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println("useList: " + duration / 1000000);
//use set
startTime = System.nanoTime();
for (int i = 0; i < 100000; i++) {
useSet(arr, "A");
}
endTime = System.nanoTime();
duration = endTime - startTime;
System.out.println("useSet: " + duration / 1000000);
//use loop
startTime = System.nanoTime();
for (int i = 0; i < 100000; i++) {
useLoop(arr, "A");
}
endTime = System.nanoTime();
duration = endTime - startTime;
System.out.println("useLoop: " + duration / 1000000);
//use Arrays.binarySearch()
startTime = System.nanoTime();
for (int i = 0; i < 100000; i++) {
useArraysBinarySearch(arr, "A");
}
endTime = System.nanoTime();
duration = endTime - startTime;
System.out.println("useArrayBinary: " + duration / 1000000);
}
运行结果:
useList: 13
useSet: 72
useLoop: 5
useArraysBinarySearch: 9
使用一个长度为1k的数组
String[] arr = new String[1000];
Random s = new Random();
for(int i=0; i< 1000; i++){
arr[i] = String.valueOf(s.nextInt());
}
结果:
useList: 112
useSet: 2055
useLoop: 99
useArrayBinary: 12
使用一个长度为10k的数组
String[] arr = new String[10000];
Random s = new Random();
for(int i=0; i< 10000; i++){
arr[i] = String.valueOf(s.nextInt());
}
结果:
useList: 1590
useSet: 23819
useLoop: 1526
useArrayBinary: 12
String inputStrs = "北京,天津,上海,四川,湖南,广州,深圳,海南";
String[] strList = inputStrs.split(",");
String target = "上海";//1
String result = Arrays.asList(strList).contains(target) ? "查到咯!" : "没这个城市啊...";
//2Set<String> set = new HashSet<>(Arrays.asList(strList));
System.out.println(set.contains(target)); // true
System.out.println(result); // result="查到咯!"-----------
来源:http://www.hollischuang.com/archives/1269
标签:Java,包含,元素,数组查找
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JAVA布局管理器与面板组合代码实例
2022-04-23 15:48:55
![](https://img.aspxhome.com/file/2023/4/60534_0s.png)
SpringBoot通过自定义注解实现参数校验
2023-09-21 21:11:02
![](https://img.aspxhome.com/file/2023/3/58343_0s.png)
eclipse maven 插件的安装和配置详解
2023-08-24 16:57:01
![](https://img.aspxhome.com/file/2023/8/58298_0s.jpg)
Java实现的求逆矩阵算法示例
2023-05-02 03:02:56
![](https://img.aspxhome.com/file/2023/2/76512_0s.jpg)
Java8 使用 stream().sorted()对List集合进行排序的操作
2022-11-23 20:57:11
![](https://img.aspxhome.com/file/2023/0/71930_0s.jpg)
DUCC配置平台实现一个动态化线程池示例代码
2023-11-28 12:07:39
![](https://img.aspxhome.com/file/2023/4/60354_0s.png)
java中functional interface的分类和使用详解
2021-09-15 15:59:20
关于Java双大括号{{}}的具体使用
2021-05-28 16:32:01
Maven如何打入依赖中指定的部分jar包
2023-09-22 02:50:33
Java发送报文与接收报文的实例代码
2023-07-09 04:44:01
SpringMVC 向jsp页面传递数据库读取到的值方法
2022-03-29 00:51:15
Java实现俄罗斯方块的源码分享
2023-02-17 04:20:48
![](https://img.aspxhome.com/file/2023/2/69902_0s.png)
C#判断一天、一年已经过了百分之多少的方法
2022-07-16 15:23:11
![](https://img.aspxhome.com/file/2023/0/68660_0s.png)
详解Spring Boot Profiles 配置和使用
2021-10-05 22:54:57
关于@Autowired注入依赖失败的问题及解决
2023-11-29 00:50:42
![](https://img.aspxhome.com/file/2023/4/60474_0s.png)
Hadoop运行时遇到java.io.FileNotFoundException错误的解决方法
2021-11-04 11:19:45
![](https://img.aspxhome.com/file/2023/5/64055_0s.png)
英语单词state与status的区别
2021-09-04 13:57:34
详解 Corba开发之Java实现Service与Client
2023-09-23 03:42:48
![](https://img.aspxhome.com/file/2023/3/60183_0s.png)
java中对list分页并显示数据到页面实例代码
2023-05-22 00:39:10
C#与C++ dll之间传递字符串string wchar_t* char* IntPtr问题
2022-09-16 09:34:53