public static <K, V, O> Map<K, Set<V>> oneToManyMap(List<O> list, Function<O, K> keyFunction, Function<O, V> valueFunction) { if (list == null) { return null; } if (list.size() == 0) { return Collections.emptyMap(); } Map<K, Set<V>> res = new HashMap<>(); list.stream().forEach(o -> { K key = keyFunction.apply(o); V value = valueFunction.apply(o); if (res.containsKey(key)) { res.get(key).add(value); } else { Set<V> values = new HashSet<>(); values.add(value); res.put(key, values); } }); return res; }
转换list为map (1个key 对应 多个value)
最新推荐文章于 2023-11-10 11:10:04 发布