V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
RedBeanIce
V2EX  ›  Java

[objectUitls] [stringUtils]

  •  
  •   RedBeanIce · 2021-12-22 12:02:42 +08:00 · 1225 次点击
    这是一个创建于 856 天前的主题,其中的信息可能已经有所发展或是发生改变。
    public class ObjectUtilsTest {
        public static void main(String[] args) {
            List<String> list = new ArrayList<>();
            Set<String> set = new HashSet<>();
            Map<String, String> map = new HashMap<>();
            Integer[] arr = {};
            Integer integer = null;
            String str1 = null;
            String str2 = "";
            String str3 = "s";
            System.out.println("list = " + ObjectUtils.isEmpty(list));
            System.out.println("set = " + ObjectUtils.isEmpty(set));
            System.out.println("map = " + ObjectUtils.isEmpty(map));
            System.out.println("arr = " + ObjectUtils.isEmpty(arr));
            System.out.println("integer = " + ObjectUtils.isEmpty(integer));
            System.out.println("str1 = " + ObjectUtils.isEmpty(str1));
            System.out.println("str2 = " + ObjectUtils.isEmpty(str2));
            System.out.println("str3 = " + ObjectUtils.isEmpty(str3));
        }
    }
    
    list = true
    set = true
    map = true
    arr = true
    integer = true
    str1 = true
    str2 = true
    str3 = false
    
    

    请问这种情况下,stringutils.isblank 的意义在哪里呢,,,

    感觉处理,只判断 string=""时,需要用到 stringutils.isempty

    stringutils.isblank 的存在意义,被 ObjectUtils.isEmpty 全部压住了

    lovedoing
        1
    lovedoing  
       2021-12-22 12:10:01 +08:00
    Checks if a String is whitespace, empty ("") or null.
    gadfly3173
        2
    gadfly3173  
       2021-12-22 12:20:37 +08:00
    首先 blank 最主要是判断字符串是不是全是空格,其次这种名字的 Util 这么多,不如先说说这是哪个包里的。。。
    RedBeanIce
        3
    RedBeanIce  
    OP
       2021-12-22 12:21:32 +08:00
    @lovedoing 我无法理解回复的意思,我只能理解到是在回答 stringutils.isblank 的功能。

    可是问题的原意是,stringutils.isblank 的存在意义,ObjectUtils.isEmpty 完全可以做到 stringutils.isblank 的功能
    RedBeanIce
        4
    RedBeanIce  
    OP
       2021-12-22 12:22:54 +08:00
    @gadfly3173 spring 自带
    GM
        5
    GM  
       2021-12-22 12:24:07 +08:00
    ```
    isBlank(" ") == true;
    isEmpty(" ") == false;

    ```
    RedBeanIce
        6
    RedBeanIce  
    OP
       2021-12-22 12:24:50 +08:00
    我懂了,,又是一个傻子般的问题。
    RedBeanIce
        7
    RedBeanIce  
    OP
       2021-12-22 12:30:36 +08:00 via iPhone
    问问题应该先看源码的,犯了提问的艺术的错误
    monetto
        8
    monetto  
       2021-12-22 12:31:09 +08:00
    其他的就不说了,补充一点,语义化作用。
    StringUtils 会让人联想到 String 的一些判断逻辑。
    而 ObjectUtil 给人感觉更是通用的一些逻辑,会让人反应一秒,或者需要点进源码里看一看。
    gadfly3173
        9
    gadfly3173  
       2021-12-22 16:59:42 +08:00
    @RedBeanIce #4 spring 自带的 isBlank 应该是被废弃的吧。。。现在是用 hasText 和 hasLength 代替
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5415 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 06:57 · PVG 14:57 · LAX 23:57 · JFK 02:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.