All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.acyou.leo.framework.util.MapUtils Maven / Gradle / Ivy

package cn.acyou.leo.framework.util;

import cn.hutool.core.map.MapUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 
 *         MapListBuild<String, String> mapList1 = MapUtils.createMapList();
 *         mapList1.putValue("1", "1");
 *         mapList1.putValue("1", "2");
 *         mapList1.putValue("1", "3");
 *         mapList1.putValue("1", "4");
 *         mapList1.putValue("2", "1");
 *         mapList1.putValue("2", "2");
 *         mapList1.putValue("2", "3");
 *         mapList1.putValue("2", "4");
 *         mapList1.putValue("3", "1")
 *                 .putValue("3", "2")
 *                 .putValue("3", "2")
 *                 .putValue("3", "2");
 *
 *         System.out.println(mapList1.getMap());
 *
 *         Map<String, String> map = MapUtils.createMap("1", "2");
 *         System.out.println(map);
 *         Map<String, Integer> map1 = MapUtils.<String, Integer>createMap().putValue("1", 2).putValue("2", 3).getMap();
 *         System.out.println(map1);
 * 
* @author fangyou * @version [1.0.0, 2021-08-24 13:53] */ public class MapUtils extends MapUtil{ /** * 是空的 * * @param map 要检查的Map,可能为空 * @return 如果为空或null,则为true */ public static boolean isEmpty(final Map map) { return map == null || map.isEmpty(); } /** * 不是空的 * * @param map 要检查的Map,可能为空 * @return 如果为非null和非空,则为true */ public static boolean isNotEmpty(final Map map) { return !isEmpty(map); } public static Map createMap(String k, String v){ Map map = new HashMap<>(); map.put(k, v); return map; } /** ********************************** 构建 Map****************************************************** */ public static MapBuild createMap() { return new MapBuild<>(new HashMap<>()); } public static MapBuild createMap(Map map) { return new MapBuild<>(map); } public static class MapBuild { private final Map map; public MapBuild(Map map) { this.map = map; } public MapBuild putValue(K k, V v){ map.put(k, v); return this; } public Map getMap(){ return map; } } /** ********************************** 构建 Map>****************************************************** */ public static MapListBuild createMapList() { return new MapListBuild<>(new HashMap<>()); } public static MapListBuild createMapList(Map> map) { return new MapListBuild<>(map); } public static class MapListBuild { private final Map> map; public MapListBuild(Map> map) { this.map = map; } public MapListBuild putValue(K k, V v){ if (map.containsKey(k)) { map.get(k).add(v); }else { List vList = new ArrayList<>(); vList.add(v); map.put(k, vList); } return this; } public Map> getMap(){ return map; } } /** * 为Map> 添加值 */ public static Map> putList(Map> map, K k, V v){ if (map == null) { map = new HashMap<>(); } if (map.containsKey(k)) { map.get(k).add(v); }else { List vList = new ArrayList<>(); vList.add(v); map.put(k, vList); } return map; } /** * 为Map> 初始化 */ @SuppressWarnings("unchecked") public static Map> initList(Map> map, K... keys){ if (keys != null && keys.length > 0) { for (K key : keys) { map.put(key, new ArrayList<>()); } } return map; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy