Skip to content

Commit

Permalink
[#238] Renamed to valuesToMap(), etc., javadocs, tests, changes, chea…
Browse files Browse the repository at this point in the history
…tsheet
  • Loading branch information
amaembo committed Nov 7, 2021
1 parent d42cd95 commit a783a50
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 26 deletions.
37 changes: 27 additions & 10 deletions src/main/java/one/util/streamex/StreamEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ public <A> A[] toArray(Class<A> elementClass) {
/**
* Returns an array containing all the stream elements. If the stream
* happens to contain no elements, the supplied empty array is returned
* instead. Otherwise, the new array is allocated which element type is the
* instead. Otherwise, the new array is allocated, whose element type is the
* same as the element type of supplied empty array.
*
* <p>
Expand All @@ -851,7 +851,7 @@ public <A> A[] toArray(Class<A> elementClass) {
* <p>
* This method is useful when the stream is expected to return empty arrays
* often, so the same instance of empty array (presumably declared in some
* static final field) can be reused.
* {@code static final} field) can be reused.
*
* @param <A> the element type of the resulting array
* @param emptyArray an empty array of the resulting type
Expand Down Expand Up @@ -946,6 +946,10 @@ public <U> List<U> toFlatList(Function<? super T, ? extends Collection<U>> mappe
* <p>
* For parallel stream the concurrent {@code Map} is created.
*
* <p>
* Use {@link #valuesToMap(Function)} if this stream contains values,
* instead of keys.
*
* @param <V> the output type of the value mapping function
* @param valMapper a mapping function to produce values
* @return a {@code Map} whose keys are elements from this stream and values
Expand Down Expand Up @@ -986,6 +990,7 @@ public <V> Map<T, V> toMap(Function<? super T, ? extends V> valMapper) {
* @see Collectors#toMap(Function, Function)
* @see Collectors#toConcurrentMap(Function, Function)
* @see #toMap(Function)
* @see #valuesToMap(Function)
*/
public <K, V> Map<K, V> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valMapper) {
Map<K, V> map = isParallel() ? new ConcurrentHashMap<>() : new HashMap<>();
Expand Down Expand Up @@ -1055,8 +1060,9 @@ public <K, V> Map<K, V> toMap(Function<? super T, ? extends K> keyMapper,
* @see Collectors#toMap(Function, Function)
* @see Collectors#toConcurrentMap(Function, Function)
* @see #toMap(Function, Function)
* @since 0.8.0
*/
public <K> Map<K, T> toMapKey(Function<? super T, ? extends K> keyMapper) {
public <K> Map<K, T> valuesToMap(Function<? super T, ? extends K> keyMapper) {
return toMap(keyMapper, Function.identity());
}

Expand Down Expand Up @@ -1114,6 +1120,10 @@ public <C extends Collection<? super T>> C into(C collection) {
* <p>
* Returned {@code SortedMap} is guaranteed to be modifiable.
*
* <p>
* Use {@link #valuesToSortedMap(Function)} if this stream contains values,
* instead of keys.
*
* @param <V> the output type of the value mapping function
* @param valMapper a mapping function to produce values
* @return a {@code SortedMap} whose keys are elements from this stream and
Expand Down Expand Up @@ -1159,6 +1169,7 @@ public <V> SortedMap<T, V> toSortedMap(Function<? super T, ? extends V> valMappe
* @see Collectors#toMap(Function, Function)
* @see Collectors#toConcurrentMap(Function, Function)
* @see #toSortedMap(Function)
* @see #valuesToSortedMap(Function)
* @see #toNavigableMap(Function, Function)
* @since 0.1.0
*/
Expand Down Expand Up @@ -1218,9 +1229,8 @@ public <K, V> SortedMap<K, V> toSortedMap(Function<? super T, ? extends K> keyMa
*
* <p>
* If the mapped keys contains duplicates (according to
* {@link Object#equals(Object)}), the value mapping function is applied to
* each equal element, and the results are merged using the provided merging
* function.
* {@link Object#equals(Object)}), an {@code IllegalStateException} is
* thrown when the collection operation is performed.
*
* <p>
* Returned {@code SortedMap} is guaranteed to be modifiable.
Expand All @@ -1233,9 +1243,10 @@ public <K, V> SortedMap<K, V> toSortedMap(Function<? super T, ? extends K> keyMa
* (according to {@link Object#equals(Object)})
* @see Collectors#toMap(Function, Function)
* @see Collectors#toConcurrentMap(Function, Function)
* @see #toMap(Function, Function)
* @see #toSortedMap(Function, Function)
* @since 0.8.0
*/
public <K> SortedMap<K, T> toSortedMapKey(Function<? super T, ? extends K> keyMapper) {
public <K> SortedMap<K, T> valuesToSortedMap(Function<? super T, ? extends K> keyMapper) {
return toSortedMap(keyMapper, Function.identity());
}

Expand All @@ -1259,6 +1270,10 @@ public <K> SortedMap<K, T> toSortedMapKey(Function<? super T, ? extends K> keyMa
* <p>
* Returned {@code NavigableMap} is guaranteed to be modifiable.
*
* <p>
* Use {@link #valuesToNavigableMap(Function)} if this stream contains values,
* instead of keys.
*
* @param <V> the output type of the value mapping function
* @param valMapper a mapping function to produce values
* @return a {@code NavigableMap} whose keys are elements from this stream and
Expand Down Expand Up @@ -1303,6 +1318,7 @@ public <V> NavigableMap<T, V> toNavigableMap(Function<? super T, ? extends V> va
* @see Collectors#toMap(Function, Function)
* @see Collectors#toConcurrentMap(Function, Function)
* @see #toNavigableMap(Function)
* @see #valuesToNavigableMap(Function)
* @since 0.6.5
*/
public <K, V> NavigableMap<K, V> toNavigableMap(Function<? super T, ? extends K> keyMapper,
Expand Down Expand Up @@ -1376,9 +1392,10 @@ public <K, V> NavigableMap<K, V> toNavigableMap(Function<? super T, ? extends K>
* (according to {@link Object#equals(Object)})
* @see Collectors#toMap(Function, Function)
* @see Collectors#toConcurrentMap(Function, Function)
* @see #toMap(Function, Function)
* @see #toNavigableMap(Function, Function)
* @since 0.8.0
*/
public <K> NavigableMap<K, T> toNavigableMapKey(Function<? super T, ? extends K> keyMapper) {
public <K> NavigableMap<K, T> valuesToNavigableMap(Function<? super T, ? extends K> keyMapper) {
return toNavigableMap(keyMapper, Function.identity());
}

Expand Down
52 changes: 36 additions & 16 deletions src/test/java/one/util/streamex/api/StreamExTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@
*/
package one.util.streamex.api;

import one.util.streamex.EntryStream;
import one.util.streamex.IntStreamEx;
import one.util.streamex.Joining;
import one.util.streamex.MoreCollectors;
import one.util.streamex.StreamEx;
import one.util.streamex.TestHelpers.Point;

import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runners.MethodSorters;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -74,21 +87,7 @@
import java.util.stream.IntStream;
import java.util.stream.Stream;

import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runners.MethodSorters;

import one.util.streamex.EntryStream;
import one.util.streamex.IntStreamEx;
import one.util.streamex.Joining;
import one.util.streamex.MoreCollectors;
import one.util.streamex.StreamEx;
import one.util.streamex.TestHelpers.Point;

import static java.util.Arrays.asList;
import static one.util.streamex.TestHelpers.assertStatementThrows;
import static one.util.streamex.TestHelpers.checkIllegalStateException;
import static one.util.streamex.TestHelpers.checkSpliterator;
import static one.util.streamex.TestHelpers.emptySpliteratorWithExactSize;
Expand Down Expand Up @@ -356,6 +355,27 @@ public void testToMap() {
});
}

@Test
public void testValuesToMap() {
Map<Integer, String> expected = new HashMap<>();
expected.put(1, "a");
expected.put(2, "bb");
expected.put(3, "ccc");
streamEx(() -> Stream.of("a", "bb", "ccc"), supplier -> {
Map<Integer, String> map = supplier.get().valuesToMap(String::length);
assertEquals(supplier.get().isParallel(), map instanceof ConcurrentMap);
assertEquals(expected, map);

SortedMap<Integer, String> smap = supplier.get().valuesToSortedMap(String::length);
assertEquals(supplier.get().isParallel(), smap instanceof ConcurrentMap);
assertEquals(expected, smap);

NavigableMap<Integer, String> nmap = supplier.get().valuesToNavigableMap(String::length);
assertEquals(supplier.get().isParallel(), nmap instanceof ConcurrentMap);
assertEquals(expected, nmap);
});
}

@Test
public void testToSortedMap() {
Map<String, Integer> expected = new HashMap<>();
Expand Down Expand Up @@ -1478,7 +1498,7 @@ public void testRunLengthsSorted() {
}

/*
* Returns longest input stream segment for which the predicate holds (like
* Returns the longest input stream segment for which the predicate holds (like
* the corresponding Scala method)
*/
private static long segmentLength(IntStreamEx source, IntPredicate predicate) {
Expand Down Expand Up @@ -1628,7 +1648,7 @@ public void testDropWhile() {
@Test
public void testTakeDropUnordered() {
repeat(10, n -> withRandom(rnd -> {
List<Boolean> data = IntStreamEx.of(rnd, n * 100, 0, rnd.nextInt(10) + 2).mapToObj(x -> x != 0).toList();
List<Boolean> data = IntStreamEx.of(rnd, n * 100L, 0, rnd.nextInt(10) + 2).mapToObj(x -> x != 0).toList();
List<Boolean> sorted = StreamEx.of(data).sorted().toList();
streamEx(() -> data.stream().unordered(), s -> {
assertFalse(StreamEx.of(s.get().takeWhile(b -> b).toList()).has(false));
Expand Down
1 change: 1 addition & 0 deletions wiki/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Check also [MIGRATION.md](MIGRATION.md) for possible compatibility problems.
* [#244] Added: `StreamEx.toMutableList` and `StreamEx.toMutableSet`.
Removed mutability guarantee for `toList` and `toSet`.
Added temporary system property `streamex.default.immutable` (set it to 'true' to test with immutable `toList` and `toSet`)
* [#238] Added: `valuesToMap`, `valuesToSortedMap`, `valuesToNavigableMap` methods to `StreamEx`
* [#250] Fixed: `EntryStream.withoutKeys` and `EntryStream.withoutValues` declared as `@SafeVarargs`
* [#251] Changed: public classes like `StreamEx` and `EntryStream` declared as final now.
* [#246] Added: Proper `module-info.class`, now it's full-fledged JPMS module
Expand Down
1 change: 1 addition & 0 deletions wiki/CHEATSHEET.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ Collect elements to `Collection` adding custom final step | `StreamEx/EntryStrea
Collect elements to immutable `List` or `Set` | `StreamEx/EntryStream.toImmutableList()/toImmutableSet()`
Collect elements to mutable `List` or `Set` | `StreamEx/EntryStream.toMutableList()/toMutableSet()`
Collect elements or entries to `Map` | `StreamEx/EntryStream.toMap()/toSortedMap()/toNavigableMap()`
Collect stream of values to `Map` | `StreamEx.valuesToMap()/valuesToSortedMap()/valuesToNavigableMap()`
Collect entries to immutable `Map` | `EntryStream.toImmutableMap()`
Collect entries to `Map` adding custom final step | `EntryStream.toMapAndThen()`
Collect entries to custom `Map` | `EntryStream.toCustomMap()`
Expand Down

0 comments on commit a783a50

Please sign in to comment.