* added examples of filtering collections using Streams, Apache CollectionUtils, Guava and Eclipse Collections
This commit is contained in:
committed by
José Carlos Valero Sánchez
parent
4ee81a2fd9
commit
5b489423d8
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.java.filtering;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.collections4.Predicate;
|
||||
|
||||
public class CollectionUtilsCollectionFilter {
|
||||
|
||||
static public Collection<Integer> findEvenNumbers(Collection<Integer> baseCollection) {
|
||||
Predicate<Integer> apacheEventNumberPredicate = new Predicate<Integer>() {
|
||||
|
||||
@Override
|
||||
public boolean evaluate(Integer object) {
|
||||
return object % 2 == 0;
|
||||
}
|
||||
};
|
||||
|
||||
CollectionUtils.filter(baseCollection, apacheEventNumberPredicate);
|
||||
return baseCollection;
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.java.filtering;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.eclipse.collections.api.block.predicate.Predicate;
|
||||
import org.eclipse.collections.impl.factory.Lists;
|
||||
import org.eclipse.collections.impl.utility.Iterate;
|
||||
|
||||
public class EclipseCollectionsCollectionFilter {
|
||||
|
||||
static public Collection<Integer> findEvenNumbers(Collection<Integer> baseCollection) {
|
||||
Predicate<Integer> eclipsePredicate = item -> item % 2 == 0;
|
||||
Collection<Integer> filteredList = Lists.mutable.ofAll(baseCollection)
|
||||
.select(eclipsePredicate);
|
||||
|
||||
return filteredList;
|
||||
}
|
||||
|
||||
static public Collection<Integer> findEvenNumbersUsingIterate(Collection<Integer> baseCollection) {
|
||||
Predicate<Integer> eclipsePredicate = new Predicate<Integer>() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public boolean accept(Integer arg0) {
|
||||
return arg0 % 2 == 0;
|
||||
}
|
||||
};
|
||||
Collection<Integer> filteredList = Iterate.select(baseCollection, eclipsePredicate);
|
||||
|
||||
return filteredList;
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.java.filtering;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.Collections2;
|
||||
|
||||
public class GuavaCollectionFilter {
|
||||
|
||||
static public Collection<Integer> findEvenNumbers(Collection<Integer> baseCollection) {
|
||||
Predicate<Integer> guavaPredicate = new Predicate<Integer>() {
|
||||
|
||||
@Override
|
||||
public boolean apply(Integer input) {
|
||||
return input % 2 == 0;
|
||||
}
|
||||
};
|
||||
Collection<Integer> filteredCollection = Collections2.filter(baseCollection, guavaPredicate);
|
||||
return filteredCollection;
|
||||
}
|
||||
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.java.filtering;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class StreamsCollectionFilter {
|
||||
|
||||
public static <T> Collection<T> filterCollectionHelperMethod(Collection<T> baseCollection, Predicate<T> predicate) {
|
||||
return baseCollection.stream()
|
||||
.filter(predicate)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
static public Collection<Integer> findEvenNumbersUsingHelperMethod(Collection<Integer> baseCollection) {
|
||||
return filterCollectionHelperMethod(baseCollection, item -> item % 2 == 0);
|
||||
}
|
||||
|
||||
static public Collection<Integer> findEvenNumbersUsingLambda(Collection<Integer> baseCollection) {
|
||||
return baseCollection.stream()
|
||||
.filter(item -> item % 2 == 0)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
static public Collection<Integer> findEvenNumbersUsingPredicate(Collection<Integer> baseCollection) {
|
||||
Predicate<Integer> evenNumberPredicate = new Predicate<Integer>() {
|
||||
@Override
|
||||
public boolean test(Integer i) {
|
||||
return i % 2 == 0;
|
||||
}
|
||||
};
|
||||
|
||||
return baseCollection.stream()
|
||||
.filter(evenNumberPredicate)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user