
com.hecloud.runtime.common.collections.Maps Maven / Gradle / Ivy
package com.hecloud.runtime.common.collections;
import com.alibaba.fastjson.JSONObject;
import java.util.*;
/**
* @author LoveinBJ
*/
public class Maps {
public static boolean isEmpty(Map map) {
return null == map || map.size() == 0;
}
/**
* 创建简单的Map
* 返回示例:{key : value}
*
* @param key 键
* @param value 值
* @return map对象
*/
public static Map single(String key, Object value) {
Map map = new HashMap<>(16, 0.75F);
map.put(key, value);
return map;
}
public static String string(Map map) {
final Map target = Optional.ofNullable(map).orElse(new HashMap<>(0));
clear(target);
StringBuilder builder = new StringBuilder();
List keyList = new ArrayList<>(map.keySet());
Collections.sort(keyList);
keyList.stream().filter(key -> Objects.nonNull(target.get(key)))
.forEach(key -> builder.append(key).append("=")
.append(JSONObject.toJSONString(map.get(key)).replace("\"", ""))
.append("&"));
builder.deleteCharAt(builder.length() - 1);
return builder.toString();
}
/**
* 移除key值为空或者value值为空的的数据
* 例如:sourceMap = {null:"c","b":"b"},处理之后为:sourceMap = {"b":"b"}
*
* @param source 待清理空值map
*/
private static void clear(Map source) {
source = Optional.ofNullable(source).orElse(new HashMap<>(0));
Iterator> iterator = source.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = iterator.next();
String key = entry.getKey();
Object value = entry.getValue();
if (null == key || null == value) {
iterator.remove();
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy