Re: Class<T> question
Randall R Schulz <[email protected]> Wed, 5 Dec 2007 09:39:10 -0800
| Newsgroups | gmane.comp.windows.devel.java.advanced |
|---|---|
| Message-ID | <[email protected]> |
On Wednesday 05 December 2007 08:36, Patrick Schneider wrote:
> In order to cut down on the type specifiers, I was shown this trick
> earlier in the week...
>
> Map<String, Object> map = createMap();
>
> ...
>
> public static <K, V> Map<K, V> createMap() {
> return new HashMap<K, V>();
> }
>
> ...which is essentially the same idea as what's going on in the
> implementation of Collections.emptyMap(). Since the method is
> generic, you only need to specify the types of the Map on the left
> hand side, saving you from typing two type specifiers.
I do the same, though originally I put in the collection factory methods
before generics came along. I did it so I could easily substitute one
collections library for another throughout my application to measure
their performance characteristics.
When generics came along, I just generified the interface, but as you
point out, there's an additional advantage gained from the type
inference done by the genericized compiler.
Also, when I face generic-related perplexity, I usually turn to Angelika
Langer's extensive generics FAQ:
<http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html>.
> Patrick
Randall Schulz
===================================
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com