import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
public class ComputesTheArray {
public static void main(String[] args) {
// TODO Auto-generated method stub
Collection smallCollection = java.util.Arrays.asList("asdf",
"java");
Collection bigCollection = java.util.Arrays.asList("asdf",
"java");
System.out.println(java.util.Arrays
.toString(computeDifferenceIndices(smallCollection,
bigCollection)));
}
/**
* Computes the array of element indices which where added to a collection.
*
* @param smallCollection
* the original collection.
* @param bigCollection
* the collection with added elements.
* @return the the array of element indices which where added to the original
* collection
*/
public static int[] computeDifferenceIndices(
Collection<?> smallCollection, Collection<?> bigCollection) {
List<Integer> addedIndices = new ArrayList<>();
int index = 0;
for (Iterator<?> ite = bigCollection.iterator(); ite.hasNext(); index++) {
if (smallCollection == null
|| !smallCollection.contains(ite.next())) {
if (smallCollection == null) {
ite.next();
}
addedIndices.add(index);
}
}
int[] result = new int[addedIndices.size()];
for (int i = 0; i < addedIndices.size(); i++) {
result[i] = addedIndices.get(i);
}
return result;
}
}
Console:[]