簡體   English   中英

java - 如何在java中檢查不在array1中但在array2中的值?

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

我遇到了一個問題,我需要使用java查找array1中不存在的數組元素,但是array2中存在。 與 SQL 不同,人們可以直接使用“like”或“not like”命令,在 Java 中,您可能需要從頭開始構建程序。 雖然,一個簡單的程序,它只是工作。 歡迎提供更多解決方案!

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

結果:

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

使用ListremoveAll()方法,它就像一個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);

輸出

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

您可以使用達到相同的內置方法List.removeAllList.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]

希望這可以幫助 :

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);
    }
}

輸出

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM