mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add emptyIfNull
This commit is contained in:
parent
d5076d9b33
commit
2f2c9e58d2
@ -8,6 +8,7 @@
|
||||
### 新特性
|
||||
* 【core】 改进CollUtil.zip逻辑,减少内存复制(issue#I10T01@Gitee)
|
||||
* 【extra】 邮件增加图片支持(pr#495@Github)
|
||||
* 【core】 MapUtil、CollUtil增加emptyIfNull(issue#502@Github)
|
||||
|
||||
### Bug修复
|
||||
* 【http】 修复HttpRquest中body方法长度计算问题(issue#I10UPG@Gitee)
|
||||
|
@ -60,6 +60,32 @@ import cn.hutool.core.util.TypeUtil;
|
||||
*/
|
||||
public class CollUtil {
|
||||
|
||||
/**
|
||||
* 如果提供的集合为{@code null},返回一个不可变的默认空集合,否则返回原集合<br>
|
||||
* 空集合使用{@link Collections#emptySet()}
|
||||
*
|
||||
* @param <T> 集合元素类型
|
||||
* @param set 提供的集合,可能为null
|
||||
* @return 原集合,若为null返回空集合
|
||||
* @since 4.6.3
|
||||
*/
|
||||
public static <T> Set<T> emptyIfNull(Set<T> set) {
|
||||
return (null == set) ? Collections.<T>emptySet() : set;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果提供的集合为{@code null},返回一个不可变的默认空集合,否则返回原集合<br>
|
||||
* 空集合使用{@link Collections#emptyList()}
|
||||
*
|
||||
* @param <T> 集合元素类型
|
||||
* @param set 提供的集合,可能为null
|
||||
* @return 原集合,若为null返回空集合
|
||||
* @since 4.6.3
|
||||
*/
|
||||
public static <T> List<T> emptyIfNull(List<T> set) {
|
||||
return (null == set) ? Collections.<T>emptyList() : set;
|
||||
}
|
||||
|
||||
/**
|
||||
* 两个集合的并集<br>
|
||||
* 针对一个集合中存在多个相同元素的情况,计算两个集合中此元素的个数,保留最多的个数<br>
|
||||
@ -852,7 +878,7 @@ public class CollUtil {
|
||||
return null;
|
||||
}
|
||||
|
||||
if(list.isEmpty()) {
|
||||
if (list.isEmpty()) {
|
||||
return new ArrayList<>(0);
|
||||
}
|
||||
|
||||
@ -1161,7 +1187,7 @@ public class CollUtil {
|
||||
*/
|
||||
public static List<Object> extract(Iterable<?> collection, Editor<Object> editor, boolean ignoreNull) {
|
||||
final List<Object> fieldValueList = new ArrayList<>();
|
||||
if(null == collection) {
|
||||
if (null == collection) {
|
||||
return fieldValueList;
|
||||
}
|
||||
|
||||
@ -1863,7 +1889,7 @@ public class CollUtil {
|
||||
}
|
||||
|
||||
final int size = collection.size();
|
||||
if(0 == size) {
|
||||
if (0 == size) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -2428,12 +2454,12 @@ public class CollUtil {
|
||||
* @return key集合
|
||||
* @since 4.5.12
|
||||
*/
|
||||
public static <K> Set<K> keySet(Collection<Map<K, ?>> mapCollection){
|
||||
if(isEmpty(mapCollection)) {
|
||||
public static <K> Set<K> keySet(Collection<Map<K, ?>> mapCollection) {
|
||||
if (isEmpty(mapCollection)) {
|
||||
return new HashSet<>();
|
||||
}
|
||||
final HashSet<K> set = new HashSet<>(mapCollection.size() * 16);
|
||||
for (Map<K,?> map : mapCollection) {
|
||||
for (Map<K, ?> map : mapCollection) {
|
||||
set.addAll(map.keySet());
|
||||
}
|
||||
|
||||
@ -2448,7 +2474,7 @@ public class CollUtil {
|
||||
* @return Value集合
|
||||
* @since 4.5.12
|
||||
*/
|
||||
public static <V> List<V> values(Collection<Map<?, V>> mapCollection){
|
||||
public static <V> List<V> values(Collection<Map<?, V>> mapCollection) {
|
||||
final List<V> values = new ArrayList<>();
|
||||
for (Map<?, V> map : mapCollection) {
|
||||
values.addAll(map.values());
|
||||
|
@ -31,6 +31,17 @@ public class DateTime extends Date {
|
||||
/** 时区 */
|
||||
private TimeZone timeZone;
|
||||
|
||||
/**
|
||||
* 转换时间戳为 DateTime
|
||||
*
|
||||
* @param timeMillis 时间戳,毫秒数
|
||||
* @return DateTime
|
||||
* @since 4.6.3
|
||||
*/
|
||||
public static DateTime of(long timeMillis) {
|
||||
return new DateTime(timeMillis);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换JDK date为 DateTime
|
||||
*
|
||||
|
@ -2,6 +2,7 @@ package cn.hutool.core.map;
|
||||
|
||||
import java.util.AbstractMap;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
@ -57,6 +58,19 @@ public class MapUtil {
|
||||
return null != map && false == map.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果提供的集合为{@code null},返回一个不可变的默认空集合,否则返回原集合<br>
|
||||
* 空集合使用{@link Collections#emptyMap()}
|
||||
*
|
||||
* @param <T> 集合元素类型
|
||||
* @param set 提供的集合,可能为null
|
||||
* @return 原集合,若为null返回空集合
|
||||
* @since 4.6.3
|
||||
*/
|
||||
public static <K, V> Map<K, V> emptyIfNull(Map<K, V> set) {
|
||||
return (null == set) ? Collections.<K, V>emptyMap() : set;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------------- new HashMap
|
||||
/**
|
||||
* 新建一个HashMap
|
||||
@ -141,7 +155,7 @@ public class MapUtil {
|
||||
* @return {@link IdentityHashMap}
|
||||
* @since 4.5.7
|
||||
*/
|
||||
public static <K, V> Map<K, V> newIdentityMap(int size){
|
||||
public static <K, V> Map<K, V> newIdentityMap(int size) {
|
||||
return new IdentityHashMap<>(size);
|
||||
}
|
||||
|
||||
@ -204,11 +218,7 @@ public class MapUtil {
|
||||
* </pre>
|
||||
*
|
||||
* <pre>
|
||||
* Map<Object, Object> colorMap = MapUtil.of(new String[][] {
|
||||
* {"RED", "#FF0000"},
|
||||
* {"GREEN", "#00FF00"},
|
||||
* {"BLUE", "#0000FF"}
|
||||
* });
|
||||
* Map<Object, Object> colorMap = MapUtil.of(new String[][] { { "RED", "#FF0000" }, { "GREEN", "#00FF00" }, { "BLUE", "#0000FF" } });
|
||||
* </pre>
|
||||
*
|
||||
* 参考:commons-lang
|
||||
@ -397,15 +407,15 @@ public class MapUtil {
|
||||
* @since 4.1.9
|
||||
*/
|
||||
public static Object[][] toObjectArray(Map<?, ?> map) {
|
||||
if(map == null) {
|
||||
if (map == null) {
|
||||
return null;
|
||||
}
|
||||
final Object[][] result = new Object[map.size()][2];
|
||||
if(map.isEmpty()) {
|
||||
if (map.isEmpty()) {
|
||||
return result;
|
||||
}
|
||||
int index = 0;
|
||||
for(Entry<?, ?> entry : map.entrySet()) {
|
||||
for (Entry<?, ?> entry : map.entrySet()) {
|
||||
result[index][0] = entry.getKey();
|
||||
result[index][1] = entry.getValue();
|
||||
index++;
|
||||
@ -489,7 +499,7 @@ public class MapUtil {
|
||||
* @return 过滤后的Map
|
||||
*/
|
||||
public static <K, V> Map<K, V> filter(Map<K, V> map, Editor<Entry<K, V>> editor) {
|
||||
if(null == map || null == editor) {
|
||||
if (null == map || null == editor) {
|
||||
return map;
|
||||
}
|
||||
|
||||
@ -525,7 +535,7 @@ public class MapUtil {
|
||||
* @since 3.1.0
|
||||
*/
|
||||
public static <K, V> Map<K, V> filter(Map<K, V> map, Filter<Entry<K, V>> filter) {
|
||||
if(null == map || null == filter) {
|
||||
if (null == map || null == filter) {
|
||||
return map;
|
||||
}
|
||||
|
||||
@ -562,7 +572,7 @@ public class MapUtil {
|
||||
|
||||
map2.clear();
|
||||
for (K key : keys) {
|
||||
if(map.containsKey(key)) {
|
||||
if (map.containsKey(key)) {
|
||||
map2.put(key, map.get(key));
|
||||
}
|
||||
}
|
||||
@ -895,8 +905,8 @@ public class MapUtil {
|
||||
* @since 4.5.16
|
||||
*/
|
||||
public static <K, V> Map<K, V> renameKey(Map<K, V> map, K oldKey, K newKey) {
|
||||
if(isNotEmpty(map) && map.containsKey(oldKey)) {
|
||||
if(map.containsKey(newKey)) {
|
||||
if (isNotEmpty(map) && map.containsKey(oldKey)) {
|
||||
if (map.containsKey(newKey)) {
|
||||
throw new IllegalArgumentException(StrUtil.format("The key '{}' exist !", newKey));
|
||||
}
|
||||
map.put(newKey, map.remove(oldKey));
|
||||
|
@ -94,7 +94,7 @@ public class JSONUtilTest {
|
||||
JSONObject propJson = JSONUtil.parseObj(prop);
|
||||
Assert.assertEquals("男", propJson.getStr("gender"));
|
||||
Assert.assertEquals(18, propJson.getInt("age").intValue());
|
||||
// Assert.assertEquals("{\"age\":18,\"gender\":\"男\"}", user.getProp());
|
||||
// Assert.assertEquals("{\"age\":18,\"gender\":\"男\"}", user.getProp());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
x
Reference in New Issue
Block a user