zoukankan      html  css  js  c++  java
  • 关于数组或集合中判断存在某个元素

    怎么判断一个数组或者集合中存在某个特定的元素:

    转自:http://zhaoningbo.iteye.com/blog/1285332

     1 import java.io.Serializable;
     2 import java.util.ArrayList;
     3 import java.util.Arrays;
     4 import java.util.Collection;
     5 import java.util.regex.Matcher;
     6 import java.util.regex.Pattern;
     7 
     8 public class Test implements Serializable {
     9 
    10     private static final long serialVersionUID = 2640934692335200272L;
    11 
    12     public static void main(String[] args) {
    13 
    14         // data segment
    15         String[] SAMPLE_ARRAY = new String[] { "aaa", "solo", "king" };
    16         String TEST_STR = "king";
    17         Collection TEMPLATE_COLL = new ArrayList();
    18         TEMPLATE_COLL.add("aaa");
    19         TEMPLATE_COLL.add("solo");
    20         TEMPLATE_COLL.add("king");
    21         // <- data segment
    22 
    23         // 1, 字符串数组是否存在子元素
    24         // 1-1, 直接使用API
    25         Arrays.sort(SAMPLE_ARRAY);
    26         int index = Arrays.binarySearch(SAMPLE_ARRAY, TEST_STR);
    27         System.out.println("1-1_sort-binarySearche:"
    28                 + ((index != -1) ? true : false));
    29 
    30         // 1-2, 使用正则(因Arrays.toString()引入了“, [ ]”故只在有限环境下可靠)
    31         String tmp = Arrays.toString(SAMPLE_ARRAY);
    32         Pattern p = Pattern.compile("king");
    33         Matcher m = p.matcher(tmp);
    34         System.out.println("1-2_toString-Regex:" + m.find());
    35 
    36         // 1-3, 都会写循环,略过。
    37         // TODO: 循环数据依次比对,此处略去5行代码。
    38 
    39         // 2, 集合是否存在子元素
    40         // 2-1, 最常用的contains
    41         System.out.println("2-1_contains:" + TEMPLATE_COLL.contains(TEST_STR));
    42 
    43         // 2-1-1, 扩展:
    44         // 按模板集合,将当前集合分为“模板已存在”与“不存在”两个子集。
    45         Collection coll = new ArrayList<String>();
    46         coll.add("aaa");
    47         coll.add("bbb");
    48         coll.add("ccc");
    49         // 完整复制集合
    50         Collection collExists = new ArrayList(coll);
    51         Collection collNotExists = new ArrayList(coll);
    52 
    53         collExists.removeAll(TEMPLATE_COLL);
    54         System.out.println("2-1-1_removeAll[exist]:" + collExists);
    55         collNotExists.removeAll(collExists);
    56         System.out.println("2-1-1_removeAll[notexist]:" + collNotExists);
    57 
    58     }
    59 }

    结果输出:

    1-1_sort-binarySearche:true
    1-2_toString-Regex:true
    2-1_contains:true
    2-1-1_removeAll[exist]:[bbb, ccc]
    2-1-1_removeAll[notexist]:[aaa]

    小结:

      1)数组至少三种: 
        A)binarySearch(,)。但条件是需要事先排序,开销需要考虑。 
        B)Regex。但需要将数组转为字符串,Arrays类提供的方法会引入“, [ ]”这三种分割符,可能影响判定结果。 
        C)循环比对。 

      2)集合至少两种: 
        A)循环。如果只是判定默认存在(非定制型存在),建议直接不考虑。 
        B)contains。能靠过来就果断靠吧。 

      3)集合提供了类似“加减”的运算,可以留意一下。 

  • 相关阅读:
    下载地址jquery upload file demo (C#)
    特征卷积基于3D卷积神经网络的人体行为理解(论文笔记)
    应用目录S5PV210的BL1应用
    手机音效手机测试游戏类
    metadata查询Querying Metadata
    arraynodeSorting
    functionclass[LeetCode]Path Sum II
    functionclass[LeetCode]Permutation Sequence
    exceptionfunction[LeetCode]Permutations
    exceptionfunction[LeetCode]Permutations II
  • 原文地址:https://www.cnblogs.com/DreamDrive/p/4724936.html
Copyright © 2011-2022 走看看