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

Add groupByEachUniqueKey to RichIterable

Open donraab opened this issue 1 year ago • 6 comments

RichIterable currently has groupBy / groupByEach but only has groupByUniqueKey without the corresponding groupByEachUniqueKey. The method groupByEachUniqueKey would index an item in a Map using multiple keys. Each key will only point to one value. An example of this might be mapping a Person to his/her aliases.

I ran into this problem recently trying to group an Object that contains a distinct range of numbers by each of the distinct numbers. I was able to accomplish this by using a nested injectInto, but it is somewhat awkward and may be difficult to comprehend. In my particular case, it is more optimal using injectInto as I am grouping into an IntObjectMap.

donraab avatar Mar 22 '24 18:03 donraab

Hi @donraab I'd like to work on this issue. Could you please assign it to me. Thanks!

pranukrish avatar Aug 01 '24 20:08 pranukrish

Hi @pranukrish, done!

donraab avatar Aug 01 '24 20:08 donraab

Hi @donraab! So while trying to implement the functionality I realized that it has to be implemented in all the classes that inherit the RichIterable interface(and that's around a 100 classes) and I wanted to know if there is any work around to implementing this in all of these classes.

pranukrish avatar Aug 12 '24 01:08 pranukrish

Hi @pranukrish, yes adding a new method to RichIterable can be very complicated and involve a lot of changes. There is an approach which can split the total amount of work into multiple pieces. If you look at adding groupByEachUniqueKey with a target map similar to groupByUniqueKey, it might be possible to add the implementation as a default method on RichIterable. There will still need to be overrides in a few places, but the return type is not covariant so will always return R extends MutableMapIterable<V, T>.

I think the signature of this version of the method with a target map would look as follows:

    <V, R extends MutableMapIterable<V, T>> R groupByEachUniqueKey(
            Function<? super T, ? extends Iterable<V>> function,
            R target);

Let me know if this makes sense or needs any clarification. Thanks!

donraab avatar Aug 12 '24 02:08 donraab

The other thing which can make this kind of change easier is searching for similar methods and putting the new method in the same classes. For example, putting this right below each groupByUniqueKey implementation.

motlin avatar Aug 12 '24 02:08 motlin

Thanks for the inputs @donraab and @motlin. I think I'll try a default implementation in RichIterable and see how that's working. And yes, I have implemented it with the same signature you mentioned. @motlin Yes, I searched for the groupByUniqueKey implementation and that's how I found implementations of this method in around 100 classes. Will try implementing it in RichIterable but if it doesn't work out I will try implementing it in all the classes!

pranukrish avatar Aug 12 '24 02:08 pranukrish