Implement MapIterable.collectKeysUnique().
<R> MapIterable<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function);
or
<R> MapIterable<R, V> collectKeysUnique(Function<? super K, ? extends R> function);
Similar to the existing api:
<R> MapIterable<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);
collectKeys is trickier than collectValues because after transformation, the new keys can be equal to each other and collapse into a smaller map. I'm inclined to say that collectKeys should throw, similar to the behavior of groupByUniqueKey, when the function returns non-unique results. On the other hand, RichIterable.toMap() and MutableMap.collectKeysAndValues() already silently collapse duplicates and no one seems to complain.
Perhaps call it collectKeysUnique if it is going to throw?
If someone needs to only collect on keys and not do anything to values and not throw for dups they can always use collectKeysAndValue() and bypass the valueFunction.
For handling duplicates on collectKeys() we can return a BagMultimap or we can just take a target Multimap and not have any covariant overrides.
If we want to throw on duplicates, we can call it collectUniqueKeys or collectKeysUnique
collectKeysAndValues() isn’t fluent. You call it on the target map, not the source. It’s a strange method.
In my use-case, the original Map keys don’t implement equal/hashcode, and I want to transform them by calling a getter which returns their unique property, which is just a name. I don’t want to get back a bag because I do expect the transformation to be unique.
Both collectKeysUnique and collectUniqueKeys sound good to me. On Fri, Nov 17, 2017 at 11:38 AM Nikhil Nanivadekar < [email protected]> wrote:
If someone needs to only collect on keys and not do anything to values and not throw for dups they can always use collectKeysAndValue() and bypass the valueFunction. For handling duplicates on collectKeys() we can return a BagMultimap or we can just take a target Multimap and not have any covariant overrides. If we want to throw on duplicates, we can call it collectUniqueKeys or collectKeysUnique
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/eclipse/eclipse-collections/issues/409#issuecomment-345295042, or mute the thread https://github.com/notifications/unsubscribe-auth/AAO6Igwvcmb-wlN2ZPncTYcilkfrURcKks5s3baLgaJpZM4QiAG2 .
how about we add both collectKeys() which will return a Multimap with a target and collectUniqueKeys which will return a Map?
@motlin is this still valid issue?
I think so. It might even be a good first issue.
Can you please recap the signature?
It's in the description above.
<R> MapIterable<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function);
Should we throw or not throw for non-unique?
I'm leaning toward: let's include unique in the name and let's throw.