zoukankan      html  css  js  c++  java
  • Java数组操作方法收集(快速判断某个值在这个数组中)

    Java数组操作最高效的方式是循环取值,如果转换成集合那么就会分配内存,效率不如前者,但是方法多,需要在性能调优上去权衡。切记:数组是数组,集合是集合。

    下面是收集最常用的数组转成集合的操作方法:

    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类型中,首先要将数组元素遍历一遍,然后再使用集合类做其他操作。
     */

    使用时要导入下面的包:

    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.6</version>
    </dependency>

    参考:

    http://www.cnblogs.com/ipetergo/p/6429747.html(以上内容转自此篇文章)

    http://www.importnew.com/18700.html

  • 相关阅读:
    服务器变量 $_SERVER 详解
    PHP 函数功能参考
    ecshop后台0day漏洞原理+利用方法 XSS+Getshll
    CSRF漏洞原理说明与利用方法
    Drupal 远程命令执行漏洞(CVE-2018-7600)
    SSH登陆验证绕过漏洞(cve-2018-10933)
    单元二:建立和维护数据表
    单元一:认识数据库系统
    【 模块1 认识计算机 】1.2 认识微型计算机
    【 模块1 认识计算机 】 1.1走进计算机世界
  • 原文地址:https://www.cnblogs.com/EasonJim/p/7719949.html
Copyright © 2011-2022 走看看