Java List Merge mergeListNoDuplicate(List one, List two)

Here you can find the source of mergeListNoDuplicate(List one, List two)

Description

merge List No Duplicate

License

Apache License

Declaration

public static <T> List<T> mergeListNoDuplicate(List<T> one, List<T> two) 

Method Source Code

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

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static <T> List<T> mergeListNoDuplicate(List<T> one, List<T> two) {

        if (isAllNull(one, two))
            return null;

        List<T> result = new ArrayList<>();

        if (isAllNotNull(one, two)) {
            result.addAll(one);//from www. j a  va 2 s.co m
            for (T t : two) {
                if (one.indexOf(t) == -1) {
                    result.add(t);
                }
            }
        } else {
            result.addAll(findFirstNotNull(one, two));
        }

        return result;
    }

    public static <T> boolean isAllNull(T one, T two) {
        return one == null ? two == null : false;
    }

    public static <T> boolean isAllNotNull(T one, T two) {
        return one != null && two != null;
    }

    public static <T> int indexOf(int start, List<T> datas, T target) {
        int index = -1;

        for (int i = start; i < datas.size(); i++) {
            if (datas.get(i).equals(target)) {
                index = i;
                break;
            }
        }
        return index;
    }

    public static <T> T findFirstNotNull(T... types) {
        for (T type : types) {
            if (type != null)
                return type;
        }
        return null;
    }
}

Related

  1. mergeList(List list1, List list2)
  2. mergeList(List list)
  3. mergeList(List list)
  4. mergeList(List list1, List list2)
  5. mergeList(List list, List... others)
  6. mergeLists(final T... array)
  7. mergeLists(List baseList, List newItems)
  8. mergeLists(List copyTo, List copyFrom)
  9. mergeLists(List dest, List inserts)

  10. HOME | Copyright © www.java2s.com 2016