doc

介绍

MapUtil是针对Map的一一列工具方法的封装,包括getXXX的快捷值转换方法。

方法

Map<Object, Object> colorMap = MapUtil.of(new String[][] {
     {"RED", "#FF0000"},
     {"GREEN", "#00FF00"},
     {"BLUE", "#0000FF"}
});
[
  {a: 1, b: 1, c: 1},
  {a: 2, b: 2},
  {a: 3, b: 3},
  {a: 4}
]

结果为:

{
   a: [1,2,3,4],
   b: [1,2,3,],
   c: [1]
}
{
   a: [1,2,3,4],
   b: [1,2,3,],
   c: [1]
}

结果为:

[
  {a: 1, b: 1, c: 1},
  {a: 2, b: 2},
  {a: 3, b: 3},
  {a: 4}
]
Map<String, String> build = MapUtil.builder(new HashMap<String, String>())
	.put("key1", "value1")
	.put("key3", "value3")
	.put("key2", "value2").build();

// key1value1key2value2key3value3
String join1 = MapUtil.sortJoin(build, StrUtil.EMPTY, StrUtil.EMPTY, false);
// key1value1key2value2key3value3123
String join2 = MapUtil.sortJoin(build, StrUtil.EMPTY, StrUtil.EMPTY, false, "123");
Map<String, String> map = MapUtil.newHashMap();
map.put("a", "1");
map.put("b", "2");
map.put("c", "3");
map.put("d", "4");

Map<String, String> map2 = MapUtil.filter(map, (Filter<Entry<String, String>>) t -> Convert.toIn(t.getValue()) % 2 == 0);

结果为

{
   b: "2",
   d: "4"
}

定义个小枚举

enum PeopleEnum {GIRL, BOY, CHILD}

开始操作

Map<Integer, String> adjectivesMap = MapUtil.<Integer, String>builder()
.put(0, "lovely")
.put(1, "friendly")
.put(2, "happily")
.build();

Map<Integer, String> resultMap = MapUtil.map(adjectivesMap, (k, v) -> v + " " + PeopleEnum.values()[k].name().toLowerCase());

结果为

{
  0: "lovely girl",
  1: "friendly boy",
  2: "happily child"
}
Map<String, String> map = MapUtil.newHashMap();
		map.put("a", "1");
		map.put("b", "2");
		map.put("c", "3");
		map.put("d", "4");

Map<String, String> map2 = MapUtil.reverse(map);

结果为:

{
   "1": "a",
   "2": "b",
   "3": "c",
   "4": "d",
}