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

com.cudrania.core.collection.wrapper.MapSupplier Maven / Gradle / Ivy

There is a newer version: 1.2.7
Show newest version
package com.cudrania.core.collection.wrapper;

import com.cudrania.core.collection.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * 定义绑定到{@link Map}对象的函数集合
 *
 * @param 
 * @param 
 */
public interface MapSupplier extends Supplier> {


    /**
     * 翻转键值对
     *
     * @return
     */
    default Map> invert() {
        return CollectionUtils.groupBy(get().entrySet(), Map.Entry::getValue, Map.Entry::getKey);
    }


    /**
     * 翻转键值对,翻转后如果健值冲突,则进行覆盖。
     *
     * @return
     */
    default Map invertUnique() {
        return CollectionUtils.map(get().entrySet(), Map.Entry::getValue, Map.Entry::getKey);
    }

    /**
     * 分批处理键值对
     *
     * @param limit
     * @param consumer
     */
    default void doBatch(int limit, Consumer> consumer) {
        Map subMap = new HashMap<>(limit);
        for (Map.Entry entry : get().entrySet()) {
            subMap.put(entry.getKey(), entry.getValue());
            if (subMap.size() == limit) {
                consumer.accept(subMap);
                subMap = new HashMap<>(limit);
            }
        }
        if (subMap.size() > 0) {
            consumer.accept(subMap);
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy