Using Generics to auto-cast

suggest change

With generics, it’s possible to return whatever the caller expects:

private Map<String, Object> data;
public <T> T get(String key) {
    return (T) data.get(key);
}

The method will compile with a warning. The code is actually more safe than it looks because the Java runtime will do a cast when you use it:

Bar bar = foo.get("bar");

It’s less safe when you use generic types:

List<Bar> bars = foo.get("bars");

Here, the cast will work when the returned type is any kind of List (i.e. returning List<String> would not trigger a ClassCastException; you’d eventually get it when taking elements out of the list).

To work around this problem, you can create an API which uses typed keys:

public final static Key<List<Bar>> BARS = new Key<>("BARS");

along with this put() method:

public <T> T put(Key<T> key, T value);

With this approach, you can’t put the wrong type into the map, so the result will always be correct (unless you accidentally create two keys with the same name but different types).

Related:

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Generics:
* Using Generics to auto-cast

Table Of Contents
6 Generics
8 Arrays
10 Maps
11 Strings
25 JAXB
29 Enums
32 Audio
41 Scanner
63 Logging
75 Lists
78 Sets
89 JAX-WS
96 XJC
98 Process
106 Modules
114 Applets
122 JNDI
139 JavaBean
141 Literals
144 Packages
150 JMX
153 JShell
159 Sockets
167 Enum Map
175 Hashtable
177 SortedMap