List 中去除 null 方法討論


先看下面的程序段:

public static void main(String[] args) {  
    List<Integer> arrays = new ArrayList<Integer>();  
    arrays.add(2);  
    arrays.add(null);  
    arrays.add(456);  
    arrays.add(null);  
    arrays.add(789);  
    System.out.println(arrays);  
}  

注:一個list,向其中插入數據時,也插入一些null。程序輸出如下:

[2, null, 456, null, 789]  

 現在有這個需求:去除list中null 元素。嘗試的代碼如下:

public static void main(String[] args) {  
    List<Integer> arrays = new ArrayList<Integer>();  
    arrays.add(2);  
    arrays.add(null);  
    arrays.add(456);  
    arrays.add(null);  
    arrays.add(789);  
    arrays.remove(null);  
    System.out.println(arrays);  
}  

調用remove(object)方法,程序的輸出如下:

[2, 456, null, 789]  

可以看出:只remove了第一個null元素。這不是我們期望的結果。繼續找方法。考慮到有一個removeAll(Collection<?> c) ,嘗試使用。代碼如下:

public static void main(String[] args) {  
    List<Integer> arrays = new ArrayList<Integer>();  
    arrays.add(2);  
    arrays.add(null);  
    arrays.add(456);  
    arrays.add(null);  
    arrays.add(789);  
    List<Integer> nullArr = new ArrayList<Integer>();  
    nullArr.add(null);  
    arrays.removeAll(nullArr);  
    System.out.println(arrays);  
}  

程序的輸出如下:

[2, 456, 789]  

這是我們期望的結果。你可能會嘗試下面這樣使用:

arrays.removeAll(null); 

很遺憾,程序出錯了:Exception in thread "main" java.lang.NullPointerException。

到這里,我們似乎找到了解決問題的辦法。但是,如果我們的系統中,有這種類型的List<E>,如List<TempProductDto>、List<merchantDto> 時,

我們要從這些List中移除掉null,就要創建如下的代碼:

List<TempProductDto> nullTempProd = new ArrayList<TempProductDto>(1);  
nullTempProd.add(null);  
  
List<MerchantDto> nullMerchant = new ArrayList<MerchantDto>(1);  
nullMerchant.add(null);  

每種類型,就要創建對應類型的List,並把null 放入到List中。是不是很麻煩。能不能寫個公用的Util類呢?以下是我寫的Util 類:

import java.io.Serializable;  
import java.util.AbstractList;  
import java.util.RandomAccess;  
  
public class NullCollection extends AbstractList<Object>  
implements RandomAccess, Serializable  {  
  
    private static final long serialVersionUID = 5206887786441397812L;  
  
    @Override  
    public Object get(int index) {  
        return null;  
    }  
  
    @Override  
    public int size() {  
        return 1;  
    }  
      
    public boolean contains(Object obj) {  
        return null == obj;  
    }  
      
    private Object readResolve() {  
        return null;  
    }  
}  
import java.util.Collection;  
import java.util.List;  
  
public class YHDCollectionUtils {  
      
     public static final Collection NULL_COLLECTION = new NullCollection();  
          
    public static final <T> Collection<T> nullCollection() {  
        return (List<T>) NULL_COLLECTION;  
    }  
}  

使用我寫的util類進行測試。代碼如下:

public static void main(String[] args) {  
    List<Integer> arrays = new ArrayList<Integer>();  
    arrays.add(2);  
    arrays.add(null);  
    arrays.add(456);  
    arrays.add(null);  
    arrays.add(789);  
    arrays.removeAll(YHDCollectionUtils.nullCollection());  
    System.out.println(arrays);  
}  

執行結果如下:

[2, 456, 789]  

Util 類可以成功的去除List中的null元素。

 

 也許你會問:為什么要把null放入List中,只有2B青年會這么干?在一般業務中,我們確實不需要把null放入List中,但有一種場景:

  從頁面封裝的List,如下面的代碼:

<input name="dto.productList[0].name" value="我是名稱1">  
<input name="dto.productList[0].price" value="我是價格1">  
  
<input name="dto.productList[2].name" value="我是名稱2">  
<input name="dto.productList[2].price" value="我是價格2">  
  
<input name="dto.productList[4].name" value="我是名稱3">  
<input name="dto.productList[4].price" value="我是價格3"> 

OGNL 會自動把dto.productList[1]、dto.productList[3] 的object封裝成null。因此,我們在操作dto.productList 前,優先把 productList 中null去除掉,防止 null 引起的空指針異常。

 最后,歡迎各位拍磚。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM