io.github.kits.MapKit Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of whimthen-kits Show documentation
Show all versions of whimthen-kits Show documentation
Easy to use java tool library.
The newest version!
package io.github.kits;
import io.github.kits.json.JsonKit;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Map tool class
*
* @project: kits
* @created: with IDEA
* @author: kits
* @date: 2018 04 26 下午5:31 | 四月. 星期四
*/
public class MapKit {
/**
* 判断map是否为空
*
* @param map
* @return
*/
public static boolean isNullOrEmpty(Map map) {
return map == null || map.isEmpty();
}
/**
* 判断map是否不为空
*
* @param map
* @return
*/
public static boolean isNotNullOrEmpty(Map map) {
return !isNullOrEmpty(map);
}
/**
* 将map转换为json
*
* @param map 任意map
* @return 转换后的结果
*/
public static String toJSON(Map, ?> map) {
StringBuilder jsonString = new StringBuilder("{");
if (isNullOrEmpty(map)) {
return "null";
}
map.forEach((key, value) -> {
if (isStringOrDate(key)) {
jsonString.append("\"").append(JsonKit.toJson(key)).append("\"");
} else {
jsonString.append(JsonKit.toJson(key));
}
if (isStringOrDate(value)) {
jsonString.append(": \"").append(JsonKit.toJson(value)).append("\"");
} else {
jsonString.append(": ").append(JsonKit.toJson(value));
}
jsonString.append(", ");
});
return jsonString.deleteCharAt(jsonString.lastIndexOf(",")).deleteCharAt(jsonString.lastIndexOf(" ")).append("}").toString();
}
/**
* 判断是否是String或Date类型
*
* @param object 需要判断的对象
* @return 是(true)
*/
private static boolean isStringOrDate(Object object) {
if (object instanceof String || object instanceof Character || object instanceof Date) {
return true;
}
return false;
}
/**
* 将多个参数组合为map
*
* @param objs
* @return
*/
public static Map asMap(String... objs) {
Map