Java List Subtract subtractFloatLists(List listA, List listB)

Here you can find the source of subtractFloatLists(List listA, List listB)

Description

Subtract float lists.

License

Apache License

Parameter

Parameter Description
listA the list a
listB the list b

Return

the list

Declaration

public static List<Float> subtractFloatLists(List<Float> listA, List<Float> listB) 

Method Source Code


//package com.java2s;
/*//www .  java  2s . c  o m
Copyright 2014 Array-Utilities
    
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.Collection;

import java.util.List;

public class Main {
    /**
     * Subtract float lists.
     *
     * @param listA
     *            the list a
     * @param listB
     *            the list b
     * @return the list
     */
    public static List<Float> subtractFloatLists(List<Float> listA, List<Float> listB) {

        // ListA empty & ListB has values
        if (isEmpty(listA) && !isEmpty(listB)) {
            return listB;
        }

        // ListA has values & ListB is empty
        if (!isEmpty(listA) && isEmpty(listB)) {
            return listA;
        }

        // Both List are empty
        if (isEmpty(listA) && isEmpty(listB)) {
            return new ArrayList<Float>();
        }

        // [1,2,3,4,5]
        // [3,4,5,6,7,8,9]
        ArrayList<Float> list = new ArrayList<Float>();
        int i = 0;
        int j = 0;

        while (i < listA.size() && j < listB.size()) {
            list.add(listA.get(i) - listB.get(j));
            i++;
            j++;

        }
        while (i < listA.size()) {
            list.add(listA.get(i));
            i++;
        }
        while (j < listB.size()) {
            list.add(listB.get(j));
            j++;
        }
        System.out.println("list is " + list);
        return list;
    }

    /**
     * Checks if is empty.
     *
     * @param <E>
     *            the element type
     * @param collection
     *            the collection
     * @return true, if is empty
     */
    public static <E> boolean isEmpty(Collection<? super E> collection) {

        if ((collection.size() == 0) || (collection == null))
            return true;
        return false;
    }
}

Related

  1. subtract(List vector, int[] sparseVector)
  2. subtract(List a, List b)
  3. subtract(List aList, List bList)
  4. subtract(String[] list1, String[] list2)
  5. subtractAsList(T[] array1, T[] array2)
  6. subtractFromList(List op1, Object op2)
  7. subtractQ(LinkedList Q, List P)