* Added examples for java-9 filtering collector
This commit is contained in:
parent
5b489423d8
commit
6b2fb615a7
@ -19,11 +19,23 @@
|
||||
<version>${awaitility.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@ -50,6 +62,8 @@
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.10.0</assertj.version>
|
||||
<junit.platform.version>1.2.0</junit.platform.version>
|
||||
<awaitility.version>1.7.0</awaitility.version>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.target>1.9</maven.compiler.target>
|
||||
|
@ -0,0 +1,27 @@
|
||||
package com.baeldung.java9.language.stream;
|
||||
|
||||
import static java.util.stream.Collectors.filtering;
|
||||
import static java.util.stream.Collectors.groupingBy;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class StreamsGroupingCollectionFilter {
|
||||
|
||||
static public Map<Integer, List<Integer>> findEvenNumbersAfterGroupingByQuantityOfDigits(Collection<Integer> baseCollection) {
|
||||
Function<Integer, Integer> getQuantityOfDigits = item -> (int) Math.log10(item) + 1;
|
||||
|
||||
return baseCollection.stream()
|
||||
.collect(groupingBy(getQuantityOfDigits, filtering(item -> item % 2 == 0, toList())));
|
||||
}
|
||||
|
||||
static public Map<Integer, List<Integer>> findEvenNumbersBeforeGroupingByQuantityOfDigits(Collection<Integer> baseCollection) {
|
||||
|
||||
return baseCollection.stream()
|
||||
.filter(item -> item % 2 == 0)
|
||||
.collect(groupingBy(item -> (int) Math.log10(item) + 1, toList()));
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package com.baeldung.java9.language.stream;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.platform.runner.JUnitPlatform;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(JUnitPlatform.class)
|
||||
public class CollectionFilterUnitTest {
|
||||
|
||||
private static final Collection<Integer> BASE_INTEGER_COLLECTION = Arrays.asList(9, 12, 55, 56, 101, 115, 8002, 223, 2668, 19, 8);
|
||||
private static final Map<Integer, List<Integer>> EXPECTED_EVEN_FILTERED_AFTER_GROUPING_MAP = createExpectedFilterAfterGroupingMap();
|
||||
|
||||
private static Map<Integer, List<Integer>> createExpectedFilterAfterGroupingMap() {
|
||||
Map<Integer, List<Integer>> map = new HashMap<>();
|
||||
map.put(1, Arrays.asList(8));
|
||||
map.put(2, Arrays.asList(12, 56));
|
||||
map.put(3, Collections.emptyList());
|
||||
map.put(4, Arrays.asList(8002, 2668));
|
||||
return map;
|
||||
|
||||
}
|
||||
|
||||
private static final Map<Integer, List<Integer>> EXPECTED_EVEN_FILTERED_BEFORE_GROUPING_MAP = createExpectedFilterBeforeGroupingMap();
|
||||
|
||||
private static Map<Integer, List<Integer>> createExpectedFilterBeforeGroupingMap() {
|
||||
Map<Integer, List<Integer>> map = new HashMap<>();
|
||||
map.put(1, Arrays.asList(8));
|
||||
map.put(2, Arrays.asList(12, 56));
|
||||
map.put(4, Arrays.asList(8002, 2668));
|
||||
return map;
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAStringCollection_whenFilteringFourLetterWords_thenObtainTheFilteredCollection() {
|
||||
Map<Integer, List<Integer>> filteredAfterGroupingMap = StreamsGroupingCollectionFilter.findEvenNumbersAfterGroupingByQuantityOfDigits(BASE_INTEGER_COLLECTION);
|
||||
Map<Integer, List<Integer>> filteredBeforeGroupingMap = StreamsGroupingCollectionFilter.findEvenNumbersBeforeGroupingByQuantityOfDigits(BASE_INTEGER_COLLECTION);
|
||||
|
||||
assertThat(filteredAfterGroupingMap).containsAllEntriesOf(EXPECTED_EVEN_FILTERED_AFTER_GROUPING_MAP);
|
||||
assertThat(filteredBeforeGroupingMap).doesNotContainKey(3)
|
||||
.containsAllEntriesOf(EXPECTED_EVEN_FILTERED_BEFORE_GROUPING_MAP);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user