zoukankan      html  css  js  c++  java
  • 如何高效检查一个数组中是否包含某个值?


    1. public static boolean useList(String[] arr, String targetValue) { return Arrays.asList(arr).contains(targetValue); }
    2. public static boolean useSet(String[] arr, String targetValue) {
        Set<String> set = new HashSet<String>(Arrays.asList(arr));
        return set.contains(targetValue);
    }
    3. public static boolean useLoop(String[] arr, String targetValue) {
        for (String s : arr) {
            if (s.equals(targetValue)) {
                return true;
            }
        }
        return false;
    }
    4。下面的代码是错误。但是,为了这个回答的完整性,还是将其列在这里。binarySearch()方法仅能用于已排序的数组。不过,你将会被下面的结果所震惊。
    public static boolean useArraysBinarySearch(String[] arr, String targetValue) {
        int a = Arrays.binarySearch(arr, targetValue);
        if (a > 0) {
            return true;
        } else {
            return false;
        }
    }
    10K的数据
    useList:  1678
    useSet:  25609
    useLoop:  1802
    useArrayBinary:  10
     
  • 相关阅读:
    【ZJOI2017】树状数组
    【ZJOI2014】力
    【WC2017】挑战
    kube event 事件监控
    k8s nginx-ingress 504 timeout
    k8s 工具集
    jvm 性能调优工具之 jmap
    Elasticsearch unassigned 故障排查
    harbor API 与tag 清理
    前后端分离文档
  • 原文地址:https://www.cnblogs.com/feiyuanxing/p/5121583.html
Copyright © 2011-2022 走看看