[BAEL-1775] Getting size of Iterable in Java
This commit is contained in:
parent
a6c5f89093
commit
fa77261c42
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<String> 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue