1) what does cardinalityA = CollectionUtils. getCardinalityMap(a); means? Is this some kind of typecasting?
It is not a typecasting. It is explicit setting generic parameter for generic method. CardinalityHelper has generic parameter <O>
as you can see from the declaration:
private static class CardinalityHelper<O> {
It is typesafe way for the clients of the CardinalityHelper to use this class.
2) What does return type
<O> Map<O, Integer>
means?
- generic parameter declaration. Map - return type.
3) What is this trick called?
It is using Java Generic methods.
You also can find simular usage in standart JDK library. For example: java.util.Collections.sort(java.util.List)
Java implementation of Metaprogramming principles is very limited, so you are able to use only Generic Type parameters to create typesafe containers, which required by strongly typed nature of Java.