Java数组操作方法收集(快速判断某个值在这个数组中)
2017-10-24 00:12
519 查看
Java数组操作最高效的方式是循环取值,如果转换成集合那么就会分配内存,效率不如前者,但是方法多,需要在性能调优上去权衡。切记:数组是数组,集合是集合。
下面是收集最常用的数组转成集合的操作方法:
使用时要导入下面的包:
参考:
http://www.cnblogs.com/ipetergo/p/6429747.html(以上内容转自此篇文章)
http://www.importnew.com/18700.html
下面是收集最常用的数组转成集合的操作方法:
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中数组判断元素存在几种方式比较
- Java判断一个对象是否是数组
- 12、Java入门—将一数组乱序排列的三种方法 (快速洗牌的小算法)
- JAVA基础 - 数组中有没有length()这个方法?String中有没有 length()这个方法?
- java笔记:Java字符串数组中判断是否存在某元素的方法
- Java中对数组的两种操作方法Arrays、foreach()
- java 编写程序实现从控制台接收一个 5 位以上的整数,使用数组来判断该数字 * 是否是回文数。(例如:789987,12344321是回文数)
- Java判断对象或者原始数据类型是否是数组
- Java判断对象或者原始数据类型是否是数组
- Java循环判断数组中是否包含字符串
- 在Java中如何高效判断数组中是否包含某个元素
- java 数组的排序,含冒泡、插入、选择、快速排序。
- 在Java中如何高效的判断数组中是否包含某个元素
- 通过递归判断一个整形数组是否按升序排列(java实现)
- C/C++面试题(三) 推断二叉树、快速排序递归实现、递归判断数组递增
- Java判断一个对象是否是数组
- java判断循环数组是否连续并获取不连续的项以及重复项
- Java-数组转List-判断一个数是否存在数组/List中
- 2--判断该数组中是否存在这个整数
- Java面试题:1-100之间的数,被放在数组a[99]中,有一个数没有包含在其中,用java代码找出这个数