Java Streams and IDE Weirdness (part 2)

ImmutableMap<String, String> sourceMap = ImmutableMap.of("key1", "value1",
"key2", "value2");
ImmutableMap<String, String> destMap = sourceMap.entrySet()
.stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue
));
ImmutableMap<String, String> destMap = sourceMap.entrySet()
.stream()
.collect(ImmutableMap.toImmutableMap(Map.Entry::getKey, Map.Entry::getValue));
  1. Anytime you see this many generics in a javadoc slow way down to make sure you understand what’s happening public static <T,K,V> Collector <T,?,ImmutableMap<K,V>>
  2. Use as specific of a stream collector as you can
  3. When in doubt (or extreme need) revert to a good ol’ for loop

--

--

--

Engineer, formerly at Amazon, currently at Google. All opinions are my own. Consider supporting here: https://devblabs.medium.com/membership

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Top Five Reasons to take the CSSLP Exam This Year!

Windows ShellBags (Part 1)

Virtual Machine manager

Safely Rotating Azure CosmosDB Account Keys: A Process That Shouldn’t Be as Hard as It Is

Do you ever forget to push your commits?

alias shorten=”a loooong, very often used command”

10 Amazing Website Every Developer Should Know

Set Up GitLab CI for Rails Applications

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Brian Olson

Brian Olson

Engineer, formerly at Amazon, currently at Google. All opinions are my own. Consider supporting here: https://devblabs.medium.com/membership

More from Medium

Reactive programming: principles, standards, implementation in Java

Improve your system using Retry Policy — Part I (Failsafe)

Single Responsibility Principle (SRP)

Oh no, another Java Exceptions Blog post