Here you can find the source of getRandomSubList(List
Parameter | Description |
---|---|
T | a parameter |
inputList | a parameter |
percentage | percentage of elements that should be randomly extracted from inputFiles |
public static <T> List<T> getRandomSubList(List<T> inputList, double percentage)
//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); } }