From charlesreid1

(Created page with "This gets complicated fast when you're using generic types. See https://charlesreid1.com:3000/cs/java/src/master/hash/README.md#timing-comparison-built-in-map-vs-hand-rolled-...")
 
m (Replacing charlesreid1.com:3000 with git.charlesreid1.com)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
This gets complicated fast when you're using generic types.
This gets complicated fast when you're using generic types.


See https://charlesreid1.com:3000/cs/java/src/master/hash/README.md#timing-comparison-built-in-map-vs-hand-rolled-map
See https://git.charlesreid1.com/cs/java/src/master/hash/README.md#timing-comparison-built-in-map-vs-hand-rolled-map


Neal Grafter's blog has a [http://gafter.blogspot.com/2006/11/reified-generics-for-java.html page talking about reified generics]. This has to do with casting objects to generic types, and casting to objects that utilize the diamond notation, like Collections objects.
Neal Grafter's blog has a [http://gafter.blogspot.com/2006/11/reified-generics-for-java.html page talking about reified generics]. This has to do with casting objects to generic types, and casting to objects that utilize the diamond notation, like Collections objects.


[https://stackoverflow.com/questions/27153731/parametrized-method-in-java-try-catch-does-not-catch-classcastexception-on-and/27154147#27154147 This stack overflow question] addresses some similar issues.
[https://stackoverflow.com/questions/27153731/parametrized-method-in-java-try-catch-does-not-catch-classcastexception-on-and/27154147#27154147 This stack overflow question] addresses some similar issues.
The approach that introduces the least amount of conflict is to use (Object o) as a parameter type, then cast to whatever type you need. If you must cast to a type that takes generic parameter types, like a Map or a Set, you can explicitly tell the complier not to complain by adding the decorator <code>@SuppressWarnings("unchecked")</code> ahead of the method where the cast happens or ahead of the class definition of the type. See [https://stackoverflow.com/questions/509076/how-do-i-address-unchecked-cast-warnings#509115 this stack overflow question] for some additional information.
[[Category:Java]]
[[Category:OOP]]
[[Category:Casting]]
[[Category:Type Checking]]
{{CSFlag}}

Latest revision as of 03:33, 9 October 2019

This gets complicated fast when you're using generic types.

See https://git.charlesreid1.com/cs/java/src/master/hash/README.md#timing-comparison-built-in-map-vs-hand-rolled-map

Neal Grafter's blog has a page talking about reified generics. This has to do with casting objects to generic types, and casting to objects that utilize the diamond notation, like Collections objects.

This stack overflow question addresses some similar issues.

The approach that introduces the least amount of conflict is to use (Object o) as a parameter type, then cast to whatever type you need. If you must cast to a type that takes generic parameter types, like a Map or a Set, you can explicitly tell the complier not to complain by adding the decorator @SuppressWarnings("unchecked") ahead of the method where the cast happens or ahead of the class definition of the type. See this stack overflow question for some additional information.





See also: