All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sportradar.utils.StreamUtils Maven / Gradle / Ivy

/*
 * Copyright (C) Sportradar AG. See LICENSE for full license governing this code
 */

package com.sportradar.utils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * Generic stream utility methods
 */
public class StreamUtils {
    private StreamUtils() {}

    /**
     * Utility method used to filter out objects by some parameter
     *
     * @param keyExtractor the provider of the parameter on which to distinct the objects
     * @param  the objects type
     * @return true if the object was already filtered, otherwise false
     */
    public static  Predicate distinctObjects(Function keyExtractor) {
        Map seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy