eclipse-collections icon indicating copy to clipboard operation
eclipse-collections copied to clipboard

Implement MapIterable.collectKeysUnique().

Open motlin opened this issue 8 years ago • 10 comments

<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.

motlin avatar Nov 17 '17 12:11 motlin

Perhaps call it collectKeysUnique if it is going to throw?

donraab avatar Nov 17 '17 15:11 donraab

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

nikhilnanivadekar avatar Nov 17 '17 16:11 nikhilnanivadekar

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 .

motlin avatar Nov 17 '17 17:11 motlin

how about we add both collectKeys() which will return a Multimap with a target and collectUniqueKeys which will return a Map?

nikhilnanivadekar avatar Nov 17 '17 17:11 nikhilnanivadekar

@motlin is this still valid issue?

nikhilnanivadekar avatar Aug 21 '20 03:08 nikhilnanivadekar

I think so. It might even be a good first issue.

motlin avatar Aug 21 '20 19:08 motlin

Can you please recap the signature?

nikhilnanivadekar avatar Aug 21 '20 19:08 nikhilnanivadekar

It's in the description above.

<R> MapIterable<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function);

motlin avatar Aug 21 '20 19:08 motlin

Should we throw or not throw for non-unique?

nikhilnanivadekar avatar Aug 21 '20 19:08 nikhilnanivadekar

I'm leaning toward: let's include unique in the name and let's throw.

motlin avatar Aug 21 '20 20:08 motlin