コード例 #1
0
  // get min element
  private static void testGetMin() {
    Collection<String> collection = Lists.newArrayList("5", "1", "3", "8", "4");
    OrderedIterable<String> orderedIterable = FastList.newListWith("5", "1", "3", "8", "4");
    Iterable<String> iterable = collection;

    // get min element
    String jdk = Collections.min(collection); // using JDK
    String gs = orderedIterable.min(); // using GS
    String guava = Ordering.natural().min(iterable); // using guava
    System.out.println("min = " + jdk + ":" + guava + ":" + gs); // print min = 1:1:1
  }
コード例 #2
0
  // get a element, if collection has only one element
  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
  }
コード例 #3
0
  // get last element
  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
  }