Utility class. A TypedMap is just a Linked HashMap<String,Object> with 2 extra methods:
- public <T> T getTyped(String key)
- public <T> T getTypedOr(String key, T defaultValue)
The first method lets the end user cast implicitly the returned Object into a custom type passed at call-time
// The old way
Map columns = manager.nativeQuery("SELECT * FROM users WHERE userId = 10").getFirst();
String name = (String)columns.get("name"); // BAD !
Long age = (Long)columns.get("age"); // BAD !
// With TypedMap
RegularStatement statement = session.newSimpleStatement("SELECT * FROM users WHERE userId = 10");
TypedMap columns = manager.typedQuery(statement).getOne();
// Explicit type (String) is passed to method invocation
String name = columns.<String>getTyped("name");
// No need to provide explicit type. The compiler will infer type in this case
Long age = columns.get("age");
Since call to
getTyped(String key) may return null, you can use the second method
getTypedOr(String key, T defaultValue) to pass a fallback value