in.ashwanthkumar.utils.collections.Lists Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of my-java-utils Show documentation
Show all versions of my-java-utils Show documentation
My personal set of utils that I take along with my java projects.
package in.ashwanthkumar.utils.collections;
import in.ashwanthkumar.utils.func.Function;
import in.ashwanthkumar.utils.func.Predicate;
import in.ashwanthkumar.utils.lang.option.None;
import in.ashwanthkumar.utils.lang.option.Option;
import in.ashwanthkumar.utils.lang.tuple.Tuple2;
import java.util.*;
import static in.ashwanthkumar.utils.lang.option.Option.option;
import static in.ashwanthkumar.utils.lang.tuple.Tuple2.tuple2;
public class Lists {
public static List Nil() {
return new ArrayList();
}
public static List of(T... elements) {
ArrayList list = new ArrayList();
Collections.addAll(list, elements);
return list;
}
public static List map(Iterable collection, Function transformation) {
ArrayList transformed = new ArrayList();
for (T item : collection) {
transformed.add(transformation.apply(item));
}
return transformed;
}
public static List map(T[] array, Function transformation) {
return map(of(array), transformation);
}
public static List filter(Iterable collection, Predicate condition) {
ArrayList filteredList = new ArrayList();
for (T item : collection) {
if (condition.apply(item)) filteredList.add(item);
}
return filteredList;
}
public static Z foldL(Iterable collection, Z initialValue, Function, Z> foldFunction) {
Z foldedValue = initialValue;
for (T item : collection) {
foldedValue = foldFunction.apply(tuple2(foldedValue, item));
}
return foldedValue;
}
public static Option find(Iterable collection, Predicate condition) {
for (T item : collection) {
if (condition.apply(item)) return option(item);
}
return new None();
}
public static > List flatten(Collection elem) {
ArrayList flattened = new ArrayList();
for (Collection data : elem) {
flattened.addAll(data);
}
return flattened;
}
public static List concat(Collection... elems) {
ArrayList concated = new ArrayList();
for (Collection elem : elems) {
concated.addAll(elem);
}
return concated;
}
public static String mkString(Iterable collection) {
return mkString(collection, "", "", ",");
}
public static String mkString(Iterable collection, String separator) {
return mkString(collection, "", "", separator);
}
public static String mkString(Iterable collection, String start, String end, String separator) {
StringBuilder builder = new StringBuilder();
builder.append(start);
for (T elem : collection) {
builder.append(String.valueOf(elem));
builder.append(separator);
builder.append(" ");
}
builder.delete(builder.length() - (separator.length() + 1), builder.length());
builder.append(end);
return builder.toString();
}
public static List take(Iterable input, int size) {
List output = new ArrayList();
int count = 0;
Iterator iterator = input.iterator();
while(iterator.hasNext() && count < size) {
output.add(iterator.next());
count++;
}
return output;
}
/**
* Takes elements from the list as long as the predicate is met and stops after that.
*/
public static List takeWhile(Iterable input, Predicate predicate) {
List output = new ArrayList();
for (T elem : input) {
if (predicate.apply(elem)) {
output.add(elem);
} else {
break;
}
}
return output;
}
}