Skip to content

Commit

Permalink
Merge pull request #248 from stickfigure/to-map-key
Browse files Browse the repository at this point in the history
Add toMapKey()
  • Loading branch information
amaembo authored Nov 7, 2021
2 parents d5d57c2 + f0656da commit 7f23def
Showing 1 changed file with 96 additions and 3 deletions.
99 changes: 96 additions & 3 deletions src/main/java/one/util/streamex/StreamEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,36 @@ public <K, V> Map<K, V> toMap(Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends V> valMapper, BinaryOperator<V> mergeFunction) {
return rawCollect(Collectors.toMap(keyMapper, valMapper, mergeFunction, HashMap::new));
}


/**
* Returns a {@link Map} whose values are elements from this stream and keys
* are the result of applying the provided mapping functions to the input
* elements.
*
* <p>
* This is a <a href="package-summary.html#StreamOps">terminal</a>
* operation.
*
* <p>
* Returned {@code Map} is guaranteed to be modifiable.
*
* <p>
* For parallel stream the concurrent {@code Map} is created.
*
* @param <K> the output type of the key mapping function
* @param keyMapper a mapping function to produce keys
* @return a {@code Map} whose values are elements from this stream and keys
* are the result of applying mapping function to the input elements
* @throws IllegalStateException if this stream contains duplicate objects
* (according to {@link Object#equals(Object)})
* @see Collectors#toMap(Function, Function)
* @see Collectors#toConcurrentMap(Function, Function)
* @see #toMap(Function, Function)
*/
public <K> Map<K, T> toMapKey(Function<? super T, ? extends K> keyMapper) {
return toMap(keyMapper, Function.identity());
}

/**
* Drains the stream content into the supplied collection.
*
Expand Down Expand Up @@ -1177,7 +1206,39 @@ public <K, V> SortedMap<K, V> toSortedMap(Function<? super T, ? extends K> keyMa
Function<? super T, ? extends V> valMapper, BinaryOperator<V> mergeFunction) {
return rawCollect(Collectors.toMap(keyMapper, valMapper, mergeFunction, TreeMap::new));
}


/**
* Returns a {@link SortedMap} whose values are elements from this stream and keys
* are the result of applying the provided mapping functions to the input
* elements.
*
* <p>
* This is a <a href="package-summary.html#StreamOps">terminal</a>
* operation.
*
* <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.
*
* <p>
* Returned {@code SortedMap} is guaranteed to be modifiable.
*
* @param <K> the output type of the key mapping function
* @param keyMapper a mapping function to produce keys
* @return a {@code Map} whose values are elements from this stream and keys
* are the result of applying mapping function to the input elements
* @throws IllegalStateException if this stream contains duplicate objects
* (according to {@link Object#equals(Object)})
* @see Collectors#toMap(Function, Function)
* @see Collectors#toConcurrentMap(Function, Function)
* @see #toMap(Function, Function)
*/
public <K> SortedMap<K, T> toSortedMapKey(Function<? super T, ? extends K> keyMapper) {
return toSortedMap(keyMapper, Function.identity());
}

/**
* Returns a {@link NavigableMap} whose keys are elements from this stream and
* values are the result of applying the provided mapping functions to the
Expand Down Expand Up @@ -1288,7 +1349,39 @@ public <K, V> NavigableMap<K, V> toNavigableMap(Function<? super T, ? extends K>
Function<? super T, ? extends V> valMapper, BinaryOperator<V> mergeFunction) {
return rawCollect(Collectors.toMap(keyMapper, valMapper, mergeFunction, TreeMap::new));
}


/**
* Returns a {@link NavigableMap} whose values are elements from this stream and keys
* are the result of applying the provided mapping functions to the input
* elements.
*
* <p>
* This is a <a href="package-summary.html#StreamOps">terminal</a>
* operation.
*
* <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.
*
* <p>
* Returned {@code NavigableMap} is guaranteed to be modifiable.
*
* @param <K> the output type of the key mapping function
* @param keyMapper a mapping function to produce keys
* @return a {@code Map} whose values are elements from this stream and keys
* are the result of applying mapping function to the input elements
* @throws IllegalStateException if this stream contains duplicate objects
* (according to {@link Object#equals(Object)})
* @see Collectors#toMap(Function, Function)
* @see Collectors#toConcurrentMap(Function, Function)
* @see #toMap(Function, Function)
*/
public <K> NavigableMap<K, T> toNavigableMapKey(Function<? super T, ? extends K> keyMapper) {
return toNavigableMap(keyMapper, Function.identity());
}

/**
* Returns a new {@code StreamEx} which is a concatenation of this stream
* and the supplied values.
Expand Down

0 comments on commit 7f23def

Please sign in to comment.