Monday, 25 April 2016

How to check an array of values with another array ?


Scenario
I have two arrays and I want to check, if the values of second array are present in the first array.

Solution
First, convert arrays into arrayList, then use containsAll method to check if all elements of second array are present in first array.

Example
Integer[] array1 = { 1,2,3 };
Integer[] array2 = { 2,3 };

List list1 = Arrays.asList(array1);
List list2 = Arrays.asList(array2);

if(list1.containsAll(list2)) {
  System.out.println("Yo Yo ! They are there !");
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.