Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How does groupingBy work when classifier returns a List?

Consider this SO answer

I have a few questions:

  1. How does it work? Does groupingBy() check if the returned value from the classifier is a List and compare each and every item? That doesn’t sound like a typical Java function to me
  2. How can List contain different variable types? Does the compiler treat it like a List<Object>?
  3. Is it anti-pattern to use List?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

What is happening here is that the value returned by the classifier is compared using the equals()-method, which in case of a List is specified:

List.equals():

Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal.

Actually, the comparing and equals-invoking is done by the underlying Map implementation.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading