Java Collection Search getSingleElement(Collection collection)

Here you can find the source of getSingleElement(Collection collection)

Description

Returns the single element contained by the input collection

License

Open Source License

Parameter

Parameter Description
T a parameter
collection a parameter

Exception

Parameter Description
IndexOutOfBoundsException if the collections contains either zerso or >1 elements

Declaration

public static <T> T getSingleElement(Collection<T> collection) 

Method Source Code


//package com.java2s;
import java.util.ArrayList;

import java.util.Collection;
import java.util.Collections;

import java.util.Iterator;

import java.util.List;

public class Main {
    /**//from   www  .  j  a va  2s .c om
     * Returns the single element contained by the input collection
     * 
     * @throws IndexOutOfBoundsException
     *             if the collections contains either zerso or >1 elements
     * @param <T>
     * @param collection
     * @return
     */
    public static <T> T getSingleElement(Collection<T> collection) {
        if (collection.size() != 1)
            throw new IndexOutOfBoundsException(
                    String.format("Expected collection to contain a single element, but observed %d elements: %s",
                            collection.size(), collection.toString()));
        return collection.iterator().next();
    }

    /**
     * Returns the single element contained by the input Iterable<T>
     * 
     * @throws IndexOutOfBoundsException
     *             if the iterable contains either zerso or >1 elements
     * @param <T>
     * @param collection
     * @return
     */
    public static <T> T getSingleElement(Iterable<T> iterable) {
        List<T> list = createList(iterable.iterator());
        return getSingleElement(list);
    }

    /**
     * Returns a String representation of the input collection by calling each
     * element's toString() method.
     * 
     * @param <T>
     * @param collection
     * @return
     */
    public static <T> Collection<String> toString(Collection<T> collection) {
        Collection<String> strings = new ArrayList<String>();
        for (T item : collection)
            strings.add(item.toString());
        return strings;
    }

    /**
     * Returns a List implementation for any input collection
     * 
     * @param <T>
     * @param collection
     * @return
     */
    public static <T> List<T> createList(Collection<T> collection) {
        return Collections.list(Collections.enumeration(collection));
    }

    /**
     * Returns an empty list
     * 
     * @param <T>
     * @return
     */
    public static <T> List<T> createList() {
        return Collections.emptyList();
    }

    /**
     * Returns a List<T> containing the input arguments.
     * 
     * @param <T>
     * @param item1
     * @return
     */
    public static <T> List<T> createList(T item1) {
        List<T> list = new ArrayList<T>();
        list.add(item1);
        return list;
    }

    /**
     * Returns a List<T> containing the input arguments.
     * 
     * @param <T>
     * @param item1
     * @param item2
     * @return
     */
    public static <T> List<T> createList(T item1, T item2) {
        List<T> list = new ArrayList<T>();
        list.add(item1);
        list.add(item2);
        return list;
    }

    /**
     * Returns a List<T> containing the input arguments.
     * 
     * @param <T>
     * @param item1
     * @param item2
     * @param item3
     * @return
     */
    public static <T> List<T> createList(T item1, T item2, T item3) {
        List<T> list = new ArrayList<T>();
        list.add(item1);
        list.add(item2);
        list.add(item3);
        return list;
    }

    /**
     * Returns a List<T> containing the input arguments.
     * 
     * @param <T>
     * @param item1
     * @param item2
     * @param item3
     * @param item4
     * @return
     */
    public static <T> List<T> createList(T item1, T item2, T item3, T item4) {
        List<T> list = new ArrayList<T>();
        list.add(item1);
        list.add(item2);
        list.add(item3);
        list.add(item4);
        return list;
    }

    /**
     * Returns a List<T> containing the input arguments.
     * 
     * @param <T>
     * @param item1
     * @param item2
     * @param item3
     * @param item4
     * @param item5
     * @return
     */
    public static <T> List<T> createList(T item1, T item2, T item3, T item4, T item5) {
        List<T> list = new ArrayList<T>();
        list.add(item1);
        list.add(item2);
        list.add(item3);
        list.add(item4);
        list.add(item5);
        return list;
    }

    /**
     * Returns a List<T> containing the input arguments.
     * 
     * @param <T>
     * @param item1
     * @param item2
     * @param item3
     * @param item4
     * @param item5
     * @param item6
     * @return
     */
    public static <T> List<T> createList(T item1, T item2, T item3, T item4, T item5, T item6) {
        List<T> list = new ArrayList<T>();
        list.add(item1);
        list.add(item2);
        list.add(item3);
        list.add(item4);
        list.add(item5);
        list.add(item6);
        return list;
    }

    /**
     * Returns a List<T> containing the input arguments.
     * 
     * @param <T>
     * @param item1
     * @param item2
     * @param item3
     * @param item4
     * @param item5
     * @param item6
     * @param item7
     * @return
     */
    public static <T> List<T> createList(T item1, T item2, T item3, T item4, T item5, T item6, T item7) {
        List<T> list = new ArrayList<T>();
        list.add(item1);
        list.add(item2);
        list.add(item3);
        list.add(item4);
        list.add(item5);
        list.add(item6);
        list.add(item7);
        return list;
    }

    /**
     * Returns a List<T> containing the input arguments.
     * 
     * @param <T>
     * @param item1
     * @param item2
     * @param item3
     * @param item4
     * @param item5
     * @param item6
     * @param item7
     * @param item8
     * @return
     */
    public static <T> List<T> createList(T item1, T item2, T item3, T item4, T item5, T item6, T item7, T item8) {
        List<T> list = new ArrayList<T>();
        list.add(item1);
        list.add(item2);
        list.add(item3);
        list.add(item4);
        list.add(item5);
        list.add(item6);
        list.add(item7);
        list.add(item8);
        return list;
    }

    /**
     * Returns a List<T> containing the input arguments.
     * 
     * @param <T>
     * @param item1
     * @param item2
     * @param item3
     * @param item4
     * @param item5
     * @param item6
     * @param item7
     * @param item8
     * @param item9
     * @return
     */
    public static <T> List<T> createList(T item1, T item2, T item3, T item4, T item5, T item6, T item7, T item8,
            T item9) {
        List<T> list = new ArrayList<T>();
        list.add(item1);
        list.add(item2);
        list.add(item3);
        list.add(item4);
        list.add(item5);
        list.add(item6);
        list.add(item7);
        list.add(item8);
        list.add(item9);
        return list;
    }

    /**
     * Returns a List<T> containing the input arguments.
     * 
     * @param <T>
     * @param item1
     * @param item2
     * @param item3
     * @param item4
     * @param item5
     * @param item6
     * @param item7
     * @param item8
     * @param item9
     * @param item10
     * @param otherItems
     * @return
     */
    public static <T> List<T> createList(T item1, T item2, T item3, T item4, T item5, T item6, T item7, T item8,
            T item9, T item10, T... otherItems) {
        List<T> list = new ArrayList<T>();
        list.add(item1);
        list.add(item2);
        list.add(item3);
        list.add(item4);
        list.add(item5);
        list.add(item6);
        list.add(item7);
        list.add(item8);
        list.add(item9);
        list.add(item10);
        for (T item : otherItems)
            list.add(item);
        return list;
    }

    /**
     * Returns the contents of an iterator as a list
     * 
     * @param <T>
     * @param iter
     * @return
     */
    public static <T> List<T> createList(Iterator<T> iter) {
        List<T> list = new ArrayList<T>();
        while (iter.hasNext()) {
            list.add(iter.next());
        }
        return list;
    }
}

Related

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