几个简单的map操作工具类

public class ZYMapUtils extends MapUtil {

	// 根据key收藏model
    public static <V> void flushMap(String key, Map<String, List<V>> map, V v) {
        List<V> vs = map.containsKey(key) ? map.get(key) : new ArrayList<>();
        vs.add(v);
        map.put(key, vs);
    }

	// map转换k或value
    public static <A, B, C, D> Map<A, B> map2map(Map<C, D> sourceMap, Function<C, A> keyFunction,
                                                 Function<D, B> valueFunction) {
        Map<A, B> targetMap = new HashMap<>();
        if (null == sourceMap || sourceMap.isEmpty()) {
            return targetMap;
        }
        sourceMap.forEach((k, v) -> {
            if (ZYStrUtils.isNotNull(v)) {
                targetMap.put(keyFunction.apply(k), valueFunction.apply(v));
            }
        });

        return targetMap;
    }

	// map key转换
    public static <A, C, D> Map<A, D> map2mapKey(Map<C, D> sourceMap, Function<C, A> keyFunction) {
        return map2map(sourceMap, keyFunction, v -> v);
    }

    public static <A, B, D> Map<A, B> map2mapValue(Map<A, D> sourceMap, Function<D, B> valueFunction) {
        return map2map(sourceMap, k -> k, valueFunction);
    }

	// 收藏list实体的某个字段
    public static <T> Map<String, List<String>> map2mapField(Map<String, List<T>> sourceMap, Function<T, String> valueFunction) {
        Map<String, List<String>> map = new HashMap<>();
        if (isEmptyMap(sourceMap)) {
            return map;
        }
        sourceMap.forEach((k, ts) -> {
            List<java.lang.String> strings = ZYListUtils.collectField(ts, valueFunction);
            map.put(k, strings);
        });
        return map;
    }


	// 统计某个字段出现的次数
    public static <T> Map<String, Integer> countField(List<T> ts, Function<T, String> fieldValueFun) {
        Map<String, Integer> countMap = new HashMap<>();
        ts.forEach(t -> {
            String fieldValue = fieldValueFun.apply(t);
            if (ZYStrUtils.isNotNull(fieldValue)) {
                Integer count = countMap.get(fieldValue);
                countMap.put(fieldValue, null == count ? 1 : count + 1);
            }
        });
        return countMap;
    }
    
    public static <T> Map<String, Integer> countFields(List<T> ts, Function<T, List<String>> fieldValueFun) {
        Map<String, Integer> countMap = new HashMap<>();
        ts.forEach(t -> toCount(fieldValueFun, countMap, t));
        return countMap;
    }

    private static <T> void toCount(Function<T, List<String>> fieldValueFun, Map<String, Integer> countMap, T t) {
        List<String> fieldValues = fieldValueFun.apply(t);
        if (null != fieldValues && fieldValues.size() > 0) {
            fieldValues.forEach(fieldValue -> {
                if (ZYStrUtils.isNotNull(fieldValue)) {
                    Integer count = countMap.get(fieldValue);
                    countMap.put(fieldValue, null == count ? 1 : count + 1);
                }
            });
        }
    }

    public static <K, V> boolean isEmptyMap(Map<K, V> map) {
        return null == map || map.isEmpty();
    }

    public static <K, V> boolean isNotEmptyMap(Map<K, V> map) {
        return !isEmptyMap(map);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值