Round 2 feedback

Brian Goetz brian.goetz at
Wed Feb 13 07:37:15 PST 2013

>> So, the idea is you take a Stream<T> and a function T->U and you get a
>> Map<T,U>.  If we had tuples, this would be -> (t, f(t))).
> Yes, I had understood how it works already, especially given the
> example provided by you in the SortedMap thread. The problem is the
> method name is completely unintuitive here. And yes, I wish I had some
> suggestions... :-(

It previously was called mappingTo.

> Sure. Imagine you have a Set<String> representing some cities in a
> business application. You call your business layer, that invokes a
> query and retuns a Map<String, Long> to you, based on a query that was
> run. You are going to use this map more than once; for some uses, it
> matters whether the city was found in the database or not, for others,
> you want non-found cities to contain zero. So to create the new Map
> you will do:
> Map<String, Long> totalByCityForEveryCity =
> ->
> totalByCity.optionalGet(city).orElse(0L));
> This is the simplest case, I have others in which I need to create a
> nested singleton map, for instance. This is a fairly common scenario
> in this code base, which is heavily collection-based.
> Hope this helps.

So, a static method:

   static<K,V> V getOrElse(Map<K,V> m, K k, V alt) {
     return map.containsKey(k) ? map.get(k) : alt;

will do what you want in the meantime? -> getOrElse(totalByCity, city, 

(The unordered is not adding any value here.)

Can simplify by wrapping:

   Function<K,V> totalOrZero = k -> getOrElse(totalByCity, k, 0);;

More information about the lambda-dev mailing list