Java List Create createIndicesOrderedList(final Collection order, final Collection values)

Here you can find the source of createIndicesOrderedList(final Collection order, final Collection values)

Description

Creates a new list with the order of entries equal to the order of entries in the collection 'order', regarding the indices of the collection 'values', and the values copied from the collection 'values'.

License

Open Source License

Declaration

public static <V> List<V> createIndicesOrderedList(final Collection<Integer> order,
        final Collection<V> values) 

Method Source Code

//package com.java2s;
/*// w ww .  j  a v a  2  s  . c  o m
 * Copyright (C) 2013 Universitat Pompeu Fabra
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;

import java.util.Collection;

import java.util.List;

public class Main {
    /**
     * Creates a new list with the order of entries equal to the order
     * of entries in the collection 'order',
     * regarding the indices of the collection 'values',
     * and the values copied from the collection 'values'.
     */
    public static <V> List<V> createIndicesOrderedList(final Collection<Integer> order,
            final Collection<V> values) {

        List<V> result = new ArrayList<V>(order.size());

        if (values instanceof List) {
            List<V> valuesList = (List<V>) values;
            for (Integer orderIndex : order) {
                result.add(valuesList.get(orderIndex));
            }
        } else {
            int curIndex = 0;
            for (V value : values) {
                if (order.contains(curIndex)) {
                    result.add(value);
                }
                curIndex++;
            }
        }

        return result;
    }
}

Related

  1. createEntireInStatement(List values)
  2. createErrorStringFromList(List errorList)
  3. createGrantScript(List objectsToGrant, String grantee)
  4. createHTMLList(String list, String link, boolean APPEND, String title)
  5. createIfNull(List list)
  6. createInString(List keys)
  7. createIntArray(List coll)
  8. CreateIntArrayFromIntegerList(List solidsList)
  9. createIntegerList(int[] array)