zoukankan      html  css  js  c++  java
  • Java 判断字符串是否为空的三种方法.

    /*对比s == null || s.equals("")
       *    s == null || s.length()<=0
       *    s == null || s.isEmpty()
       */

    方法一: 最多人使用的一个方法, 直观, 方便, 但效率很低.

    方法二: 比较字符串长度, 效率高, 是我知道的最好一个方法.

    方法三: Java SE 6.0 才开始提供的方法, 效率和方法二几乎相等, 但出于兼容性考虑, 推荐使用方法二.


    以下代码在我机器上的运行结果: (机器性能不一, 仅供参考)

    function 1 use time: 141ms

    function 2 use time: 46ms

    function 3 use time: 47ms


    public class CompareStringNothing {

    String s = "";

    long n = 10000000;


    private void function1() {

    long startTime = System.currentTimeMillis();


    for(long i = 0; i<n; i++) {

       if(s == null || s.equals(""));

    }

    long endTime = System.currentTimeMillis();


    System.out.println("function 1 use time: "+ (endTime - startTime) +"ms");

    }


    private void function2() {

    long startTime = System.currentTimeMillis();


    for(long i = 0; i< n; i++) {

       if(s == null || s.length() <= 0);

    }

    long endTime = System.currentTimeMillis();


    System.out.println("function 2 use time: "+ (endTime - startTime) +"ms");

    }


    private void function3() {

    long startTime = System.currentTimeMillis();


    for(long i = 0; i <n; i++) {

       if(s == null || s.isEmpty());

    }

    long endTime = System.currentTimeMillis();


    System.out.println("function 3 use time: "+ (endTime - startTime) +"ms");

    }


    public static void main(String[] args) {

    CompareStringNothing com = new CompareStringNothing();

    com.function1();

    com.function2();

    com.function3();

    }

    }

    我使用的equals()、length()和JDK6之后的 isEmpty(),在性能上原文分析结果是equals()性能几乎是length()的3.5倍,这个我不敢苟同,我实际测试的结果如下:

    equals use time: 110ms
    length use time: 78ms

    还没到2倍,于是乎我查看了String的源码:

        public int length() {
            return count;
        }

        public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = count;
            if (n == anotherString.count) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = offset;
            int j = anotherString.offset;
            while (n-- != 0) {
                if (v1[i++] != v2[j++])
                return false;
            }
            return true;
            }
        }

    通过源码我很容易发现length()的效率要高于equals(),length()直接返回String对象的Count属性,而equals()需要先进行判断目标比对对象的类型,然后再进行其它操作,同时还要遍历String对象的每一个char,查看是否相同。

    同时原文提到的JDK6提供的String .isEmpty()方法我没有安装JDK6,所以只能替代行的查看了Commons-lang-2.6内StriingUtils的该方法,我相信JDK6的源码与它应该是相同的,源码如下:

      public static boolean isEmpty(String str)
      {
        return (str == null) || (str.length() == 0);
      }

    可见isEmpty()同样也使用了length()方法,呵呵~只能说apache不傻。

    综上我们可以清晰的得出三者性能上的优劣。equals()  <<  length() / isEmpty()

  • 相关阅读:
    Mysql命令下导出select查询数据之 select ... into outfile方法
    接口调试工具Postman之自动同步Chrome cookies,实现自动登陆验证
    PHP函数file_get_contents()使用 https 协议时报错:SSL operation failed
    MySQL中连接超时自动断开的解决方案
    UEditor富文本WEB编辑器设置代码高亮
    Laravel 自定义公共函数全局使用,并设置自定加载
    Laravel 解决blade模板转义html标签问题
    PHP 高效导入导出Excel(csv)方法之fgetcsv()和fputcsv()函数
    Mysql命令行tab自动补全方法
    PHP利用get_headers()函数判断远程的url地址是否有效
  • 原文地址:https://www.cnblogs.com/wangs/p/2810829.html
Copyright © 2011-2022 走看看