From fa77261c42f8f2acded208ae9372d01154b3de94 Mon Sep 17 00:00:00 2001 From: Siben Nayak Date: Sun, 20 May 2018 01:07:00 +0530 Subject: [PATCH] [BAEL-1775] Getting size of Iterable in Java --- .../baeldung/java/iterable/IterableSize.java | 65 +++++++++++++++++++ .../java/iterable/IterableSizeTest.java | 59 +++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 core-java-collections/src/main/java/com/baeldung/java/iterable/IterableSize.java create mode 100644 core-java-collections/src/test/java/com/baeldung/java/iterable/IterableSizeTest.java diff --git a/core-java-collections/src/main/java/com/baeldung/java/iterable/IterableSize.java b/core-java-collections/src/main/java/com/baeldung/java/iterable/IterableSize.java new file mode 100644 index 0000000000..03864f16f2 --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/java/iterable/IterableSize.java @@ -0,0 +1,65 @@ +package com.baeldung.java.iterable; + +import java.util.Collection; +import java.util.stream.StreamSupport; + +import org.apache.commons.collections4.IterableUtils; + +import com.google.common.collect.Iterables; + +/** + * Provides methods for getting the size of an {@link Iterable} object. + */ +public class IterableSize { + + /** + * Get the size of {@code Iterable} using Java 7. + * + * @param data the iterable + * @return the size of the iterable + */ + public static int sizeUsingJava7(final Iterable data) { + + if (data instanceof Collection) { + return ((Collection) data).size(); + } + int counter = 0; + for (final Object i : data) { + counter++; + } + return counter; + } + + /** + * Get the size of {@code Iterable} using Java 8. + * + * @param data the iterable + * @return the size of the iterable + */ + public static long sizeUsingJava8(final Iterable data) { + + return StreamSupport.stream(data.spliterator(), false).count(); + } + + /** + * Get the size of {@code Iterable} using Apache Collections. + * + * @param data the iterable + * @return the size of the iterable + */ + public static int sizeUsingApacheCollections(final Iterable data) { + + return IterableUtils.size(data); + } + + /** + * Get the size of {@code Iterable} using Google Guava. + * + * @param data the iterable + * @return the size of the iterable + */ + public static int sizeUsingGoogleGuava(final Iterable data) { + + return Iterables.size(data); + } +} diff --git a/core-java-collections/src/test/java/com/baeldung/java/iterable/IterableSizeTest.java b/core-java-collections/src/test/java/com/baeldung/java/iterable/IterableSizeTest.java new file mode 100644 index 0000000000..d73e3a0eb5 --- /dev/null +++ b/core-java-collections/src/test/java/com/baeldung/java/iterable/IterableSizeTest.java @@ -0,0 +1,59 @@ +package com.baeldung.java.iterable; + +import static org.junit.Assert.assertEquals; + +import java.sql.SQLException; +import java.util.List; + +import org.junit.jupiter.api.Test; + +import com.google.common.collect.Lists; + +class IterableSizeTest { + + private final List list = Lists.newArrayList("Apple", "Orange", "Banana"); + + private Iterable data; + + @Test + void whenUsingJava7_iterableOfCollectionType_thenCorrectSize() { + + final int size = IterableSize.sizeUsingJava7(list); + + assertEquals(3, size); + } + + @Test + void whenUsingJava7_iterableNotOfCollectionType_thenCorrect() { + + final SQLException exception = new SQLException(); + exception.setNextException(new SQLException()); + final int size = IterableSize.sizeUsingJava7(exception); + + assertEquals(2, size); + } + + @Test + void whenUsingJava8_thenCorrect() { + + final long size = IterableSize.sizeUsingJava8(list); + + assertEquals(3, size); + } + + @Test + void whenUsingApacheCollections_thenCorrect() { + + final int size = IterableSize.sizeUsingApacheCollections(list); + + assertEquals(3, size); + } + + @Test + void whenUsingGoogleGuava_thenCorrect() { + + final int size = IterableSize.sizeUsingGoogleGuava(list); + + assertEquals(3, size); + } +} \ No newline at end of file