jsat.utils
Class ListUtils
- java.lang.Object
-
- jsat.utils.ListUtils
-
public class ListUtils extends java.lang.Object
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method and Description static voidaddRange(java.util.Collection<java.lang.Integer> c, int start, int to, int step)Adds values into the given collection using integer in the specified range and step size.static <T> java.util.List<T>collectFutures(java.util.Collection<java.util.concurrent.Future<T>> futures)Collects all future values in a collection into a list, and returns said list.static <T> java.util.List<T>mergedView(java.util.List<T> left, java.util.List<T> right)Returns a new unmodifiable view that is the merging of two listsstatic <T> voidrandomSample(java.util.Collection<T> source, java.util.Collection<T> dest, int samples, java.util.Random rand)Obtains a random sample without replacement from a source collection and places it in the destination collection.static <T> voidrandomSample(java.util.List<T> source, java.util.List<T> dest, int samples)Obtains a random sample without replacement from a source list and places it in the destination list.static <T> voidrandomSample(java.util.List<T> source, java.util.List<T> dest, int samples, java.util.Random rand)Obtains a random sample without replacement from a source list and places it in the destination list.static IntListrange(int start, int to)Returns a list of integers with values in the given rangestatic IntListrange(int start, int to, int step)Returns a list of integers with values in the given rangestatic <T> java.util.List<java.util.List<T>>splitList(java.util.List<T> source, int count)This method takes a list and breaks it into count lists backed by the original list, with elements being equally spaced among the lists.static voidswap(java.util.List list, int i, int j)Swaps the values in the list at the given positions
-
-
-
Method Detail
-
splitList
public static <T> java.util.List<java.util.List<T>> splitList(java.util.List<T> source, int count)This method takes a list and breaks it into count lists backed by the original list, with elements being equally spaced among the lists. The lists will be returned in order of the consecutive values they represent in the source list.
NOTE: Because the implementation usesList.subList(int, int), changes to the returned lists will be reflected in the source list.- Type Parameters:
T- the type contained in the list- Parameters:
source- the source list that will be used to back the count listscount- the number of lists to partition the source into.- Returns:
- a lists of lists, each of with the same size with at most a difference of 1.
-
mergedView
public static <T> java.util.List<T> mergedView(java.util.List<T> left, java.util.List<T> right)Returns a new unmodifiable view that is the merging of two lists- Type Parameters:
T- the type the lists hold- Parameters:
left- the left portion of the merged viewright- the right portion of the merged view- Returns:
- a list view that contains bot the left and right lists
-
swap
public static void swap(java.util.List list, int i, int j)Swaps the values in the list at the given positions- Parameters:
list- the list to perform the swap ini- the first position to swapj- the second position to swap
-
collectFutures
public static <T> java.util.List<T> collectFutures(java.util.Collection<java.util.concurrent.Future<T>> futures) throws java.util.concurrent.ExecutionException, java.lang.InterruptedExceptionCollects all future values in a collection into a list, and returns said list. This method will block until all future objects are collected.- Type Parameters:
T- the type of future object- Parameters:
futures- the collection of future objects- Returns:
- a list containing the object from the future.
- Throws:
java.util.concurrent.ExecutionExceptionjava.lang.InterruptedException
-
addRange
public static void addRange(java.util.Collection<java.lang.Integer> c, int start, int to, int step)Adds values into the given collection using integer in the specified range and step size. If the start value is greater or equal to the to value, nothing will be added to the collection.- Parameters:
c- the collection to add tostart- the first value to add, inclusiveto- the last value to add, exclusivestep- the step size.- Throws:
java.lang.RuntimeException- if the step size is zero or negative.
-
range
public static IntList range(int start, int to)
Returns a list of integers with values in the given range- Parameters:
start- the starting integer value (inclusive)to- the ending integer value (exclusive)- Returns:
- a list of integers containing the specified range of integers
-
range
public static IntList range(int start, int to, int step)
Returns a list of integers with values in the given range- Parameters:
start- the starting integer value (inclusive)to- the ending integer value (exclusive)step- the step size between values- Returns:
- a list of integers containing the specified range of integers
-
randomSample
public static <T> void randomSample(java.util.List<T> source, java.util.List<T> dest, int samples, java.util.Random rand)Obtains a random sample without replacement from a source list and places it in the destination list. This is done without modifying the source list.- Type Parameters:
T- the list content type involved- Parameters:
source- the source of values to randomly sample fromdest- the list to store the random samples in. The list does not need to be empty for the sampling to work correctlysamples- the number of samples to select from the sourcerand- the source of randomness for the sampling- Throws:
java.lang.IllegalArgumentException- if the sample size is not positive or l arger than the source population.
-
randomSample
public static <T> void randomSample(java.util.Collection<T> source, java.util.Collection<T> dest, int samples, java.util.Random rand)Obtains a random sample without replacement from a source collection and places it in the destination collection. This is done without modifying the source collection.
This random sampling is oblivious to failures to add to a collection that may occur, such as if the collection is aSet- Type Parameters:
T- the list content type involved- Parameters:
source- the source of values to randomly sample fromdest- the collection to store the random samples in. It does not need to be empty for the sampling to work correctlysamples- the number of samples to select from the sourcerand- the source of randomness for the sampling- Throws:
java.lang.IllegalArgumentException- if the sample size is not positive or l arger than the source population.
-
randomSample
public static <T> void randomSample(java.util.List<T> source, java.util.List<T> dest, int samples)Obtains a random sample without replacement from a source list and places it in the destination list. This is done without modifying the source list.- Type Parameters:
T- the list content type involved- Parameters:
source- the source of values to randomly sample fromdest- the list to store the random samples in. The list does not need to be empty for the sampling to work correctlysamples- the number of samples to select from the source- Throws:
java.lang.IllegalArgumentException- if the sample size is not positive or l arger than the source population.
-
-
DataMelt 3.0 © DataMelt by jWork.ORG