Skip to content

Commit 5b48942

Browse files
rozagerardoCalamarBicefalo
authored andcommitted
* added examples of filtering collections using Streams, Apache CollectionUtils, Guava and Eclipse Collections
1 parent 4ee81a2 commit 5b48942

6 files changed

Lines changed: 176 additions & 0 deletions

File tree

‎core-java-collections/pom.xml‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,32 @@
3636
<artifactId>commons-lang3</artifactId>
3737
<version>${commons-lang3.version}</version>
3838
</dependency>
39+
<dependency>
40+
<groupId>org.eclipse.collections</groupId>
41+
<artifactId>eclipse-collections-api</artifactId>
42+
<version>9.2.0</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.eclipse.collections</groupId>
46+
<artifactId>eclipse-collections</artifactId>
47+
<version>9.2.0</version>
48+
</dependency>
3949
<dependency>
4050
<groupId>org.assertj</groupId>
4151
<artifactId>assertj-core</artifactId>
4252
<version>${assertj.version}</version>
4353
<scope>test</scope>
4454
</dependency>
55+
<dependency>
56+
<groupId>org.junit.platform</groupId>
57+
<artifactId>junit-platform-runner</artifactId>
58+
<version>${junit.platform.version}</version>
59+
<scope>test</scope>
60+
</dependency>
4561
</dependencies>
4662

