Java Collection Search getSingleItem(Collection values)

Here you can find the source of getSingleItem(Collection values)

Description

get Single Item

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
    static <T> T getSingleItem(Collection<T> values) 

Method Source Code

//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];
        }
    }
}

Related

  1. getSingle(Collection configs)
  2. getSingleElement(Collection collection)
  3. getSingleElement(Collection collection)
  4. getSingleValue(Iterable collection)
  5. hasElements(Collection c)
  6. hasElements(Collection c)
  7. hasElements(Collection collection)