Java數組操作方法收集(快速判斷某個值在這個數組中)

來源:互聯網
上載者:User

標籤:取值   target   time   int   equals   匯入   val   pre   get   

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

Java數組操作方法收集(快速判斷某個值在這個數組中)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.