1

Say I have two string arrays:

String[] first = new String[]{"12","23","44","67"};
String[] second= new String[]{"12","22","46","67"};

I searched for a function like PHP's array_diff which will give me the difference of these two arrays like this:

{"23","44"}

Is there a in-built function for this operation, or should I create a for loop and check for the differences ?

6
  • no there is no built in function. you can create a comparator for that. Commented May 13, 2013 at 10:50
  • Unrelated to your question: You should consider using List<String> first = new ArrayList<String>(Arrays.asList("12", "23"); instead of String[]. Commented May 13, 2013 at 10:51
  • @MichaWiedenmann Arrays.asList returns a List, why would you pass this to a List constructor? Second, depending on the use case an array is a perfectly acceptable data structure. Commented May 13, 2013 at 10:54
  • If the second list was "12","22","23","67", what would he diff be? Commented May 13, 2013 at 10:55
  • @JohnB Since it depends I was careful to use the word "consider". Similarly for Arrays.asList, the returned list implements some of the methods (e.g. add()) of the List interface by throwing an exception. I do not know the circumstances under which the OP wants to use the Strings and therefore choose the more flexible version. Commented May 13, 2013 at 11:09

3 Answers 3

4

You can create two Sets from these arrays, like:

List<String> firstList = Arrays.asList(first);
List<String> secondList = Arrays.asList(second);

Set<String> firstSet = new HashSet<String>(first);
Set<String> secondSet = new HashSet<String>(second);  

and then use the removeAll method:

firstSet.removeAll(secondList);
secondSet.removeAll(firstList);

so now firstList contains all the elements that are only available in the first array and secondList only the elements available in the second array.

A set that will contain only the elements available in one of the sets (without elements available in both sets) can be created using:

new HashSet<String>(firstSet).addAll(secondSet);
Sign up to request clarification or add additional context in comments.

1 Comment

This code Set<String> firstSet = new HashSet<String>(first); gives "Connot resolve constructor Hashset(java.lang.String[])" error. Can Hashset be constructed like this?
3

Guava's Sets class has a difference method.

so

Set<String> diff = Sets.difference(newHashSet(first), newHashSet(second));

2 Comments

I didn't know Guava, Also thank you for valuable library info.
1

PHP arrays are not arrays at all, that's why there is such weird method for diff.

If you want difference between two sets (A - B) in mathematical sense, then

1) use sets

Set<Integer> set1 = new HashSet<Integer>();
Set<Integer> set2 = new HashSet<Integer>();

2) use difference method (contains all elements in set1 that not in set2)

set1.removeAll(set2)

Note, this is assymetric difference.

2 Comments

Doesn't removeAll returns a boolean rather then set elements?
@trante Yes. But it also modifies set1 according to set1 - set2

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.