Java List Difference difference(final List minuend, final List subtrahend)

Here you can find the source of difference(final List minuend, final List subtrahend)

Description

difference

License

Apache License

Declaration

public static List<Double> difference(final List<? extends Object> minuend,
            final List<? extends Object> subtrahend) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2015/*from   ww  w.j a v a2 s .c om*/
 * Ubiquitous Knowledge Processing (UKP) Lab
 * Technische Universit?t Darmstadt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static List<Double> difference(final List<? extends Object> minuend,
            final List<? extends Object> subtrahend) {
        final List<Double> result = new ArrayList<Double>();

        for (int i = 0; i < minuend.size(); ++i) {
            final Object o1 = minuend.get(i);
            final Object o2 = subtrahend.get(i);
            if (areBothNumbers(o1, o2)) {
                subtract(result, o1, o2);
            } else {
                result.add(Double.NaN);
            }
        }

        return result;
    }

    private static boolean areBothNumbers(final Object o1, final Object o2) {
        return o1 instanceof Number && o2 instanceof Number;
    }

    private static void subtract(final List<Double> result, final Object o1, final Object o2) {
        final Number val1 = (Number) o1;
        final Number val2 = (Number) o2;

        result.add(val1.doubleValue() - val2.doubleValue());
    }
}

Related

  1. diff(List ls, List ls2)
  2. diff(List doublesA, List doublesB)
  3. diffAsSet(Collection list1, Collection list2)
  4. difference(final List list1, final List list2)
  5. difference(final String[] list1, final String[] list2)
  6. difference(List firstList, List secondList)
  7. difference(List lst1, List lst2)