Here you can find the source of getSingleItem(Collection
@SuppressWarnings("unchecked") static <T> T getSingleItem(Collection<T> values)
//package com.java2s; /******************************************************************************* * Copyright (c) 2012 NumberFour AG// w w w . j ava2 s . c om * * 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: * NumberFour AG - initial API and Implementation (Alex Panchenko) *******************************************************************************/ import java.util.Collection; import java.util.List; public class Main { @SuppressWarnings("unchecked") static <T> T getSingleItem(Collection<T> values) { assert values.size() == 1; if (values instanceof List) { return ((List<T>) values).get(0); } else { return (T) values.toArray()[0]; } } }