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

The following examples show how to use org.apache.commons.collections4.CollectionUtils#get() . 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 6 votes vote down vote up
private static void testGetLast() {
    Collection<String> collection = Lists.newArrayList("a1", "a2", "a3", "a8");
    OrderedIterable<String> orderedIterable = FastList.newListWith("a1", "a2", "a3", "a8");
    Iterable<String> iterable = collection;

    // get last element
    Iterator<String> iterator = collection.iterator(); // using JDK
    String jdk = "1";
    while(iterator.hasNext()) {
        jdk = iterator.next();
    }
    String guava = Iterables.getLast(iterable, "1"); // using guava
    String apache = CollectionUtils.get(collection, collection.size()-1);  // using Apache
    String gs = orderedIterable.getLast(); // using GS
    String stream = collection.stream().skip(collection.size()-1).findFirst().orElse("1"); // using Stream API
    System.out.println("last = " + jdk + ":" + guava + ":" + apache + ":" + gs + ":" + stream); // print last = a8:a8:a8:a8:a8
}
 
Example 2
Source File: CollectionSearchTests.java    From java_in_examples with Apache License 2.0 6 votes vote down vote up
private static void testGetLast() {
    Collection<String> collection = Lists.newArrayList("a1", "a2", "a3", "a8");
    OrderedIterable<String> orderedIterable = FastList.newListWith("a1", "a2", "a3", "a8");
    Iterable<String> iterable = collection;

    // вернуть последней элемент коллекции
    Iterator<String> iterator = collection.iterator(); // c помощью JDK
    String jdk = "1";
    while(iterator.hasNext()) {
        jdk = iterator.next();
    }
    String guava = Iterables.getLast(iterable, "1"); // с помощью guava
    String apache = CollectionUtils.get(collection, collection.size()-1);  // c помощью Apache
    String gs = orderedIterable.getLast(); // c помощью GS
    String stream = collection.stream().skip(collection.size()-1).findFirst().orElse("1"); // c помощью Stream API
    System.out.println("last = " + jdk + ":" + guava + ":" + apache + ":" + gs + ":" + stream); // напечатает last = a8:a8:a8:a8:a8
}
 
Example 3
Source File: CollectionSearchTests.java    From java_in_examples with Apache License 2.0 5 votes vote down vote up
private static void testGetFirst() {
    Collection<String> collection = Lists.newArrayList("a1", "a2", "a3", "a1");
    OrderedIterable<String> orderedIterable = FastList.newListWith("a1", "a2", "a3", "a1");
    Iterable<String> iterable = collection;

    // get first element
    Iterator<String> iterator = collection.iterator(); // using JDK
    String jdk = iterator.hasNext() ? iterator.next(): "1";
    String guava = Iterables.getFirst(iterable, "1"); // using guava
    String apache = CollectionUtils.get(iterable, 0);  // using Apache
    String gs = orderedIterable.getFirst(); // using GS
    String stream = collection.stream().findFirst().orElse("1"); // using Stream API
    System.out.println("first = " + jdk + ":" + guava + ":" + apache + ":" + gs + ":" + stream); // print first = a1:a1:a1:a1:a1
}
 
Example 4
Source File: CollectionSearchTests.java    From java_in_examples with Apache License 2.0 5 votes vote down vote up
private static void testGetByIndex() {
    Collection<String> collection = Lists.newArrayList("a1", "a2", "a3", "a1");
    MutableCollection<String> orderedIterable = FastList.newListWith("a1", "a2", "a3", "a1");
    Iterable<String> iterable = collection;

    // get third element
    String jdk = collection.stream().skip(2).findFirst().get(); // using JDK
    String guava = Iterables.get(iterable, 2); // using guava
    String apache = CollectionUtils.get(iterable, 2);  // using Apache
    System.out.println("third = " + jdk + ":" + guava + ":" + apache); // print third = a3:a3:a3
}
 
Example 5
Source File: CollectionSearchTests.java    From java_in_examples with Apache License 2.0 5 votes vote down vote up
private static void testGetFirst() {
    Collection<String> collection = Lists.newArrayList("a1", "a2", "a3", "a1");
    OrderedIterable<String> orderedIterable = FastList.newListWith("a1", "a2", "a3", "a1");
    Iterable<String> iterable = collection;

    // вернуть первый элемент коллекции
    Iterator<String> iterator = collection.iterator(); // c помощью JDK
    String jdk = iterator.hasNext() ? iterator.next(): "1";
    String guava = Iterables.getFirst(iterable, "1"); // с помощью guava
    String apache = CollectionUtils.get(iterable, 0);  // c помощью Apache
    String gs = orderedIterable.getFirst(); // c помощью GS
    String stream = collection.stream().findFirst().orElse("1"); // c помощью Stream API
    System.out.println("first = " + jdk + ":" + guava + ":" + apache + ":" + gs + ":" + stream); // напечатает first = a1:a1:a1:a1:a1
}
 
Example 6
Source File: CollectionSearchTests.java    From java_in_examples with Apache License 2.0 5 votes vote down vote up
private static void testGetByIndex() {
    Collection<String> collection = Lists.newArrayList("a1", "a2", "a3", "a1");
    MutableCollection<String> orderedIterable = FastList.newListWith("a1", "a2", "a3", "a1");
    Iterable<String> iterable = collection;

    // вернуть третий элемент коллекции по порядку
    String jdk = collection.stream().skip(2).findFirst().get(); // c помощью JDK
    String guava = Iterables.get(iterable, 2); // с помощью guava
    String apache = CollectionUtils.get(iterable, 2);  // c помощью Apache
    System.out.println("third = " + jdk + ":" + guava + ":" + apache); // напечатает third = a3:a3:a3
}