Here you can find the source of getFirstItem(Collection c)
Parameter | Description |
---|---|
c | The Collection. |
Parameter | Description |
---|
public static Object getFirstItem(Collection c)
//package com.java2s; /* $Id$// w ww .ja va2 s . c o m ******************************************************************************* * Copyright (c) 2009 Contributors - see below * 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: * Bob Tarling ******************************************************************************* */ import java.util.Collection; import java.util.List; public class Main { /** * Return the first item from a collection using the most efficient * method possible. * * @param c The Collection. * @return the first element of a Collection. * @throws java.util.NoSuchElementException if the collection is empty. */ public static Object getFirstItem(Collection c) { if (c instanceof List) { return ((List) c).get(0); } return c.iterator().next(); } }