java Ejemplo de Java 8

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 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());
    }

}

以上是关于java Ejemplo de Java 8的主要内容,如果未能解决你的问题,请参考以下文章

PHP ejemplo de AJAX

PHP ejemplo de AJAX

typescript Ejemplo de测试路由

xml Ejemplo del XML de curvas de nivel 100k

JavaScript Ejemplo de Ajax.Responders(原型)

typescript Ejemplo de test para服务