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

com.github.sinjar.common.ext.bean.BeanListUtils Maven / Gradle / Ivy

There is a newer version: 1.3
Show newest version
package com.github.sinjar.common.ext.bean;

import com.github.sinjar.common.outer.guava.Maps;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * Description : 
 *
 * @author CPF
 * Date: 2020/6/15 15:48
 */
@Slf4j
public class BeanListUtils {

    private BeanListUtils() {}

    /**
     * 将 list 按照指定规则转为 Map
     *
     * @param getHKey 从对象中寻求 hash key 的方法
     * @param list 待转换的list
     * @param  hash key
     * @param  hash value
     * @return 转换后的map
     */
    public static  Map castListToMap(@NonNull List list, @NonNull Function getHKey) {
        if (list.isEmpty()) {
            return new HashMap<>();
        }
        Map map = Maps.newHashMapWithExpectedSize(list.size());
        list.forEach(it -> {
            if (it == null) {
                return;
            }
            K key = getHKey.apply(it);
            if (key != null) {
                map.put(key, it);
            }
        });
        return map;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy