Java List Merge mergeLists(List baseList, List newItems)

Here you can find the source of mergeLists(List baseList, List newItems)

Description

merges the entries from newItems into the baseList
this implementation does adopt items from the baseList to the newItems in the same order
- without creating a new list
- without touching existing entries (which should not be removed)

This implementation only works if .equals() of the type T is implemented in a proper way

License

Apache License

Parameter

Parameter Description
baseList the base list which will be altered
newItems the new entries which should be merged into the the existing list

Declaration

public static <T> void mergeLists(List<T> baseList, List<T> newItems) 

Method Source Code

//package com.java2s;
/*// w ww . ja v a2s .  c  o  m
 * Copyright 2012 - 2016 Manuel Laggner
 *
 * 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.List;

public class Main {
    /**
     * merges the entries from newItems into the baseList<br />
     * this implementation does adopt items from the baseList to the newItems in the same order<br />
     * - without creating a new list<br />
     * - without touching existing entries (which should not be removed)<br />
     * <br />
     * This implementation only works if .equals() of the type T is implemented in a proper way
     * 
     * @param baseList
     *          the base list which will be altered
     * @param newItems
     *          the new entries which should be merged into the the existing list
     */
    public static <T> void mergeLists(List<T> baseList, List<T> newItems) {
        // first remove old ones
        for (int i = baseList.size() - 1; i >= 0; i--) {
            T entry = baseList.get(i);
            if (!newItems.contains(entry)) {
                baseList.remove(entry);
            }
        }

        // second, add new ones in the right order
        for (int i = 0; i < newItems.size(); i++) {
            T entry = newItems.get(i);
            if (!baseList.contains(entry)) {
                try {
                    baseList.add(i, entry);
                } catch (IndexOutOfBoundsException e) {
                    baseList.add(entry);
                }
            } else {
                int indexOldList = baseList.indexOf(entry);
                if (i != indexOldList) {
                    T oldEntry = baseList.remove(indexOldList);
                    try {
                        baseList.add(i, oldEntry);
                    } catch (IndexOutOfBoundsException e) {
                        baseList.add(oldEntry);
                    }
                }
            }
        }
    }
}

Related

  1. mergeList(List list)
  2. mergeList(List list1, List list2)
  3. mergeList(List list, List... others)
  4. mergeListNoDuplicate(List one, List two)
  5. mergeLists(final T... array)
  6. mergeLists(List copyTo, List copyFrom)
  7. mergeLists(List dest, List inserts)
  8. mergeLists(List listA, List listB, List listC)
  9. mergeLists(Object oldValue, Object newValue, Object newValue2)

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