Java Collection First first(Collection items)

Here you can find the source of first(Collection items)

Description

Null safe access to the first object in a collection

License

Apache License

Declaration

public static Object first(Collection items) 

Method Source Code


//package com.java2s;
/*//from  ww  w . java2 s .  c o  m
 * Copyright 2010 Vamonos Software
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import java.util.Collection;

public class Main {
    /**
     * Null safe access to the first object in a collection
     */
    public static Object first(Collection items) {
        if (items != null && items.size() > 0) {
            return items.iterator().next();
        }
        return null;
    }

    /**
     * Null safe summation of the sizes of all of the given collections
     */
    public static int size(Collection... collections) {
        int size = 0;
        if (collections != null) {
            for (Collection collection : collections) {
                if (collection != null)
                    size += collection.size();
            }
        }
        return size;
    }
}

Related

  1. first(Collection collection)
  2. first(Collection collection)
  3. first(Collection collection, E alternative)
  4. first(Collection collection)
  5. first(Collection collection)
  6. first(Collection values)