簡體   English   中英

相當於Guava的Strings.isNullOrEmpty()的Long,Integer等

[英]Equivalent of Guava's Strings.isNullOrEmpty() for Long, Integer, etc

在諸如Java盒裝數字基元類型的番石榴等實用程序庫中是否有類似.isNullOrZero()的東西?

如果Integer,Long不為null ,則不能為null 在這種情況下可以為零。 因此,對於Integer或Long,您可以編寫如下內容-

public static boolean isNullOrZero( final Object obj) {

   if(null == obj) return true;

   if( obj instanceof Integer ){
      Integer i = (Integer) obj;
      return (i == 0);
   } 

   if( obj instanceof Long ){
      Long l = (Long) obj;
      return (l == 0);
   } 
}

對於Collection,如果它不為null,則可以為空。 然后,您可以像這樣編寫自己的isNullOrEmpty()方法-

public static boolean isNullOrEmpty( final Collection< ? > collection ) {
    return (collection == null || collection.isEmpty() );
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM