diff --git a/core-java-9/src/test/java/com/baeldung/java9/language/stream/CollectionFilterUnitTest.java b/core-java-9/src/test/java/com/baeldung/java9/language/stream/CollectionFilterUnitTest.java index 0a6424fa81..1b9315678a 100644 --- a/core-java-9/src/test/java/com/baeldung/java9/language/stream/CollectionFilterUnitTest.java +++ b/core-java-9/src/test/java/com/baeldung/java9/language/stream/CollectionFilterUnitTest.java @@ -18,7 +18,6 @@ public class CollectionFilterUnitTest { private static final Collection BASE_INTEGER_COLLECTION = Arrays.asList(9, 12, 55, 56, 101, 115, 8002, 223, 2668, 19, 8); private static final Map> EXPECTED_EVEN_FILTERED_AFTER_GROUPING_MAP = createExpectedFilterAfterGroupingMap(); - private static Map> createExpectedFilterAfterGroupingMap() { Map> map = new HashMap<>(); map.put(1, Arrays.asList(8)); @@ -30,7 +29,6 @@ public class CollectionFilterUnitTest { } private static final Map> EXPECTED_EVEN_FILTERED_BEFORE_GROUPING_MAP = createExpectedFilterBeforeGroupingMap(); - private static Map> createExpectedFilterBeforeGroupingMap() { Map> map = new HashMap<>(); map.put(1, Arrays.asList(8)); diff --git a/core-java-collections/pom.xml b/core-java-collections/pom.xml index d63f4d2a45..ff06714bfe 100644 --- a/core-java-collections/pom.xml +++ b/core-java-collections/pom.xml @@ -39,12 +39,12 @@ org.eclipse.collections eclipse-collections-api - 9.2.0 + ${eclipse.collections.version} org.eclipse.collections eclipse-collections - 9.2.0 + ${eclipse.collections.version} org.assertj @@ -67,5 +67,6 @@ 4.01 1.7.0 3.6.1 + 9.2.0 diff --git a/core-java-collections/src/main/java/com/baeldung/java/filtering/CollectionUtilsCollectionFilter.java b/core-java-collections/src/main/java/com/baeldung/java/filtering/CollectionUtilsCollectionFilter.java index 4d6a3d62fd..58f9f6af54 100644 --- a/core-java-collections/src/main/java/com/baeldung/java/filtering/CollectionUtilsCollectionFilter.java +++ b/core-java-collections/src/main/java/com/baeldung/java/filtering/CollectionUtilsCollectionFilter.java @@ -8,13 +8,7 @@ import org.apache.commons.collections4.Predicate; public class CollectionUtilsCollectionFilter { static public Collection findEvenNumbers(Collection baseCollection) { - Predicate apacheEventNumberPredicate = new Predicate() { - - @Override - public boolean evaluate(Integer object) { - return object % 2 == 0; - } - }; + Predicate apacheEventNumberPredicate = item -> item % 2 == 0; CollectionUtils.filter(baseCollection, apacheEventNumberPredicate); return baseCollection; diff --git a/core-java-collections/src/main/java/com/baeldung/java/filtering/GuavaCollectionFilter.java b/core-java-collections/src/main/java/com/baeldung/java/filtering/GuavaCollectionFilter.java index f5a27f0bdb..88338fd6d4 100644 --- a/core-java-collections/src/main/java/com/baeldung/java/filtering/GuavaCollectionFilter.java +++ b/core-java-collections/src/main/java/com/baeldung/java/filtering/GuavaCollectionFilter.java @@ -8,13 +8,8 @@ import com.google.common.collect.Collections2; public class GuavaCollectionFilter { static public Collection findEvenNumbers(Collection baseCollection) { - Predicate guavaPredicate = new Predicate() { + Predicate guavaPredicate = item -> item % 2 == 0; - @Override - public boolean apply(Integer input) { - return input % 2 == 0; - } - }; Collection filteredCollection = Collections2.filter(baseCollection, guavaPredicate); return filteredCollection; } diff --git a/core-java-collections/src/main/java/com/baeldung/java/filtering/StreamsCollectionFilter.java b/core-java-collections/src/main/java/com/baeldung/java/filtering/StreamsCollectionFilter.java index d51283de55..f074f74199 100644 --- a/core-java-collections/src/main/java/com/baeldung/java/filtering/StreamsCollectionFilter.java +++ b/core-java-collections/src/main/java/com/baeldung/java/filtering/StreamsCollectionFilter.java @@ -16,22 +16,11 @@ public class StreamsCollectionFilter { return filterCollectionHelperMethod(baseCollection, item -> item % 2 == 0); } - static public Collection findEvenNumbersUsingLambda(Collection baseCollection) { - return baseCollection.stream() - .filter(item -> item % 2 == 0) - .collect(Collectors.toList()); - } - - static public Collection findEvenNumbersUsingPredicate(Collection baseCollection) { - Predicate evenNumberPredicate = new Predicate() { - @Override - public boolean test(Integer i) { - return i % 2 == 0; - } - }; + static public Collection findEvenNumbers(Collection baseCollection) { + Predicate streamsPredicate = item -> item % 2 == 0; return baseCollection.stream() - .filter(evenNumberPredicate) + .filter(streamsPredicate) .collect(Collectors.toList()); } } diff --git a/core-java-collections/src/test/java/com/baeldung/java/filtering/CollectionFiltersUnitTest.java b/core-java-collections/src/test/java/com/baeldung/java/filtering/CollectionFiltersUnitTest.java index c85b7a526d..b30805d471 100644 --- a/core-java-collections/src/test/java/com/baeldung/java/filtering/CollectionFiltersUnitTest.java +++ b/core-java-collections/src/test/java/com/baeldung/java/filtering/CollectionFiltersUnitTest.java @@ -27,15 +27,13 @@ public class CollectionFiltersUnitTest { @Test public void givenAnIntegerCollection_whenFilteringEvenValues_thenObtainTheFilteredCollectionForAllCases() { - Collection filteredWithStreams1 = StreamsCollectionFilter.findEvenNumbersUsingLambda(BASE_INTEGER_COLLECTION); - Collection filteredWithStreams2 = StreamsCollectionFilter.findEvenNumbersUsingPredicate(BASE_INTEGER_COLLECTION); + Collection filteredWithStreams1 = StreamsCollectionFilter.findEvenNumbers(BASE_INTEGER_COLLECTION); Collection filteredWithCollectionUtils = CollectionUtilsCollectionFilter.findEvenNumbers(new ArrayList<>(BASE_INTEGER_COLLECTION)); Collection filteredWithEclipseCollections = EclipseCollectionsCollectionFilter.findEvenNumbers(BASE_INTEGER_COLLECTION); Collection filteredWithEclipseCollectionsUsingIterate = EclipseCollectionsCollectionFilter.findEvenNumbersUsingIterate(BASE_INTEGER_COLLECTION); Collection filteredWithGuava = GuavaCollectionFilter.findEvenNumbers(BASE_INTEGER_COLLECTION); - assertThat(filteredWithStreams1).hasSameElementsAs(filteredWithStreams2) - .hasSameElementsAs(filteredWithCollectionUtils) + assertThat(filteredWithStreams1).hasSameElementsAs(filteredWithCollectionUtils) .hasSameElementsAs(filteredWithEclipseCollections) .hasSameElementsAs(filteredWithEclipseCollectionsUsingIterate) .hasSameElementsAs(filteredWithEclipseCollectionsUsingIterate)