Generics in Java

Tags: generics java
By : Jason

Is there any shorthand way of defining and using generic definations without having to keep repeating a particular generic description such that if there is a change I don't have to change all definations/usages though out the codebase for example is somethhing like this possible:

Typedef myGenDef = < Object1, Object2 >;

HashMap< myGenDef > hm = new HashMap< myGenDef >();

for (Entry< myGenDef > ent : hm..entrySet())
By : Jason


There's the pseudo-typedef antipattern...

class StringList extends ArrayList<String> { }

Good stuff, drink up! ;-)

As the article notes, this technique has some serious issues, primarily that this "typedef" is actually a separate class and thus cannot be used interchangeably with either the type it extends or other similarly defined types.

By : Shog9

The pseudo-typedef antipattern mentioned by Shog9 would work - though it's not recommended to use an ANTIPATTERN - but it does not address your intentions. The goal of pseudo-typedef is to reduce clutter in declaration and improve readability.

What you want is to be able to replace a group of generics declarations by one single trade. I think you have to stop and think: "in witch ways is it valuable?". I mean, I can't think of a scenario where you would need this. Imagine class A:

class A {
     private Map<String, Integer> values = new HashMap<String, Integer>();

Imagine now that I want to change the 'values' field to a Map. Why would exist many other fields scattered through the code that needs the same change? As for the operations that uses 'values' a simple refactoring would be enough.

hey john, you are definitely on track to becoming a mature software dev house when undertake post-mortems (aka. lessons-learnt report in PRINCE2 parlance)

i have two templates, one is thorough (my preferred one), the other can be done in only a few hours( 2-3 hours)

use the short version if you are having trouble selling it to your manager

hope that helps


By : louism

This video can help you solving your question :)
By: admin