Two string arrays A and B, method to find the same element

Question: Two string arrays A and B, a way to find the same elements? Note: A and B arrays are relatively large

The first: Compare A and B to find the elements that are not the same in B and A, and then subtract the elements that are not the same in B and A from B.

    The result is the same elements in A and B.

The second type: Use the properties of the Map collection to use the data of array A as the key and value of the map, and then use the data of B to take the value, not NULL, the description is the same.

Code directly below

    /**
     * Basic idea: same = set B- (set B- same in set A = different)
     *
     * @return
     */
    public List<String> getSameList(String[] strArr1, String[] strArr2) {
        // 数组A
        List<String> coll = Arrays.asList(strArr1);
        // 数组B
        List<String> coll2 = Arrays.asList(strArr2);

        // 数组B的list
        List<String> alter0 = new ArrayList<String>(coll2);

        // 数组A的List
        List<String> alter1 = new ArrayList<String>(coll);

        // List List<String> of different elements of array B and A 
        alter2 = new ArrayList<String> (coll2);
         // Remove the same element in A from B 
        alter2.removeAll(alter1);

        // Array BList -B and A not used List=same 
        alter0.removeAll(alter2);
        Iterator<String> it2 = alter0.iterator();
        while (it2.hasNext()) {System.out.println ( " The same element in array AB " + 
            it2.next ());
        }
        return alter0;
    }

    /**
     * Use the data of array A as the key and value of the map, and then use the data of B to take the value, not NULL, the same description
     *
     * Map interface Map provides a mapping relationship, in which the elements are stored in the form of key-value pairs, which can quickly find the value according to the key;
     * The key-value pairs in the Map exist in the form of object instances of the Entry type;
     * Build (key value) cannot be repeated, value value can be repeated, one value value can form a corresponding relationship with many key values, and each build can only be mapped to one value at most.
     * Map supports generics, in the form of: Map<K,V> The put(K key,V value) method is used in the Map to add
     *
     * HashMap class HashMap is an important implementation class of Map, and it is also the most commonly used. Entry objects in HashMap are arranged out of order based on hash table implementation.
     * Both the key value and the value value can be null, but a HashMap can only have a map with a null key value (the key value cannot be repeated)
     *
     */
    public List<String> getSameElementByMap(String[] strArr1, String[] strArr2) {

        // HashMap key value cannot be repeated both Key value and value value can be null 
        HashMap<String, Object> map = new HashMap<String, Object> ();

        // The elements in the array A are put into the Map 
        for (String string1 : strArr1) {
            map.put(string1, string1);
        }

        List <String> list = new ArrayList<String> ();
         // Use the array B element as the Key to get the value, if it is NULL, it means the same 
        for (String string2 : strArr2) {
            Object j = map.get(string2);
            if (j != null) {
                list.add(string2);
                // System.out.println("Same element in array AB: "+j.toString()); 
            }
        }
        return list;

    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325910234&siteId=291194637