Java List Sub List getRandomSubList(List inputList, double percentage)

Here you can find the source of getRandomSubList(List inputList, double percentage)

Description

get Random Sub List

License

Open Source License

Parameter

Parameter Description
T a parameter
inputList a parameter
percentage percentage of elements that should be randomly extracted from inputFiles

Declaration

public static <T> List<T> getRandomSubList(List<T> inputList, double percentage) 

Method Source Code

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

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
    /**/*  w  ww .j a va 2s. com*/
     *
     * @param <T>
     * @param inputList
     * @param percentage
     *            percentage of elements that should be randomly extracted from
     *            inputFiles
     * @return
     */
    public static <T> List<T> getRandomSubList(List<T> inputList, double percentage) {
        if ((percentage < 0) || (percentage > 1)) {
            throw new IllegalArgumentException("percentage has to be between 0 and 1");
        }

        int numberOfElements = (int) (inputList.size() * percentage);
        return getRandomSubList(inputList, numberOfElements);
    }

    /**
     *
     * @param <T>
     * @param inputList
     * @param numberOfFiles
     *            number of files that should be randomly extracted from
     *            inputFiles
     * @return
     */
    public static <T> List<T> getRandomSubList(List<T> inputList, int numberOfFiles) {
        if ((numberOfFiles < 0) || (numberOfFiles > inputList.size())) {
            throw new IllegalArgumentException("numberOfFiles has to be between 0 and size of inputList");
        }

        List<T> shuffeledList = new ArrayList<T>(inputList);
        Collections.shuffle(shuffeledList);
        return shuffeledList.subList(0, numberOfFiles);
    }
}

Related

  1. copySubList(final List list, final int fromIndex, final int toIndex)
  2. copySubList(List list, int fromIndex, int toIndex)
  3. divideListInSublistsOfNSize(List list, int n)
  4. getIndexesId(List aList, List aSubList)
  5. getIndicesOfItems(List superList, List sublist)
  6. getSubList(int offset, int count, List originalList)
  7. getSubList(List entireList, Class cls)
  8. getSubList(List list, Class type)
  9. getSubList(List source, int nFrom, int nTo)