在Java中判断数组中包含某个元素的几种方式的比较
2016-11-04 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
直接上代码:
package test.contain.lishaojie;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class TestContain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] arr = new String[] { "DD", "CC", "DD", "FF", "KK"};
String target ="A";
int times = 1000;//次数
//转换成list方式
long startTime = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
ByList(arr, target);
}
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
System.out.println("list方式: " + duration / 1000000);
//转换成set方式
startTime = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
BySet(arr, target);
}
endTime = System.currentTimeMillis();
duration = endTime - startTime;
System.out.println("set方式: " + duration / 1000000);
//直接循环方式
startTime = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
ByForLoop(arr, target);
}
endTime = System.currentTimeMillis();
duration = endTime - startTime;
System.out.println("循环方式: " + duration / 1000000);
//二分法查找
startTime = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
ByArraysBinarySearch(arr, target);
}
endTime = System.currentTimeMillis();
duration = endTime - startTime;
System.out.println("二分法查找: " + duration / 1000000);
}
public static boolean ByList(String[] arr, String targetValue) {
return Arrays.asList(arr).contains(targetValue);
}
public static boolean BySet(String[] arr, String targetValue) {
Set<String> set = new HashSet<String>(Arrays.asList(arr));
return set.contains(targetValue);
}
public static boolean ByForLoop(String[] arr, String targetValue) {
for(String s: arr){
if(s.equals(targetValue))
return true;
}
return false;
}
public static boolean ByArraysBinarySearch(String[] arr, String targetValue) {
int a = Arrays.binarySearch(arr, targetValue);
if(a > 0)
return true;
else
return false;
}
}
运行结果如下:
list方式: 5
set方式: 22
循环方式: 2
二分法查找: 3
经过大量数据测试循环方式效率最高,其次是二分法,最后是list,和set因为因为将数组压入Collection类型中,首先要将数组元素遍历一遍,然后再使用集合类做其他操作。但是list方式明显要比set方式快很多,这是为什么呢?直接看代码:首先
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
返回的是ArrayList所以set方式还要进行一次操作将ArrayList转换成set,
public HashSet(Collection<? extends E> c) {
map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
addAll(c);
}
之一addAll方法:
public boolean addAll(Collection<? extends E> c) {
boolean modified = false;
for (E e : c)
if (add(e))
modified = true;
return modified;
}
又一次进行了选环,所以效率比较低,binggo
package test.contain.lishaojie;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class TestContain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] arr = new String[] { "DD", "CC", "DD", "FF", "KK"};
String target ="A";
int times = 1000;//次数
//转换成list方式
long startTime = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
ByList(arr, target);
}
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
System.out.println("list方式: " + duration / 1000000);
//转换成set方式
startTime = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
BySet(arr, target);
}
endTime = System.currentTimeMillis();
duration = endTime - startTime;
System.out.println("set方式: " + duration / 1000000);
//直接循环方式
startTime = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
ByForLoop(arr, target);
}
endTime = System.currentTimeMillis();
duration = endTime - startTime;
System.out.println("循环方式: " + duration / 1000000);
//二分法查找
startTime = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
ByArraysBinarySearch(arr, target);
}
endTime = System.currentTimeMillis();
duration = endTime - startTime;
System.out.println("二分法查找: " + duration / 1000000);
}
public static boolean ByList(String[] arr, String targetValue) {
return Arrays.asList(arr).contains(targetValue);
}
public static boolean BySet(String[] arr, String targetValue) {
Set<String> set = new HashSet<String>(Arrays.asList(arr));
return set.contains(targetValue);
}
public static boolean ByForLoop(String[] arr, String targetValue) {
for(String s: arr){
if(s.equals(targetValue))
return true;
}
return false;
}
public static boolean ByArraysBinarySearch(String[] arr, String targetValue) {
int a = Arrays.binarySearch(arr, targetValue);
if(a > 0)
return true;
else
return false;
}
}
运行结果如下:
list方式: 5
set方式: 22
循环方式: 2
二分法查找: 3
经过大量数据测试循环方式效率最高,其次是二分法,最后是list,和set因为因为将数组压入Collection类型中,首先要将数组元素遍历一遍,然后再使用集合类做其他操作。但是list方式明显要比set方式快很多,这是为什么呢?直接看代码:首先
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
返回的是ArrayList所以set方式还要进行一次操作将ArrayList转换成set,
public HashSet(Collection<? extends E> c) {
map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
addAll(c);
}
之一addAll方法:
public boolean addAll(Collection<? extends E> c) {
boolean modified = false;
for (E e : c)
if (add(e))
modified = true;
return modified;
}
又一次进行了选环,所以效率比较低,binggo
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询