Java Collection Difference computeDifferenceIndices(Collection smallCollection, Collection bigCollection)

Here you can find the source of computeDifferenceIndices(Collection smallCollection, Collection bigCollection)

Description

Computes the array of element indices which where added to a collection.

License

Open Source License

Parameter

Parameter Description
smallCollection the original collection.
bigCollection the collection with added elements.

Return

the the array of element indices which where added to the original collection

Declaration

public static int[] computeDifferenceIndices(Collection<?> smallCollection, Collection<?> bigCollection) 

Method Source Code


//package com.java2s;
/*//  w w  w . j a  va  2  s. co  m
 * Copyright (c) 2005-2016 Vincent Vandenschrick. All rights reserved.
 *
 *  This file is part of the Jspresso framework.
 *
 *  Jspresso is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Jspresso is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Jspresso.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class Main {
    /**
     * 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;
    }
}

Related

  1. diff(Collection c1, Collection c2)
  2. diffColls(Collection oldColl, Collection newColl)
  3. difference(Collection set1, Collection set2)
  4. difference(Collection collection1, Collection result, Collection collection2)