Issue
Consider this SO answer
I have a few questions:
- How does it work? Does
groupingBy()
check if the returned value from theclassifier
is aList
and compare each and every item? That doesn't sound like a typical Java function to me - How can
List
contain different variable types? Does the compiler treat it like aList<Object>
? - Is it anti-pattern to use
List
?
Solution
As Hulk already said, there's absolutely no magic involved here. The List
is compared with the equals
method, just like any other object would.
To answer your second question, the type of items within a list can be controlled with generics. It's too bad they actually use a raw type in the answer you linked to. One should have used List<?>
instead, but List<Object>
would also have been fine.
Typically, the underlying array of a list implementation uses Object[]
(at least for ArrayList
is this the case).
A List<Object>
can only contain Object
instances, but guess what, all objects in the Java language are Object
s, so that list can contain any object.
Immutability
It is important that the keys of a map (in this case List
) are immutable. The way a typical Map
works, is that it relies on the keys not being changed. Fortunately, the author of the linked post uses List::of
, which returns an unmodifiable list.
Related: Are mutable hashmap keys a dangerous practice?
The author of the linked post used a List
as key, in which he stored the fields to use as classifier. I would propose an alternative solution to using a list so select multiple fields to use as classifier. I would create a record
instead, to make the intent clear of creating a classifier: record AgeAndNameClassifier(int age, String name) { }
and then grouping by: groupingBy(item -> new AgeAndNameClassifier(item.getAge(), item.getName())...
Answered By - MC Emperor