
solid.collectors.ToSparseArray Maven / Gradle / Ivy
package solid.collectors;
import android.util.SparseArray;
import solid.functions.Func1;
public class ToSparseArray {
/**
* Returns a method that can be used with {@link solid.stream.Stream#collect(Func1)}
* to convert a stream into a {@link SparseArray}.
*
* @param a type of stream items.
* @param itemToKey a method that should return a key for an item.
* @return a method that converts an iterable into a {@link SparseArray}.
*/
public static Func1, SparseArray> toSparseArray(Func1 itemToKey) {
return toSparseArray(itemToKey, 10);
}
/**
* Returns a method that can be used with {@link solid.stream.Stream#collect(Func1)}
* to convert a stream into a {@link SparseArray}.
*
* Use this method instead of {@link #toSparseArray(Func1)}} for better performance on
* streams that can have more than 10 items.
*
* @param a type of stream items.
* @param itemToKey a method that should return a key for an item.
* @param initialCapacity initial capacity on the sparse array.
* @return a method that converts an iterable into a {@link SparseArray}.
*/
public static Func1, SparseArray> toSparseArray(final Func1 itemToKey, final int initialCapacity) {
return new Func1, SparseArray>() {
@Override
public SparseArray call(Iterable iterable) {
SparseArray array = new SparseArray<>(initialCapacity);
for (T value : iterable)
array.put(itemToKey.call(value), value);
return array;
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy