Skip to main content
Post Undeleted by Jacob G.
Post Deleted by Jacob G.
added 40 characters in body
Source Link
Jacob G.
  • 29.8k
  • 7
  • 70
  • 122

An easy way to go about this is to convert each array into a Set, and then use Set#retainAll to determine equality.

int[] a = {1, 2, 3, 4, 5}; int[] b = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Set<Integer> A = Arrays.stream(a).boxed().collect(Collectors.toSet()); Set<Integer> B = Arrays.stream(b).boxed().collect(Collectors.toSet()); B.retainAll(A); System.out.println(B.equalscontainsAll(A)); 

This will print true if every element in A is in B, otherwise false.

Thanks to shmosel for recommending Set#containsAll!

An easy way to go about this is to convert each array into a Set, and then use Set#retainAll to determine equality.

int[] a = {1, 2, 3, 4, 5}; int[] b = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Set<Integer> A = Arrays.stream(a).boxed().collect(Collectors.toSet()); Set<Integer> B = Arrays.stream(b).boxed().collect(Collectors.toSet()); B.retainAll(A); System.out.println(B.equals(A)); 

This will print true if every element in A is in B, otherwise false.

An easy way to go about this is to convert each array into a Set, and then use Set#retainAll to determine equality.

int[] a = {1, 2, 3, 4, 5}; int[] b = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Set<Integer> A = Arrays.stream(a).boxed().collect(Collectors.toSet()); Set<Integer> B = Arrays.stream(b).boxed().collect(Collectors.toSet()); System.out.println(B.containsAll(A)); 

This will print true if every element in A is in B, otherwise false.

Thanks to shmosel for recommending Set#containsAll!

Source Link
Jacob G.
  • 29.8k
  • 7
  • 70
  • 122

An easy way to go about this is to convert each array into a Set, and then use Set#retainAll to determine equality.

int[] a = {1, 2, 3, 4, 5}; int[] b = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Set<Integer> A = Arrays.stream(a).boxed().collect(Collectors.toSet()); Set<Integer> B = Arrays.stream(b).boxed().collect(Collectors.toSet()); B.retainAll(A); System.out.println(B.equals(A)); 

This will print true if every element in A is in B, otherwise false.