Java List Merge mergedView(final List left, final List right)

Here you can find the source of mergedView(final List left, final List right)

Description

Returns a new unmodifiable view that is the merging of two lists

License

Open Source License

Parameter

Parameter Description
T the type the lists hold
left the left portion of the merged view
right the right portion of the merged view

Return

a list view that contains bot the left and right lists

Declaration

public static <T> List<T> mergedView(final List<T> left, final List<T> right) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.AbstractList;

import java.util.List;

public class Main {
    /**/*from   w w w.  ja  v  a2s .  co  m*/
     * Returns a new unmodifiable view that is the merging of two lists
     * @param <T> the type the lists hold
     * @param left the left portion of the merged view
     * @param right the right portion of the merged view
     * @return a list view that contains bot the left and right lists
     */
    public static <T> List<T> mergedView(final List<T> left, final List<T> right) {
        List<T> merged = new AbstractList<T>() {

            @Override
            public T get(int index) {
                if (index < left.size())
                    return left.get(index);
                else if (index - left.size() < right.size())
                    return right.get(index - left.size());
                else
                    throw new IndexOutOfBoundsException("List of lengt " + size() + " has no index " + index);
            }

            @Override
            public int size() {
                return left.size() + right.size();
            }
        };
        return merged;
    }
}

Related

  1. mergeBandY(List A, int z, int y, int yn)
  2. mergeBytes(List packages)
  3. mergeCollectionsToList(Collection... cols)
  4. mergeCoords(List x, List y)
  5. mergeData(List lstMain, List lstData, List lstKey)
  6. mergeErrorMsgList(List errorMsgs)
  7. mergeException(List t)
  8. mergeExcludedFieldLists(String[] superClassExcludes, String[] localClassExcludes)
  9. mergeFilterLists(List childFilters, List parentFilters)