Skip to content

Commit 7dc605f

Browse files
authored
Merge pull request eugenp#4308 from theawesomenayak/BAEL-1775
[BAEL-1775] Getting size of Iterable in Java
2 parents c2452b6 + fa77261 commit 7dc605f

2 files changed

Lines changed: 124 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.baeldung.java.iterable;
2+
3+
import java.util.Collection;
4+
import java.util.stream.StreamSupport;
5+
6+
import org.apache.commons.collections4.IterableUtils;
7+
8+
import com.google.common.collect.Iterables;
9+
10+
/**
11+
* Provides methods for getting the size of an {@link Iterable} object.
12+
*/
13+
public class IterableSize {
14+
15+
/**
16+
* Get the size of {@code Iterable} using Java 7.
17+
*
18+
* @param data the iterable
19+
* @return the size of the iterable
20+
*/
21+
public static int sizeUsingJava7(final Iterable data) {
22+
23+
if (data instanceof Collection) {
24+
return ((Collection<?>) data).size();
25+
}
26+
int counter = 0;
27+
for (final Object i : data) {
28+
counter++;
29+
}
30+
return counter;
31+
}
32+
33+
/**
34+
* Get the size of {@code Iterable} using Java 8.
35+
*
36+
* @param data the iterable
37+
* @return the size of the iterable
38+
*/
39+
public static long sizeUsingJava8(final Iterable data) {
40+
41+
return StreamSupport.stream(data.spliterator(), false).count();
42+
}
43+
44+
/**
45+
* Get the size of {@code Iterable} using Apache Collections.
46+
*
47+
* @param data the iterable
48+
* @return the size of the iterable
49+
*/
50+
public static int sizeUsingApacheCollections(final Iterable data) {
51+
52+
return IterableUtils.size(data);
53+
}
54+
55+
/**
56+
* Get the size of {@code Iterable} using Google Guava.
57+
*
58+
* @param data the iterable
59+
* @return the size of the iterable
60+
*/
61+
public static int sizeUsingGoogleGuava(final Iterable data) {
62+
63+
return Iterables.size(data);
64+
}
65+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.java.iterable;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.sql.SQLException;
6+
import java.util.List;
7+
8+
import org.junit.jupiter.api.Test;
9+
10+
import com.google.common.collect.Lists;
11+
12+
class IterableSizeTest {
13+
14+
private final List<String> list = Lists.newArrayList("Apple", "Orange", "Banana");
15+
16+
private Iterable data;
17+
18+
@Test
19+
void whenUsingJava7_iterableOfCollectionType_thenCorrectSize() {
20+
21+
final int size = IterableSize.sizeUsingJava7(list);
22+
23+
assertEquals(3, size);
24+
}
25+
26+
@Test
27+
void whenUsingJava7_iterableNotOfCollectionType_thenCorrect() {
28+
29+
final SQLException exception = new SQLException();
30+
exception.setNextException(new SQLException());
31+
final int size = IterableSize.sizeUsingJava7(exception);
32+
33+
assertEquals(2, size);
34+
}
35+
36+
@Test
37+
void whenUsingJava8_thenCorrect() {
38+
39+
final long size = IterableSize.sizeUsingJava8(list);
40+
41+
assertEquals(3, size);
42+
}
43+
44+
@Test
45+
void whenUsingApacheCollections_thenCorrect() {
46+
47+
final int size = IterableSize.sizeUsingApacheCollections(list);
48+
49+
assertEquals(3, size);
50+
}
51+
52+
@Test
53+
void whenUsingGoogleGuava_thenCorrect() {
54+
55+
final int size = IterableSize.sizeUsingGoogleGuava(list);
56+
57+
assertEquals(3, size);
58+
}
59+
}

0 commit comments

Comments
 (0)