I have two arrays of strings:
String[] A = {"AQBC","BSA","BAA"}
String[] B = {"AWF","AQBC","SSA","SFW","BSA","WQQR","WEWQ","BAA","RDR","GGWQ","GQEW"}
I want to check if all elements of array A are in array B. What is the easiest way to do it?
Converts your arrays into set to remove possible duplicated elements ans then containsAll() returns true if setB contains all of the elements of setA:
Set<T> setA = new HashSet<T>(Arrays.asList(A));
Set<T> setB = new HashSet<T>(Arrays.asList(B));
setB.containsAll(setA)
You can try this is to make String Array A and B compare in a array. and sort them
String[] A = {"AQBC","BSA","BAA"}
String[] B = {"AWF","AQBC","SSA","SFW","BSA","WQQR","WEWQ","BAA","RDR","GGWQ","GQEW"}
compareStringArray(A, B, true);
public static void printArrayListOfString(List Al) {
for (int i = 0; i < Al.size(); i++) {
if (i != 0) {
System.out.print(", ");
}
System.out.print(Al.get(i));
}
System.out.println();
}
Sets; buildsetAandsetBand check thatsetB.containsAll(setA)