Skip to content

Commit

Permalink
Suggest using streams instead of Maps.toMap().
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 646599789
  • Loading branch information
java-team-github-bot authored and Google Java Core Libraries committed Jun 25, 2024
1 parent 93c7a3a commit b50df33
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions android/guava/src/com/google/common/collect/Maps.java
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,18 @@ public NavigableSet<E> descendingSet() {
* <p>If {@code keys} is a {@link Set}, a live view can be obtained instead of a copy using {@link
* Maps#asMap(Set, Function)}.
*
* <p><b>Note:</b> on Java 8+, it is usually better to use streams. For example:
*
* <pre>{@code
* import static com.google.common.collect.ImmutableMap.toImmutableMap;
* ...
* ImmutableMap<Color, String> colorNames =
* allColors.stream().collect(toImmutableMap(c -> c, c -> c.toString()));
* }</pre>
*
* <p>Streams provide a more standard and flexible API and the lambdas make it clear what the keys
* and values in the map are.
*
* @throws NullPointerException if any element of {@code keys} is {@code null}, or if {@code
* valueFunction} produces {@code null} for any key
* @since 14.0
Expand Down
12 changes: 12 additions & 0 deletions guava/src/com/google/common/collect/Maps.java
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,18 @@ public NavigableSet<E> descendingSet() {
* <p>If {@code keys} is a {@link Set}, a live view can be obtained instead of a copy using {@link
* Maps#asMap(Set, Function)}.
*
* <p><b>Note:</b> on Java 8+, it is usually better to use streams. For example:
*
* <pre>{@code
* import static com.google.common.collect.ImmutableMap.toImmutableMap;
* ...
* ImmutableMap<Color, String> colorNames =
* allColors.stream().collect(toImmutableMap(c -> c, c -> c.toString()));
* }</pre>
*
* <p>Streams provide a more standard and flexible API and the lambdas make it clear what the keys
* and values in the map are.
*
* @throws NullPointerException if any element of {@code keys} is {@code null}, or if {@code
* valueFunction} produces {@code null} for any key
* @since 14.0
Expand Down

0 comments on commit b50df33

Please sign in to comment.