Quivalent method to array_diff() in JAVA

Asked

Viewed 164 times

1

In PHP there is the method array_diff() which verifies values of two arrays and returns the items referring to the difference between them. For example:

$arrExclusion = array('as', 'coisas', 'acontece', 'no', 'tempo', 'certo');

$arr = array('tudo', 'as', 'coisas' 'acontece', 'me', 'é', 'no', 'lícito',
'mas', 'seu', 'nem', 'tudo', 'tempo', 'me', 'convém', 'certo' );

$new_array = array_diff($arr, $arrExclusion);

The values of array $new_array evening:

''everything', 'me', 'me', 'lawful', 'but', 'not', 'all', 'me', 'it suits'

Is there any method equivalent to array_diff() in JAVA? If yes, which one? If not, how could I perform this procedure?

  • 1

    http://stackoverflow.com/questions/1235033/java-comparing-two-string-arrays-and-removing-elements-that-exist-in-both-array

2 answers

1


You can use the library Commons Collections the method removeAll class Collectionutils removing the elements of one array from another, thus:

List<String> arrExclusion = Arrays.asList("as", "coisas", "acontece", "no", "tempo", "certo");

List<String> arr = Arrays.asList("tudo", "as", "coisas", "acontece", "me", "é", "no", "lícito","mas", "seu", "nem", "tudo", "tempo", "me", "convém", "certo");

Collection<String> new_array = CollectionUtils.removeAll(arr, arrExclusion);

Generating the following result:

[everything, me, is, lawful, but, you, not everything, me, it suits]

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.