Java List Combine combineSubtractArrays(List a1, List a2)

Here you can find the source of combineSubtractArrays(List a1, List a2)

Description

Changes first array to have same elements as second Given arrays a1 and a2 Returns array a1 with elements present in a2, but without ones that aren't in a2

License

Apache License

Parameter

Parameter Description
a1 first array - origin
a2 second array - destination

Return

origin array changed to destination

Declaration

public static <T> List<T> combineSubtractArrays(List<T> a1, List<T> a2) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.Iterator;
import java.util.List;

public class Main {
    /**//from  w w  w  .  j  a v  a 2 s  . c o m
     * Changes first array to have same elements as second
     * Given arrays a1 and a2
     * Returns array a1 with elements present in a2, but without ones that aren't
     * in a2
     * 
     * @param a1
     *           first array - origin
     * @param a2
     *           second array - destination
     * @return origin array changed to destination
     */
    public static <T> List<T> combineSubtractArrays(List<T> a1, List<T> a2) {
        Iterator<T> it = a1.iterator();
        while (it.hasNext()) {
            T e1 = it.next();
            if (!a2.contains(e1)) {
                a1.remove(e1);
            }
        }
        if (a1.size() == a2.size()) {
            return a1;
        }
        it = a2.iterator();
        while (it.hasNext()) {
            T e2 = it.next();
            if (!a1.contains(e2)) {
                a1.add(e2);
            }
        }
        return a1;
    }
}

Related

  1. combineList(List l1, List l2)
  2. combineLists(List keys, List values)
  3. combineLists(List a, List b)
  4. combineString(List lstInput, String strToken)
  5. combineStringList(List sList)
  6. getCombinedColumnName(final List columnNames, final boolean sortByName)
  7. putCombinedList(Map map, Object key, Object value)
  8. recursiveCombine(List>> toCombinate, int index)