简体   繁体   中英

How to check for a value not in array1 but in array2 in java?

I came across a problem where I need to find array elements that do not exist in array1, however, exists in array2 using java. Unlike SQL, where people can directly use commands "like" or "not like", in java you might need to build a program from scratch. Although, a simple program, it just works. More solutions are welcome!

String[] colours = { "black", "red", "white", "yellow","magenta","mahogini"};
String[] colours2 = { "black", "red","orange","yellow","Yellow","pink","purple","indigo","nuque" };

ArrayList<String> coloursExist = new ArrayList<String>();
        ArrayList<String> coloursDoNotExist = new ArrayList<String>();
    
    int unmatch=0;
    int max_length= colours.length;
    for (int i = 0; i < colours2.length; i++) {
        for (int j = 0; j < colours.length; j++) {
            if (colours[j].contains(colours2[i])) {
                // System.out.println(colours2[i]+" exists in colours");
                coloursExist.add(colours2[i]);
                break;
            }
            
            unmatch++;
            if (unmatch==max_length) {
                coloursDoNotExist.add(colours2[i]);
            
            }
        }//end of inner for loop
        unmatch=0;
    }//end of for loop

Result:

[black, red, yellow]
[orange, Yellow, pink, purple, indigo, nuque]
 

Use the removeAll() method of List , which is like a set relative-complement :

String[] colours = { "black", "red", "white", "yellow","magenta","mahogini"};
String[] colours2 = { "black", "red","orange","yellow","Yellow","pink","purple","indigo","nuque" };

List<String> list = new ArrayList<>(Arrays.asList(colours2));
list.removeAll(Arrays.asList(colours));

System.out.println(list);

Output

[orange, Yellow, pink, purple, indigo, nuque]

You can achieve the same using built-in method List.removeAll and List.retainAll

String[] colours = {"black", "red", "white", "yellow", "magenta", "mahogini"};
String[] colours2 = {"black", "red", "orange", "yellow", "Yellow", "pink", "purple", "indigo", "nuque"};

List<String> colList = Arrays.asList(colours);
List<String> col2List = Arrays.asList(colours2);

List<String> coloursExist = new ArrayList<>(colList);
coloursExist.retainAll(col2List);

List<String> coloursDoNotExist = new ArrayList<>(col2List);
coloursDoNotExist.removeAll(colList);

System.out.println(coloursExist); // [black, red, yellow]
System.out.println(coloursDoNotExist); //[orange, Yellow, pink, purple, indigo, nuque]

Hope this helps :

import java.util.*;  

public class Main {
    
    public static void main(String[] args) {
        String[] colours = { "black", "red", "white", "yellow","magenta","mahogini"};
        String[] colours2 = { "black", "red","orange","yellow","Yellow","pink","purple","indigo","nuque" };
        
        ArrayList<String> coloursList1 = new ArrayList(Arrays.asList(colours));
        ArrayList<String> coloursList2 = new ArrayList(Arrays.asList(colours2));
        
        ArrayList<String> tempColoursList1 = new ArrayList(Arrays.asList(colours));
        ArrayList<String> tempColoursList2 = new ArrayList(Arrays.asList(colours2));

        tempColoursList2.removeAll(tempColoursList1);
        System.out.println("Uncommon elements : " + tempColoursList2);
        coloursList1.retainAll(coloursList2);
        System.out.println("Common elements   : " + coloursList1);
    }
}

Output

Uncommon elements : [orange, Yellow, pink, purple, indigo, nuque]                                                                                           
Common elements   : [black, red, yellow] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM