Java Utililty Methods Collection Null

List of utility methods to do Collection Null

Description

The list of methods to do Collection Null are organized into topic(s).

Method

CollectionnullSafe(Collection in)
null Safe
if (in == null) {
    return (Collection<T>) EMPTY_LIST;
return in;
CollectionnullSafeCollection(Collection source)
null Safe Collection
return source == null ? Collections.emptyList() : source;
intnullSafeSize(Collection collection)
size
if (collection == null) {
    return 0;
return collection.size();
intnullSafeSize(final Collection collection)
null Safe Size
return nullSafeSize(collection, 0);
voidpadWithNulls(final Collection s1, final Collection s2)
pad With Nulls
if (s1.size() != s2.size()) {
    while (s1.size() < s2.size()) {
        s1.add(null);
    while (s1.size() > s2.size()) {
        s2.add(null);
booleanremoveAllIfNotNull(final Collection collection, final Collection c)
remove All If Not Null
return (collection != null) && (c != null) && collection.removeAll(c);
booleanremoveNull(Collection collection)
remove Null
return collection.removeAll(Collections.singleton(null));
voidremoveNull(final Collection c)
remove Null
synchronized (c) {
    for (final Iterator<?> it = c.iterator(); it.hasNext();) {
        final Object object = it.next();
        if (object == null) {
            it.remove();
CollectionremoveNullElement(Collection collection)
remove Null Element
Collection<T> result = new ArrayList<T>();
for (T t : collection) {
    if (t != null) {
        result.add(t);
return result;
booleanremoveNulls(Collection p_collection)
Removes null objects from the Collection.
if (p_collection == null) {
    return false;
boolean removed = false;
Iterator it = p_collection.iterator();
while (it.hasNext()) {
    Object o = it.next();
    if (o == null) {
...