Here you can find the source of unionList(List
Parameter | Description |
---|---|
T | the generic type |
list1 | the list1 |
list2 | the list2 |
public static <T> List<T> unionList(List<T> list1, List<T> list2)
//package com.java2s; /*//from w ww . j a va2 s .c om 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 { /** * Union list. * * @param <T> the generic type * @param list1 the list1 * @param list2 the list2 * @return the list */ public static <T> List<T> unionList(List<T> list1, List<T> list2) { if (isEmpty(list1) && isEmpty(list2)) { return new ArrayList<T>(); } // list1 empty & list2 has values if (isEmpty(list1) && !isEmpty(list2)) { return list2; } // list1 has values & list2 is empty if (!isEmpty(list1) && isEmpty(list2)) { return list1; } if (list1.equals(list2)) { return list1; } List<T> list3 = new ArrayList<T>(); for (T object : list1) { list2.add(object); } for (T object : list2) { if (!list3.contains(object)) { list3.add(object); } } return list3; } /** * 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; } }