MapStream.sorted

Peter Levart peter.levart at marand.si
Tue May 8 01:22:51 PDT 2012


Hi Mike and others,

Sorry to be picky, but the following MapStream method:

    MapStream<K, V> sorted(Comparator<? super K> comparator) default {
        TreeMap<K,V> result = new TreeMap<>(comparator);
        forEach( (k, v) -> { result.put(k, v); } );

        return result;
    }


..."squashes" entries with duplicate keys (those that comparator.compare(k1, 
k2) returns 0 for)...

The analog Iterable.sorted is more correct (uses PriorityQueue).

One still might want this behavior, but she could achieve that same by:

    mapStream.into(new TreeMap<>(comparator))


Regards, Peter



More information about the lambda-dev mailing list