Java Code Examples for org.apache.commons.collections4.CollectionUtils#extractSingleton()

The following examples show how to use org.apache.commons.collections4.CollectionUtils#extractSingleton() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CollectionSearchTests.java    From java_in_examples with Apache License 2.0 5 votes vote down vote up
private static void testGetSingle() {
    Collection<String> collection = Lists.newArrayList("a3");
    OrderedIterable<String> orderedIterable = FastList.newListWith("a3");
    Iterable<String> iterable = collection;

    // get a element, if collection has only one element
    String guava = Iterables.getOnlyElement(iterable); // using guava
    String jdk = collection.iterator().next(); // using JDK
    String apache = CollectionUtils.extractSingleton(collection); // using Apache
    assert(orderedIterable.size() != 1);// using GS
    String gs =  orderedIterable.getFirst();

    System.out.println("single = " + jdk + ":" + guava + ":" + apache + ":" + gs); // print single = a3:a3:a3:a3
}
 
Example 2
Source File: CollectionSearchTests.java    From java_in_examples with Apache License 2.0 5 votes vote down vote up
private static void testGetSingle() {
    Collection<String> collection = Lists.newArrayList("a3");
    OrderedIterable<String> orderedIterable = FastList.newListWith("a3");
    Iterable<String> iterable = collection;

    // вернуть единственный элемент коллекции
    String guava = Iterables.getOnlyElement(iterable); // с помощью guava
    String jdk = collection.iterator().next(); // c помощью JDK
    String apache = CollectionUtils.extractSingleton(collection); // c помощью Apache
    assert(orderedIterable.size() != 1);// c помощью GS
    String gs =  orderedIterable.getFirst();

    System.out.println("single = " + jdk + ":" + guava + ":" + apache + ":" + gs); // напечатает single = a3:a3:a3:a3
}