1

Is there a quick way in Painless to compare the values of one array to another? I'm trying to avoid a long loop statement. I'm looking for something that will evaluate as the following two items:

ARRAY[1,4,3] contains ARRAY[3,1] = true
ARRAY[2,7] is contained by ARRAY[1,7,4,2,6] = true
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
EricPSU
  • 163
  • 1
  • 7

1 Answers1

2

You can use the following code to achieve what you want:

Arrays.asList(biggerArray).containsAll(Arrays.asList(smallerArray))

In your case, both of the following statements will yield true:

Arrays.asList(new Integer[]{1,4,3}).containsAll(Arrays.asList(new Integer[]{3,1}))
Arrays.asList(new Integer[]{1,7,4,2,6}).containsAll(Arrays.asList(new Integer[]{2,7}))
Val
  • 207,596
  • 13
  • 358
  • 360
  • Is there a way to get the set difference between the two lists? In the context of the above example, is there a way to get a list of elements that belong in the first list but not the second, and vice versa? – Ned_the_Dolphin Mar 01 '22 at 00:00
  • 1
    @Ned_the_Dolphin you can leverage the `removeAll()` method: `Arrays.asList(new Integer[]{1,7,4,2,6}).removeAll(Arrays.asList(new Integer[]{2,7}))` leaves you with an array containing 1, 4 and 6 ` – Val Mar 01 '22 at 04:18