4763
<properties>
64+
<junit.platform.version>1.2.0</junit.platform.version>
4865
<commons-lang3.version>3.5</commons-lang3.version>
4966
<commons-collections4.version>4.1</commons-collections4.version>
5067
<collections-generic.version>4.01</collections-generic.version>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.java.filtering;
2+
3+
import java.util.Collection;
4+
5+
import org.apache.commons.collections4.CollectionUtils;
6+
import org.apache.commons.collections4.Predicate;
7+
8+
public class CollectionUtilsCollectionFilter {
9+
10+
static public Collection<Integer> findEvenNumbers(Collection<Integer> baseCollection) {
11+
Predicate<Integer> apacheEventNumberPredicate = new Predicate<Integer>() {
12+
13+
@Override
14+
public boolean evaluate(Integer object) {
15+
return object % 2 == 0;
16+
}
17+
};
18+
19+
CollectionUtils.filter(baseCollection, apacheEventNumberPredicate);
20+
return baseCollection;
21+
}
22+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.java.filtering;
2+
3+
import java.util.Collection;
4+
5+
import org.eclipse.collections.api.block.predicate.Predicate;
6+
import org.eclipse.collections.impl.factory.Lists;
7+
import org.eclipse.collections.impl.utility.Iterate;
8+
9+
public class EclipseCollectionsCollectionFilter {
10+
11+
static public Collection<Integer> findEvenNumbers(Collection<Integer> baseCollection) {
12+
Predicate<Integer> eclipsePredicate = item -> item % 2 == 0;
13+
Collection<Integer> filteredList = Lists.mutable.ofAll(baseCollection)
14+
.select(eclipsePredicate);
15+
16+
return filteredList;
17+
}
18+
19+
static public Collection<Integer> findEvenNumbersUsingIterate(Collection<Integer> baseCollection) {
20+
Predicate<Integer> eclipsePredicate = new Predicate<Integer>() {
21+
private static final long serialVersionUID = 1L;
22+
23+
@Override
24+
public boolean accept(Integer arg0) {
25+
return arg0 % 2 == 0;
26+
}
27+
};
28+
Collection<Integer> filteredList = Iterate.select(baseCollection, eclipsePredicate);
29+
30+
return filteredList;
31+
}
32+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.java.filtering;
2+
3+
import java.util.Collection;
4+
5+
import com.google.common.base.Predicate;
6+
import com.google.common.collect.Collections2;
7+
8+
public class GuavaCollectionFilter {
9+
10+
static public Collection<Integer> findEvenNumbers(Collection<Integer> baseCollection) {
11+
Predicate<Integer> guavaPredicate = new Predicate<Integer>() {
12+
13+
@Override
14+
public boolean apply(Integer input) {
15+
return input % 2 == 0;
16+
}
17+
};
18+
Collection<Integer> filteredCollection = Collections2.filter(baseCollection, guavaPredicate);
19+
return filteredCollection;
20+
}
21+
22+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.java.filtering;
2+
3+
import java.util.Collection;
4+
import java.util.function.Predicate;
5+
import java.util.stream.Collectors;
6+
7+
public class StreamsCollectionFilter {
8+
9+
public static <T> Collection<T> filterCollectionHelperMethod(Collection<T> baseCollection, Predicate<T> predicate) {
10+
return baseCollection.stream()
11+
.filter(predicate)
12+
.collect(Collectors.toList());
13+
}
14+
15+
static public Collection<Integer> findEvenNumbersUsingHelperMethod(Collection<Integer> baseCollection) {
16+
return filterCollectionHelperMethod(baseCollection, item -> item % 2 == 0);
17+
}
18+
19+
static public Collection<Integer> findEvenNumbersUsingLambda(Collection<Integer> baseCollection) {
20+
return baseCollection.stream()
21+
.filter(item -> item % 2 == 0)
22+
.collect(Collectors.toList());
23+
}
24+
25+
static public Collection<Integer> findEvenNumbersUsingPredicate(Collection<Integer> baseCollection) {
26+
Predicate<Integer> evenNumberPredicate = new Predicate<Integer>() {
27+
@Override
28+
public boolean test(Integer i) {
29+
return i % 2 == 0;
30+
}
31+
};
32+
33+
return baseCollection.stream()
34+
.filter(evenNumberPredicate)
35+
.collect(Collectors.toList());
36+
}
37+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.java.filtering;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.Collection;
8+
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.platform.runner.JUnitPlatform;
11+
import org.junit.runner.RunWith;
12+
13+
@RunWith(JUnitPlatform.class)
14+
public class CollectionFiltersUnitTest {
15+
16+
private static final Collection<Integer> BASE_INTEGER_COLLECTION = Arrays.asList(9, 14, 2, 7, 1, 5, 8);
17+
private static final Collection<Integer> EXPECTED_EVEN_FILTERED_COLLECTION = Arrays.asList(14, 2, 8);
18+
19+
@Test
20+
public void givenAStringCollection_whenFilteringFourLetterWords_thenObtainTheFilteredCollection() {
21+
final Collection<String> baseStrings = Arrays.asList("java", "baeldung", "type", "example", "other");
22+
23+
Collection<String> filtered = StreamsCollectionFilter.filterCollectionHelperMethod(baseStrings, item -> item.length() == 4);
24+
25+
assertThat(filtered).containsExactlyInAnyOrder("java", "type");
26+
}
27+
28+
@Test
29+
public void givenAnIntegerCollection_whenFilteringEvenValues_thenObtainTheFilteredCollectionForAllCases() {
30+
Collection<Integer> filteredWithStreams1 = StreamsCollectionFilter.findEvenNumbersUsingLambda(BASE_INTEGER_COLLECTION);
31+
Collection<Integer> filteredWithStreams2 = StreamsCollectionFilter.findEvenNumbersUsingPredicate(BASE_INTEGER_COLLECTION);
32+
Collection<Integer> filteredWithCollectionUtils = CollectionUtilsCollectionFilter.findEvenNumbers(new ArrayList<>(BASE_INTEGER_COLLECTION));
33+
Collection<Integer> filteredWithEclipseCollections = EclipseCollectionsCollectionFilter.findEvenNumbers(BASE_INTEGER_COLLECTION);
34+
Collection<Integer> filteredWithEclipseCollectionsUsingIterate = EclipseCollectionsCollectionFilter.findEvenNumbersUsingIterate(BASE_INTEGER_COLLECTION);
35+
Collection<Integer> filteredWithGuava = GuavaCollectionFilter.findEvenNumbers(BASE_INTEGER_COLLECTION);
36+
37+
assertThat(filteredWithStreams1).hasSameElementsAs(filteredWithStreams2)
38+
.hasSameElementsAs(filteredWithCollectionUtils)
39+
.hasSameElementsAs(filteredWithEclipseCollections)
40+
.hasSameElementsAs(filteredWithEclipseCollectionsUsingIterate)
41+
.hasSameElementsAs(filteredWithEclipseCollectionsUsingIterate)
42+
.hasSameElementsAs(filteredWithGuava)
43+
.hasSameElementsAs(EXPECTED_EVEN_FILTERED_COLLECTION);
44+
}
45+
46+
}

0 commit comments

Comments
 (0)