学无先后,达者为师

网站首页 编程语言 正文

org.apache.commons.collections.MapUtils Map集合工具类

作者:Jothan Zhong 更新时间: 2024-01-11 编程语言
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.2</version>
</dependency>

包:org.apache.commons.collections4

public static void main(String[] args) {
    // 批量赋值
    Map<String, Object> colorMap = MapUtils.putAll(new HashMap<>(),
            new Object[] { "str", "我是字符串", "boo", true, "num", 1000 });
    System.out.println("colorMap:" + colorMap.toString()); // colorMap:{str=我是字符串, boo=true, num=1000}
    System.out.println("colorMap-isEmpty:" + MapUtils.isEmpty(colorMap)); // colorMap-isEmpty:false
    System.out.println("colorMap-isNotEmpty:" + MapUtils.isNotEmpty(colorMap)); // colorMap-isNotEmpty:true
 
    // 获取值,还有其他类型可以看MapUtils的API方法
    String str = MapUtils.getString(colorMap, "str");
    System.out.println("str:" + str); // str:我是字符串
 
    Boolean boo = MapUtils.getBoolean(colorMap, "boo");
    System.out.println("boo:" + boo); // boo:true
 
    Integer num = MapUtils.getInteger(colorMap, "num");
    System.out.println("num:" + num); // num:1000
 
    String not = MapUtils.getString(colorMap, "not");
    System.out.println("not:" + not); // not:null
 
    not = MapUtils.getString(colorMap, "not", "未知");
    System.out.println("not:" + not); // not:未知
 
    // 为NULL的情况设置值
    colorMap = MapUtils.emptyIfNull(null);
    System.out.println("colorMap:" + colorMap.toString()); // colorMap:{}
    System.out.println("colorMap-isEmpty:" + MapUtils.isEmpty(colorMap)); // colorMap-isEmpty:true
    System.out.println("colorMap-isNotEmpty:" + MapUtils.isNotEmpty(colorMap)); // colorMap-isNotEmpty:false
 
    // 判断值
    colorMap = null;
    System.out.println("colorMap-isEmpty:" + MapUtils.isEmpty(colorMap)); // colorMap-isEmpty:true
    System.out.println("colorMap-isNotEmpty:" + MapUtils.isNotEmpty(colorMap)); // colorMap-isNotEmpty:false
}

原文链接:https://blog.csdn.net/qq_43985303/article/details/135235853

  • 上一篇:没有了
  • 下一篇:没有了
栏目分类
最近更新