取出list中的null

package com.boventech.learning.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 = 1L;

	@Override
	public Object get(int index) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return 1;
	}
	
	public boolean contains(Object obj) {  
        return null == obj;  
    }  
	
	private Object readResolve() {  
        return null;  
    } 
}


package com.boventech.learning.util;

import java.util.Collection;  
import java.util.List;  
  
public class YHDCollectionUtils {  
      
     @SuppressWarnings("rawtypes")
	public static final Collection NULL_COLLECTION = new NullCollection();  
          
    @SuppressWarnings("unchecked")
	public static final <T> Collection<T> nullCollection() {  
        return (List<T>) NULL_COLLECTION;  
    }  
}  

package com.boventech.learning.test;

import java.util.ArrayList;
import java.util.List;

import com.boventech.learning.util.YHDCollectionUtils;

public class NullTest {

	/**
	 * @param args
	 */
	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);  
	} 

}


你可能感兴趣的:(取出list中的null)