Java Collection First first(Collection self)

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

Description

Implementation of the OCL
  • OrderedSet::first() : T
  • Sequence::first() : T
operations.

License

Open Source License

Parameter

Parameter Description
self the source collection

Return

the first object of the source collection

Declaration

public static <E> E first(Collection<E> self) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006, 2012, 2011 IBM Corporation, Zeligsoft Inc., and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from w w w.ja  v  a  2 s  .com*/
 *   IBM - Initial API and implementation
 *   Zeligsoft - Bugs 244946, 248869
 *   Axel Uhl (SAP AG) - Bug 342644
 *******************************************************************************/

import java.util.Collection;

public class Main {
    /**
     * Implementation of the OCL
     * <ul>
     * <li><tt>OrderedSet::first() : T</tt></li>
     * <li><tt>Sequence::first() : T</tt></li>
     * </ul>
     * operations.
     * 
     * @param self the source collection
     * @return the first object of the source collection
     */
    public static <E> E first(Collection<E> self) {
        if (self.isEmpty()) {
            return null; // undefined
        }
        return self.iterator().next();
    }

    /**
     * Implementation of the OCL
     * <tt>Collection::isEmpty() : Boolean</tt>
     * operation.
     * 
     * @param self the source collection
     * @return whether the collection does not have any elements
     */
    public static boolean isEmpty(Collection<?> self) {
        return self.isEmpty();
    }
}

Related

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