net.dongliu.commons.lang.collection.Helper Maven / Gradle / Ivy
package net.dongliu.commons.lang.collection;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* utils for all arrays/collections, for more convenient using.
* You should use static import if you want to use this instead of Arrays/Maps/Lists/Pair.
*
* {@code
* import static net.dongliu.commons.lang.collection.Helper.*;
* }
*
*
* @author Dong Liu
*/
public class Helper {
/**
* get pair.
* Usage:
*
* {@code Pair pair = pair("test", 1);}
*
*/
public static Pair pair(K key, V value) {
return Pair.of(key, value);
}
/**
* get array with type T.
* Usage:
*
* {@code String[] strings = array("test", "test1", "test2");}
*
*/
@SafeVarargs
public static T[] array(T... values) {
return Array.of(values);
}
/**
* get list with inner type T.
* Usage:
*
* {@code List list = list("test", "test1", "test2");}
*
*/
@SafeVarargs
public static List list(T... values) {
return Lists.of(values);
}
/**
* get set with inner type T.
* Usage:
*
* {@code Set set = set("test", "test1", "test2");}
*
*/
@SafeVarargs
public static Set set(T... values) {
return Sets.of(values);
}
/**
* get map. Usage:
*
* {@code Map map = map(
* pair("test", 1),
* pair("test", 2),
* pair("test", 3)
* );
* }
*
*/
@SafeVarargs
public static Map map(Pair... pairs) {
return Maps.of(pairs);
}
/**
* zip list to map. Usage:
*
* {@code Map map = zip(
* Utils.list("test", "test1", "test2"),
* list(1, 2, 3)
* );
* }
*
*/
public static Map zip(Collection keys, Collection values) {
return Maps.zip(keys, values);
}
/**
* zip arrays to map.Usage:
*
* {@code Map map = map = zip(
* array("test", "test1", "test2"),
* array(1, 2, 3)
* }
* );
*
*/
public static Map zip(K[] keys, V[] values) {
return Maps.zip(keys, values);
}
/**
* get immutable list
*/
public static List immutable(List list) {
return Lists.immutable(list);
}
/**
* get immutable map
*/
public static Map immutable(Map map) {
return Maps.immutable(map);
}
/**
* filter list, get a new list.
*
* {@code List list = list("test", "test1", "test2");}
* {@code List newList = filter(list, e -> e.endsWith("1"));}
*
*/
public static List filter(List list, Predicate super T> filter) {
return list.stream().filter(filter).collect(Collectors.toList());
}
/**
* filter set, get a new set.
*
* {@code Set set = set("test", "test1", "test2");}
* {@code Set newSet = filter(set, e -> e.endsWith("1"));}
*
*/
public static Set filter(Set set, Predicate super T> filter) {
return set.stream().filter(filter).collect(Collectors.toSet());
}
/**
* filter collection, get a new collection.
*/
public static Collection filter(Collection values, Predicate super T> filter) {
return values.stream().filter(filter).collect(Collectors.toCollection(ArrayList::new));
}
/**
* filter map.
*
* {@code Map map = map(pair("test", 1), pair("test2", 2));}
* {@code Map newMap = filter(map, e -> e.getKey().endsWith("2"));}
*
*/
public static Map filter(Map map,
Predicate super Pair> filter) {
return map.entrySet().stream().map(e -> pair(e.getKey(), e.getValue()))
.filter(filter)
.collect(Collectors.toMap(Pair::getKey, Pair::getValue));
}
/**
* map list with function.
*
* {@code List list = list("test", "test1", "test2");}
* {@code List mlist = map(list, String::length);}
*
*/
public static List map(List list, Function super T, ? extends D> mapper) {
return list.stream().map(mapper).collect(Collectors.toList());
}
/**
* map set with function.
*
* {@code List list = list("test", "test1", "test2");}
* {@code List mlist = map(list, String::length);}
*
*/
public static Set map(Set set, Function super T, ? extends D> mapper) {
return set.stream().map(mapper).collect(Collectors.toSet());
}
/**
* map collection
*/
public static Collection map(Collection values,
Function super T, ? extends D> mapper) {
return values.stream().map(mapper).collect(Collectors.toCollection(ArrayList::new));
}
/**
* map maps....
*
* {@code Map map = map(pair("test", 1), pair("test2", 2));}
* {@code Map newMap = map(map, e -> pair(e.getKey().length(), e.getValue().toString()));}
*
*/
public static Map map(Map map,
Function, Pair> mapper) {
return map.entrySet().stream()
.map(e -> mapper.apply(pair(e.getKey(), e.getValue())))
.collect(Collectors.toMap(Pair::getKey, Pair::getValue));
}
/**
* do reduce.
*
* {@code List list = list("test", "test1", "test2");}
* {@code int total = reduce(list, (x, y) -> x + y.length(), 0);}
*
*/
public static D reduce(Collection values, BiFunction reducer, D initValue) {
D result = initValue;
for (T value : values) {
result = reducer.apply(result, value);
}
return result;
}
/**
* do reduce.
*
* {@code Map map = map(pair("test", 1), pair("test2", 2));}
* {@code int total = reduce(map, (x, y) -> x + (y.getKey().endsWith("2") ? y.getValue() : 0), 0);}
*
*/
public static D reduce(Map map, BiFunction, D> reducer,
D initValue) {
D result = initValue;
for (Map.Entry e : map.entrySet()) {
result = reducer.apply(result, pair(e.getKey(), e.getValue()));
}
return result;
}
}