簡體   English   中英

如何驗證地圖是否為空或泛型

[英]how to validate map is null or not in generic

我需要驗證Map值是否為null,因為我與集合合作,願意實現驗證Map值的通用方法,因此嘗試了

public boolean checkNull(Map<String,Object> data){

        if(data != null && !data.isEmpty()){
            return true;
        }
        return false;
    }

現在我只能使用鍵作為字符串和值作為對象來驗證我的地圖,但是我還需要使用任何類型的地圖Map或Map.etc來檢查此空條件,因此我該如何執行泛型函數來驗證此地圖處理。

  public boolean checkNull(Map<K,V> data){

            if(data != null && !data.isEmpty()){
                return true;
            }
            return false;
        }

有人可以幫助我深入理解這一邏輯。

 public boolean checkNull(Map<String,?> data){

       // and you can also use this instead of the if:
       return data != null && !data.isEmpty();
    }

要么

 public boolean checkNull(Map<?,?> data){

取決於編譯器已知或應檢查的內容。

嘗試將Map<K,V> data替換為Map<?, ?> data

用這個:

public <K, V> boolean checkNull(Map<K, V> entry){
    if(entry!=null&&!entry.isEmpty()){
        return true;
    }
    return false;
}

然后<String, Integer> checkNull(myStringAndIntegerMapInstance)您可以調用<String, Integer> checkNull(myStringAndIntegerMapInstance) ,以確保在運行時也鍵入引用問題。 有關更多信息,請參見javadoc

暫無
暫無

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

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