package com.tzld.piaoquan.ad.engine.commons.util;


import org.apache.commons.collections.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author dyp
 */
public class CommonCollectionUtils {
    public static <T, R> List<R> toList(List<T> list, Function<T, R> map) {
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        return list.stream().map(map).collect(Collectors.toList());
    }

    public static <T, K, V> Map<K, V> toMap(List<T> list, Function<T, K> keyFunc, Function<T, V> valueFunc) {
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyMap();
        }
        return list.stream().collect(Collectors.toMap(keyFunc::apply, valueFunc::apply));
    }
}
