Oh. I have Java 8 and want to collect a
Map<K, V>
from
Stream<Tuple2<K, V>>
I do not want to use Pair, because of verbose syntax. Is there any way to do
.collect(toMap(Tuple2::_1, Tuple2::_2))
Thanx
Oh. I have Java 8 and want to collect a
Map<K, V>
from
Stream<Tuple2<K, V>>
I do not want to use Pair, because of verbose syntax. Is there any way to do
.collect(toMap(Tuple2::_1, Tuple2::_2))
Thanx
AFAIK, This is not avoidable in java (compared to the way its done in scala.)
However If you're going to use a lot of Tuple to Map Conversions in your code and want to avoid verbose syntax, You could create a custom TupleCollector
and add a toMap
Method.
This might be the closet you'll get to scala.
static class TupleCollector {
public static <K, V, T extends Tuple2<K, V>> Collector<T, ?, Map<K, V>> toMap() {
return Collectors.toMap(T::_1, T::_2);
}
}
Code to Invoke
import static TupleCollector.toMap
...
myStream.collect(toMap());
PS: Then again, I wouldn't be surprised if this doesn't pass code review phase.