Java List Merge merge(List lst1, List lst2)

Here you can find the source of merge(List lst1, List lst2)

Description

Joins two lists in such a way that common end of the first list and common start with the second list are merged.

License

Apache License

Parameter

Parameter Description
lst1 first list
lst2 second list

Return

merged list

Declaration

public static List<String> merge(List<String> lst1, List<String> lst2) 

Method Source Code


//package com.java2s;
/*/*from   w  w w. ja v  a2  s.c  om*/
 * Copyright 2016 Esri, Inc..
 *
 * 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 {
    /**
     * Joins two lists in such a way that common end of the first list and common 
     * start with the second list are merged.
     * @param lst1 first list
     * @param lst2 second list
     * @return merged list
     */
    public static List<String> merge(List<String> lst1, List<String> lst2) {
        int rootStart = lst1.size() > lst2.size() ? lst1.size() - lst2.size() : 0;
        int suffixEnd = lst2.size() > lst1.size() ? lst1.size() : lst2.size();
        boolean ends = false;

        while (rootStart < lst1.size() && suffixEnd >= 0) {
            ends = endsWith(lst1, head(lst2, suffixEnd));
            if (ends)
                break;
            rootStart++;
            suffixEnd--;
        }

        ArrayList<String> acc = new ArrayList<String>(lst1);
        if (ends) {
            acc.addAll(tail(lst2, suffixEnd));
        } else {
            acc.addAll(lst2);
        }

        return acc;
    }

    /**
     * Checks if list ends with the specific suffix
     * @param lst source list 
     * @param suffix suffix
     * @return <code>true</code> if list ends with suffix
     */
    private static boolean endsWith(List<String> lst, List<String> suffix) {
        int idx = lst.size() - suffix.size();

        if (idx < 0) {
            return false;
        }

        for (int i = 0; i < suffix.size(); i++) {
            if (!lst.get(idx + i).equals(suffix.get(i))) {
                return false;
            }
        }

        return true;
    }

    /**
     * Gets head of the list.
     * @param lst source list
     * @param to end index
     * @return head 
     */
    public static List<String> head(List<String> lst, int to) {
        return lst.subList(0, Math.max(0, Math.min(to, lst.size())));
    }

    /**
     * Gets tail of the list
     * @param lst source list
     * @param from start index
     * @return 
     */
    public static List<String> tail(List<String> lst, int from) {
        return lst.subList(Math.max(0, Math.min(from, lst.size())), lst.size());
    }
}

Related

  1. merge(List list1, List list2)
  2. merge(List> list)
  3. merge(List l1, List l2)
  4. merge(List l1, List l2)
  5. merge(List list)
  6. merge(List stringList)
  7. merge(List text)
  8. merge(List a, List b)
  9. merge(List a, List b, Comparator comp)