豁然高

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

Java学习--数组--判断数组中是否包含某个元素的方法

复制代码
package zaLearnpackage;
import org.apache.commons.lang3.ArrayUtils;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

//检查数组是否包含某个值的方法
public class TestArray {
    //使用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;
    }
    //查找有序数组中是否包含某个值的用法
    public static boolean useArraysBinarySearch(String[] arr,String targetValue){
        int a=Arrays.binarySearch(arr, targetValue);
        if(a>0)
            return true;
        else
            return false;
    }
    //使用ArrayUtils
    public static boolean useArrayUtils(String[] arr,String targetValue){
        return ArrayUtils.contains(arr,targetValue);
    }
    public static void main(String[] args) {
        String[] arr=new String[]{"CD","BC","EF","DE","AB","JK"};
        //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
        long startTime2=System.nanoTime();
        for(int i=0;i<100000;i++){
            useSet(arr, "A");
        }
        long endTime2=System.nanoTime();
        long duration2=endTime2-startTime2;
        System.out.println("useSet:"+duration/1000000);
        //use loop
        long startTime3=System.nanoTime();
        for(int i=0;i<100000;i++){
            useLoop(arr, "A");
        }
        long endTime3=System.nanoTime();
        long duration3=endTime3-startTime3;
        System.out.println("useLoop:"+duration/1000000);
        //use Arrays.binarySearch()
        long startTime4=System.nanoTime();
        for(int i=0;i<100000;i++){
            useArraysBinarySearch(arr, "A");
        }
        long endTime4=System.nanoTime();
        long duration4=endTime4-startTime4;
        System.out.println("useArraysBinarySearch:"+duration/1000000);
    }
}
/*
 * 显然,使用一个简单的循环方法比使用任何集合都更加高效。
 * 许多开发人员为了方便,都使用第一种方法,但是他的效率也相对较低。
 * 因为将数组压入Collection类型中,首先要将数组元素遍历一遍,然后再使用集合类做其他操作。
 */
复制代码

 

posted on   豁然高  阅读(5746)  评论(0编辑  收藏  举报

编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示