Home  >  Article  >  Java  >  Java determines whether an instance of an element exists in an array or collection

Java determines whether an instance of an element exists in an array or collection

高洛峰
高洛峰Original
2017-01-23 16:47:291567browse

Introduction:

Today a friend in the group asked "How to know whether an array collection already exists the current object?" Everyone knows about circular comparison, including me, a great group member. Is there any other way? Let’s take a look at this article.

Text:

The people you can find here are all programmers. It should be clearer to directly enter the code.

import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.Collection; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 
  
public class Test implements Serializable { 
  
  private static final long serialVersionUID = 2640934692335200272L; 
  
  public static void main(String[] args) { 
  
    // data segment 
    String[] SAMPLE_ARRAY = new String[] { "aaa", "solo", "king" }; 
    String TEST_STR = "king"; 
    Collection TEMPLATE_COLL = new ArrayList(); 
    TEMPLATE_COLL.add("aaa"); 
    TEMPLATE_COLL.add("solo"); 
    TEMPLATE_COLL.add("king"); 
    // <- data segment 
  
    // 1, 字符串数组是否存在子元素 
    // 1-1, 直接使用API 
    Arrays.sort(SAMPLE_ARRAY); 
    int index = Arrays.binarySearch(SAMPLE_ARRAY, TEST_STR); 
    System.out.println("1-1_sort-binarySearche:"
        + ((index != -1) ? true : false)); 
  
    // 1-2, 使用正则(因Arrays.toString()引入了“, [ ]”故只在有限环境下可靠) 
    String tmp = Arrays.toString(SAMPLE_ARRAY); 
    Pattern p = Pattern.compile("king"); 
    Matcher m = p.matcher(tmp); 
    System.out.println("1-2_toString-Regex:" + m.find()); 
  
    // 1-3, 都会写循环,略过。 
    // TODO: 循环数据依次比对,此处略去5行代码。 
  
    // 2, 集合是否存在子元素 
    // 2-1, 最常用的contains 
    System.out.println("2-1_contains:" + TEMPLATE_COLL.contains(TEST_STR)); 
  
    // 2-1-1, 扩展: 
    // 按模板集合,将当前集合分为“模板已存在”与“不存在”两个子集。 
    Collection coll = new ArrayList<String>(); 
    coll.add("aaa"); 
    coll.add("bbb"); 
    coll.add("ccc"); 
    // 完整复制集合 
    Collection collExists = new ArrayList(coll); 
    Collection collNotExists = new ArrayList(coll); 
  
    collExists.removeAll(TEMPLATE_COLL); 
    System.out.println("2-1-1_removeAll[exist]:" + collExists); 
    collNotExists.removeAll(collExists); 
    System.out.println("2-1-1_removeAll[notexist]:" + collNotExists); 
  
  } 
  
}

Running results:

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]

Let’s summarize~. =

1) There are at least three types of arrays:

A) binarySearch(,). But the condition is that it needs to be sorted in advance, and the overhead needs to be considered.
B) Regex. However, the array needs to be converted into a string. The method provided by the Arrays class will introduce the three delimiters ", [ ]", which may affect the determination result.
C) Circular comparison.

2) There are at least two types of collections:

A) Loop. If it is only determined to exist by default (non-customized existence), it is recommended not to consider it directly.
B) contains. If you can get closer, do it decisively.

3) Sets provide operations similar to "addition and subtraction", so you can pay attention to them.

The above is the entire content of the java examples brought by the editor to determine whether an element exists in an array or collection. I hope you will support the PHP Chinese website~

More java determination arrays Or whether there is an instance of a certain element in the collection. For related articles, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn