0

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?

1
  • The easiest would be to use Sets; build setA and setB and check that setB.containsAll(setA) Commented Apr 12, 2014 at 20:30

3 Answers 3

2
Arrays.asList(B).containsAll(Arrays.asList(A));

The easiest. Not the fastest.

Sign up to request clarification or add additional context in comments.

Comments

2

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)

Comments

0

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.