Skip to content
This repository has been archived by the owner on Aug 20, 2021. It is now read-only.

新增 MapUtil.putSumValue(Map<String, BigDecimal>, String, Number) #769

Closed
venusdrogon opened this issue Apr 8, 2019 · 0 comments
Closed
Assignees
Milestone

Comments

@venusdrogon
Copy link
Collaborator

新增 MapUtil.putSumValue(Map<String, BigDecimal>, String, Number)

    /**
     * 将<code>key</code>和<code>value</code> 累加的形式put到 map中,如果<code>map</code>中存在<code>key</code>,那么累加<code>value</code>值;如果不存在那么直接put.
     * 
     * <p>
     * 常用于数据统计, 比如 {@link com.feilong.core.util.AggregateUtil#groupSum(Iterable, String, String)}
     * </p>
     * 
     * <h3>示例:</h3>
     * 
     * <blockquote>
     * 
     * <pre class="code">
     * 
     * Map{@code <String, BigDecimal>} map = new HashMap{@code <>}();
     * MapUtil.putSumValue(map, "1000001", 5);
     * MapUtil.putSumValue(map, "1000002", 5);
     * MapUtil.putSumValue(map, "1000002", 5);
     * LOGGER.debug(JsonUtil.format(map));
     * 
     * </pre>
     * 
     * <b>返回:</b>
     * 
     * <pre class="code">
     * {
     * "1000001": 5,
     * "1000002": 10
     * }
     * </pre>
     * 
     * </blockquote>
     * 
     * @param <K>
     *            the key type
     * @param map
     *            the map
     * @param key
     *            the key
     * @param value
     *            数值,不能为null,可以是负数
     * @return 如果 <code>map</code> 是null,抛出 {@link NullPointerException}<br>
     *         如果 <code>value</code> 是null,抛出 {@link NullPointerException}<br>
     * @see org.apache.commons.collections4.bag.HashBag
     * @see "java.util.Map#getOrDefault(Object, Object)"
     * @see <a href="http://stackoverflow.com/questions/81346/most-efficient-way-to-increment-a-map-value-in-java">most-efficient-way-to-
     *      increment-a-map-value-in-java</a>
     * @since 1.13.2
     */
    public static <K> Map<K, BigDecimal> putSumValue(Map<K, BigDecimal> map,K key,Number value){
        Validate.notNull(map, "map can't be null!");
        Validate.notNull(value, "value can't be null!");

        BigDecimal v = map.get(key);//这里不要使用 map.containsKey(key),否则会有2次  two potentially expensive operations
        map.put(key, null == v ? toBigDecimal(value) : NumberUtil.getAddValue(value, v));//Suggestion: you should care about code readability more than little performance gain in most of the time.
        return map;
    }
@venusdrogon venusdrogon added this to the 1.13.2 milestone Apr 8, 2019
@venusdrogon venusdrogon self-assigned this Apr 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant