Trebor006
4/5/2017 - 2:05 PM

Ejemplo de Java 8

Ejemplo de Java 8

package com.bo.mibu.java;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.ToIntFunction;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class CollectionUtil {

    /**
     * @param collection
     * @param predicate
     * @param <T>
     * @return True if there is any value with the condition
     */
    public static <T> boolean anyMatch(Collection<T> collection, Predicate<? super T> predicate) {
        return collection.stream().anyMatch(predicate);
    }

    public static <T> boolean allMatch(Collection<T> collection, Predicate<? super T> predicate) {
        return !collection.isEmpty() && collection.stream().allMatch(predicate);
    }

    public static <T, R> Stream<R> map(Collection<T> collection, Function<? super T, ? extends R> mapper) {
        return collection.stream().map(mapper);
    }

    public static <T, R> IntStream mapToInt(Collection<T> collection, ToIntFunction<? super T> mapper) {
        return collection.stream().mapToInt(mapper);
    }

    public static <T, R> List<R> toList(Collection<T> collection, Function<? super T, ? extends R> mapper) {
        return toList(map(collection, mapper));
    }

    public static <T, R> List<R> filterAndMap(Collection<T> collection,
                                              Predicate<? super T> predicate,
                                              Function<? super T, ? extends R> mapper) {
        return toList(filterCollection(collection, predicate).map(mapper));
    }

    public static <T, K> Map<K, List<T>> toMap(Collection<T> collection, Function<? super T, ? extends K> classifier) {
        return collection.stream().collect(Collectors.groupingBy(classifier));
    }

    public static <T> Integer sum(Collection<T> collection, ToIntFunction<? super T> mapper) {
        return mapToInt(collection, mapper).sum();
    }

    public static <T> BigDecimal reduceSum(Collection<T> collection, Function<? super T, ? extends BigDecimal> mapper) {
        return map(collection, mapper).reduce(BigDecimal.ZERO, BigDecimal::add);
    }

    public static <T> Long reduceLongSum(Collection<T> collection, Function<? super T, ? extends Long> mapper) {
        return map(collection, mapper).reduce(0L, Long::sum);
    }

    public static <T> Optional<T> first(Collection<T> collection) {
        return collection.stream().findFirst();
    }

    public static <T> Optional<T> first(Collection<T> collection, Predicate<? super T> predicate) {
        return filterCollection(collection, predicate).findFirst();
    }

    public static <T> List<T> filter(Collection<T> collection, Predicate<? super T> predicate) {
        return toList(filterCollection(collection, predicate));
    }

    private static <T> Stream<T> filterCollection(Collection<T> collection, Predicate<? super T> predicate) {
        return collection.stream().filter(predicate);
    }

    private static <R> List<R> toList(Stream<R> stream) {
        return stream.collect(Collectors.toList());
    }

}