* minor fixes and cleaning duties

This commit is contained in:
geroza
2018-07-25 17:36:17 -03:00
committed by José Carlos Valero Sánchez
parent 6b2fb615a7
commit 81027b7fbd
6 changed files with 10 additions and 35 deletions
@@ -8,13 +8,7 @@ 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;
}
};
Predicate<Integer> apacheEventNumberPredicate = item -> item % 2 == 0;
CollectionUtils.filter(baseCollection, apacheEventNumberPredicate);
return baseCollection;
@@ -8,13 +8,8 @@ import com.google.common.collect.Collections2;
public class GuavaCollectionFilter {
static public Collection<Integer> findEvenNumbers(Collection<Integer> baseCollection) {
Predicate<Integer> guavaPredicate = new Predicate<Integer>() {
Predicate<Integer> guavaPredicate = item -> item % 2 == 0;
@Override
public boolean apply(Integer input) {
return input % 2 == 0;
}
};
Collection<Integer> filteredCollection = Collections2.filter(baseCollection, guavaPredicate);
return filteredCollection;
}
@@ -16,22 +16,11 @@ public class StreamsCollectionFilter {
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;
}
};
static public Collection<Integer> findEvenNumbers(Collection<Integer> baseCollection) {
Predicate<Integer> streamsPredicate = item -> item % 2 == 0;
return baseCollection.stream()
.filter(evenNumberPredicate)
.filter(streamsPredicate)
.collect(Collectors.toList());
}
}
@@ -27,15 +27,13 @@ public class CollectionFiltersUnitTest {
@Test
public void givenAnIntegerCollection_whenFilteringEvenValues_thenObtainTheFilteredCollectionForAllCases() {
Collection<Integer> filteredWithStreams1 = StreamsCollectionFilter.findEvenNumbersUsingLambda(BASE_INTEGER_COLLECTION);
Collection<Integer> filteredWithStreams2 = StreamsCollectionFilter.findEvenNumbersUsingPredicate(BASE_INTEGER_COLLECTION);
Collection<Integer> filteredWithStreams1 = StreamsCollectionFilter.findEvenNumbers(BASE_INTEGER_COLLECTION);
Collection<Integer> filteredWithCollectionUtils = CollectionUtilsCollectionFilter.findEvenNumbers(new ArrayList<>(BASE_INTEGER_COLLECTION));
Collection<Integer> filteredWithEclipseCollections = EclipseCollectionsCollectionFilter.findEvenNumbers(BASE_INTEGER_COLLECTION);
Collection<Integer> filteredWithEclipseCollectionsUsingIterate = EclipseCollectionsCollectionFilter.findEvenNumbersUsingIterate(BASE_INTEGER_COLLECTION);
Collection<Integer> filteredWithGuava = GuavaCollectionFilter.findEvenNumbers(BASE_INTEGER_COLLECTION);
assertThat(filteredWithStreams1).hasSameElementsAs(filteredWithStreams2)
.hasSameElementsAs(filteredWithCollectionUtils)
assertThat(filteredWithStreams1).hasSameElementsAs(filteredWithCollectionUtils)
.hasSameElementsAs(filteredWithEclipseCollections)
.hasSameElementsAs(filteredWithEclipseCollectionsUsingIterate)
.hasSameElementsAs(filteredWithEclipseCollectionsUsingIterate)