As someone who has shifted from Python to Java, this article discusses two of the problems I’ve faced in Java that don’t exist in Python:
- Java’s type system cannot represent containers or maps with heterogenous value types. Python dicts can hold anything.
- Java’s methods lack named and optional parameters. These useful language features are provided by Python, Ruby, and C#.
As a response to these problems, this article presents a small library that makes them a bit less painful. We build on Effective Java’s item #29: consider typesafe heterogeneous containers. The library code is available at https://github.com/stevewedig/blog and is published to Maven for easy usage as a dependency. This is the second in a series of Java libraries I’m sharing on this blog (the first was Value Objects in Java & Python).
Article Outline
- Two problems with Java
- Get and cast: Heterogenous maps without type safety
- TypeMap: Effective Java’s type safe heterogenous map
- Symbol: A key with a value type parameter
- SymbolMap: An improved type safe heterogenous map
- SymbolBus: Heterogeneous event publishing/subscribing
- Using SymbolMap for named and optional parameters
- SymbolFormat: Parsing & writing SymbolMaps
- SymbolSchema: Validating the symbols in SymbolMaps