Could someone suggest how could I transform list like ["bla", "blabla", "blablabla"]
to map like {"bla" : 3, "blabla" : 6, "blablabla" : 9}
with words stands for keys and values stands for words lengths?
I do something like:
Map<String, Integer> map = list.stream().collect(Collectors.groupingBy(Function.identity(), String::length));
but have no luck.
Thank you!
You were almost correct with groupingBy
, but the second parameter of that is a Collector
, not a Function
. Thus I used toMap
.
Map<String, Integer> map = Stream.of("bla", "blabla", "blablabla").distinct()
.collect(Collectors.toMap(Function.identity(), String::length));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With