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

Commit

Permalink
move MapUtil T getMinValue(Map<K, T> map,K...keys)
Browse files Browse the repository at this point in the history
StatisticsUtil add `T getMinValue(Map<K, T> map,K...keys)`
  • Loading branch information
venusdrogon committed Jul 5, 2016
1 parent d739109 commit ed964ad
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 65 deletions.
47 changes: 0 additions & 47 deletions src/main/java/com/feilong/core/util/MapUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.feilong.core.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
Expand Down Expand Up @@ -540,52 +539,6 @@ public static <K, V> Map<K, List<V>> putMultiValue(Map<K, List<V>> map,K key,V v
return map;
}

/**
* 指定一个<code>map</code>,指定特定的keys,取得其中的 value 最小值.
*
* <h3>示例:</h3>
* <blockquote>
*
* <pre class="code">
* Map{@code <String, Integer>} map = new HashMap{@code <String, Integer>}();
*
* map.put("a", 3007);
* map.put("b", 3001);
* map.put("c", 3002);
* map.put("d", 3003);
* map.put("e", 3004);
* map.put("f", 3005);
* map.put("g", -1005);
*
* LOGGER.info("" + MapUtil.getMinValue(map, "a", "b", "d", "g", "m"));
* </pre>
*
* 返回:
* -1005
*
* </blockquote>
*
* @param <K>
* the key type
* @param <T>
* the generic type
* @param map
* 指定一个map
* @param keys
* 指定特定的key
* @return 如果 <code>map</code> 是null或者empty,返回 null;<br>
* 如果 <code>keys</code> 是null或者empty,返回<code>map</code>所有value的最小值<br>
* 如果循环的 key不在map key里面,则返回的map中忽略该key,并输出warn level log<br>
* 如果 keys 中的所有的key 都不在 map 中出现 ,那么返回null
* @see #getSubMap(Map, Object...)
* @see java.util.Collections#min(Collection)
*/
@SafeVarargs
public static <K, T extends Number & Comparable<? super T>> T getMinValue(Map<K, T> map,K...keys){
Map<K, T> subMap = getSubMap(map, keys);
return Validator.isNullOrEmpty(subMap) ? null : Collections.min(subMap.values()); //注意 Number本身 没有实现Comparable接口
}

/**
* 获得一个<code>map</code> 中的按照指定的<code>key</code> 整理成新的map.
*
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/com/feilong/core/util/StatisticsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -520,4 +520,50 @@ public static <T, O> Map<T, Integer> groupCount(Collection<O> objectCollection,S
}
return map;
}

/**
* 指定一个<code>map</code>,指定特定的keys,取得其中的 value 最小值.
*
* <h3>示例:</h3>
* <blockquote>
*
* <pre class="code">
* Map{@code <String, Integer>} map = new HashMap{@code <String, Integer>}();
*
* map.put("a", 3007);
* map.put("b", 3001);
* map.put("c", 3002);
* map.put("d", 3003);
* map.put("e", 3004);
* map.put("f", 3005);
* map.put("g", -1005);
*
* LOGGER.info("" + MapUtil.getMinValue(map, "a", "b", "d", "g", "m"));
* </pre>
*
* 返回:
* -1005
*
* </blockquote>
*
* @param <K>
* the key type
* @param <T>
* the generic type
* @param map
* 指定一个map
* @param keys
* 指定特定的key
* @return 如果 <code>map</code> 是null或者empty,返回 null;<br>
* 如果 <code>keys</code> 是null或者empty,返回<code>map</code>所有value的最小值<br>
* 如果循环的 key不在map key里面,则返回的map中忽略该key,并输出warn level log<br>
* 如果 keys 中的所有的key 都不在 map 中出现 ,那么返回null
* @see MapUtil#getSubMap(Map, Object...)
* @see java.util.Collections#min(Collection)
*/
@SafeVarargs
public static <K, T extends Number & Comparable<? super T>> T getMinValue(Map<K, T> map,K...keys){
Map<K, T> subMap = MapUtil.getSubMap(map, keys);
return Validator.isNullOrEmpty(subMap) ? null : Collections.min(subMap.values()); //注意 Number本身 没有实现Comparable接口
}
}
18 changes: 0 additions & 18 deletions src/test/java/com/feilong/core/util/MapUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,24 +210,6 @@ public void testGetSubMapExcludeKeys(){
assertThat(subMapExcludeKeys, allOf(hasEntry("b", 3001), hasEntry("c", 3002), not(hasKey("a")), not(hasKey("g"))));
}

/**
* Test get min value.
*/
@Test
public void testGetMinValue(){
Map<String, Integer> map = new HashMap<String, Integer>();

map.put("a", 3007);
map.put("b", 3001);
map.put("c", 3002);
map.put("d", 3003);
map.put("e", 3004);
map.put("f", 3005);
map.put("g", -1005);

assertThat(MapUtil.getMinValue(map, "a", "b", "d", "g", "m"), is(-1005));
}

/**
* Test sort by value asc.
*/
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/com/feilong/core/util/StatisticsUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import static com.feilong.core.bean.ConvertUtil.toList;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -186,4 +188,22 @@ public void testGroupCount1(){
Map<String, Integer> map = StatisticsUtil.groupCount(list, "name");
assertThat(map, allOf(hasEntry("刘备", 2), hasEntry("张飞", 1), hasEntry("关羽", 1)));
}

/**
* Test get min value.
*/
@Test
public void testGetMinValue(){
Map<String, Integer> map = new HashMap<String, Integer>();

map.put("a", 3007);
map.put("b", 3001);
map.put("c", 3002);
map.put("d", 3003);
map.put("e", 3004);
map.put("f", 3005);
map.put("g", -1005);

assertThat(StatisticsUtil.getMinValue(map, "a", "b", "d", "g", "m"), is(-1005));
}
}

0 comments on commit ed964ad

Please sign in to comment.