host.anzo.commons.utils.StreamUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-core Show documentation
Show all versions of commons-core Show documentation
Commons library to make me happy.
package host.anzo.commons.utils;
import org.jetbrains.annotations.NotNull;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* @author ANZO
*/
public class StreamUtils {
public static List closedRange(int startInclusive, int endInclusive) {
return IntStream.rangeClosed(startInclusive, endInclusive)
.boxed()
.collect(Collectors.toList());
}
public static > Map sortMapByValue(@NotNull Map map, boolean isReverseOrder) {
return map.entrySet().stream()
.sorted(isReverseOrder ? Map.Entry.comparingByValue(Comparator.reverseOrder()) : Map.Entry.comparingByValue(Comparator.naturalOrder()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
}
}